Files
dvcp-node-service/src/websocket/custom/getZip.js
2023-01-18 14:25:00 +08:00

65 lines
2.6 KiB
JavaScript

const db = require("../../utils/dbUitls");
const execute = require("../../tools/exec")
const process = require("../../tools/childProcess")
const dayjs = require("dayjs")
const fse = require("fs-extra");
let pid
const handleZip = (id, ws) => {
const uniCon = `id='${id}'`
const sendMessage = data => ws.send(JSON.stringify(data))
db.detail({table: "node_custom_config", id}).then(info => {
if (info?.id) {
sendMessage({code: 0, progress: 1, data: `正在处理 ${info.name} 的打包工作...`})
const buildPath = {
web: 'base-web',
wxwork: 'base-wxcp',
mp: 'dvcp_v2_wxmp'
}[info.type] || {}
let path = `../${buildPath}`, {dist} = info, progress
dist = dist || `../zips/${info.name}v${info.version || "1.0.0"}`
Promise.all([
db.query(`update node_custom_config set download=null,error=null,zipTime='${dayjs().format("YYYY-MM-DD HH:mm:ss")}' where ${uniCon}`),
process.new(`./shell/update.sh ${info.name} ${path}`)])
.then(() => progress = 30, sendMessage({code: 0, progress}))
.then(() => pid = new process(`cd ${path}&&npm run apps&&node bin/pages.js ${id}&&npm run build`), () => sendMessage({code: 0, progress: ++progress}))
.then(() => pid.ins())
.then(() => fse.emptyDir(dist))
.then(() => progress = 70, sendMessage({code: 0, progress}))
.then(() => pid = new process(`./shell/move.sh ${info.type} ${path} ${dist}`), () => sendMessage({code: 0, progress: ++progress})).then(() => pid.ins())
.then(() => progress = 90, sendMessage({code: 0, progress}))
.then(() => db.query(`update node_custom_config set download='${dayjs().format("YYYY-MM-DD HH:mm:ss")}',error='' where ${uniCon}`))
.then(() => db.detail({table: "node_custom_config", id}))
.then(row => sendMessage({code: 0, progress: 100, row}))
.catch(err => {
console.log(err)
const msg = `执行失败:${err.cmd}`
return db.query(`update node_custom_config set error='${msg}',zipTime=null where ${uniCon}`)
.then(() => db.detail({table: "node_custom_config", id}))
.then(row => sendMessage({code: 1, row}))
.catch(() => 0)
})
}
}).catch(err => {
console.log(err)
})
}
module.exports = {
action: "/custom/getZip",
execute: (ws, request) => {
const {id} = request.query
handleZip(id, ws)
ws.onmessage = res => {
if (res?.data) {
const data = JSON.parse(res.data)
if (data.cid == id) {
pid.kill()
} else if (id == data.id) {
pid.kill()
handleZip(id, ws)
}
}
}
}
}