diff --git a/src/tools/childProcess.js b/src/tools/childProcess.js deleted file mode 100644 index e0307a4..0000000 --- a/src/tools/childProcess.js +++ /dev/null @@ -1,38 +0,0 @@ -const {exec} = require("child_process"); - -class Process { - constructor(cmd, cb) { - this.cmd = cmd - this.pid = exec(cmd, {windowsHide: true, encoding: "utf8"}); - this.callback = cb - } - - static new(cmd) { - return new Promise((resolve, reject) => exec(cmd, {windowsHide: true}, (err) => { - if (!err) { - resolve() - } else reject(err) - })) - } - - ins() { - return new Promise((resolve, reject) => { - this.pid.on('close', (code, err) => { - if (code == 0 && !err) { - resolve() - } else reject(err) - }) - this.pid.on('exit', (code, err) => { - if (code == 0 && !err) { - resolve() - } else reject(err) - }) - this.pid.stdout.on('data', data => { - console.log(data) - this.callback?.() - }) - }) - } -} - -module.exports = Process diff --git a/src/tools/exec.js b/src/tools/exec.js index 2333861..139105f 100644 --- a/src/tools/exec.js +++ b/src/tools/exec.js @@ -1,20 +1,12 @@ -const {exec} = require("child_process"); -const execute = cmd => new Promise((resolve, reject) => { - let pid = exec(cmd, {windowsHide: true}, (err) => { +const cp = require("child_process"); +const execute = (cmd, signal) => new Promise((resolve, reject) => { + const pid = cp.exec(cmd, {windowsHide: true, signal}, (err) => { if (!err) { - resolve() + resolve(cmd) } else reject(err) }) - pid.on('close', (code, err) => { - if (code == 0 && !err) { - resolve() - } else reject() - }) pid.stdout.on('data', data => { console.log(data) }) - pid.stderr.on('data', data => { - console.log(data) - }) }) module.exports = execute diff --git a/src/websocket/custom/getZip.js b/src/websocket/custom/getZip.js index 140665a..772536e 100644 --- a/src/websocket/custom/getZip.js +++ b/src/websocket/custom/getZip.js @@ -1,37 +1,66 @@ 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 controller = new AbortController() const handleZip = (id, ws) => { const uniCon = `id='${id}'` const sendMessage = data => ws.send(JSON.stringify(data)) + + class counter { + constructor(remark) { + this.count = 0 + this.timer = setInterval(() => { + sendMessage({code: 0, progress: ++this.count, remark}) + }, 6000) + } + + set(progress, remark) { + if (this.timer) clearInterval(this.timer) + this.count = progress + this.timer = setInterval(() => { + sendMessage({code: 0, progress: ++this.count, remark}) + }, 4000) + } + + stop() { + if (this.timer) clearInterval(this.timer) + } + + finish(remark) { + if (this.timer) clearInterval(this.timer) + sendMessage({code: 0, progress: 100, remark}) + } + } + db.detail({table: "node_custom_config", id}).then(info => { if (info?.id) { - sendMessage({code: 0, progress: 1, data: `正在处理 ${info.name} 的打包工作...`}) + const {signal} = controller; + const progress = new counter(`正在处理 ${info.name} 的打包工作...`) const buildPath = { web: 'base-web', wxwork: 'base-wxcp', mp: 'dvcp_v2_wxmp' }[info.type] || {} - let path = `../${buildPath}`, {dist} = info, progress = 30 + let path = `../${buildPath}`, {dist} = info 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(() => 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()) + execute(`./shell/update.sh ${info.name} ${path}`, signal) + ]) + .then(cmd => progress.set(30, cmd)) + .then(() => execute(`cd ${path}&&npm run apps&&node bin/pages.js ${id}&&npm run build`, signal)) + .then(cmd => progress.set(70, cmd)) .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(() => execute(`./shell/move.sh ${info.type} ${path} ${dist}`, signal)) + .then(cmd => progress.set(90, cmd)) .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})) + .then(row => progress.finish(row)) .catch(err => { console.log(err) + progress.stop() 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})) @@ -53,12 +82,11 @@ module.exports = { if (res?.data) { const data = JSON.parse(res.data) if (data.cid == id) { - pid.unref() + controller.abort() } else if (id == data.id) { - pid.unref() handleZip(id, ws) } } } - } + }, }