diff --git a/src/websocket/custom/getZip.js b/src/websocket/custom/getZip.js index d84dea8..373557b 100644 --- a/src/websocket/custom/getZip.js +++ b/src/websocket/custom/getZip.js @@ -41,34 +41,29 @@ const handleZip = (id, ws) => { const {signal} = controller; const progress = new counter(`正在处理 ${info.name} 的打包工作...`) const buildPath = { - web: 'base-web', - wxwork: 'base-wxcp', - mp: 'dvcp_v2_wxmp' + web: 'base-web', wxwork: 'base-wxcp', mp: 'dvcp_v2_wxmp' }[info.type] || {} 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}`), - 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(() => 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 => 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}`) + Promise.all([db.query(`update node_custom_config set download=null,error=null,zipTime='${dayjs().format("YYYY-MM-DD HH:mm:ss")}' where ${uniCon}`), 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(() => 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: 1, row})) - .catch(() => 0) - }) + .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})) + .then(row => sendMessage({code: 1, row})) + .catch(() => 0) + }) } }).catch(err => { console.log(err) @@ -76,20 +71,13 @@ const handleZip = (id, ws) => { } 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) { - controller.abort() - controller = new AbortController() - } else if (id == data.id) { - handleZip(id, ws) - } - } + action: "/custom/getZip", execute: (ws, request) => { + const {id, cid} = request.params + if (cid) { + controller.abort() + controller = new AbortController() + } else if (id) { + handleZip(id, ws) } }, } diff --git a/src/websocket/index.js b/src/websocket/index.js index 84b698f..290a06e 100644 --- a/src/websocket/index.js +++ b/src/websocket/index.js @@ -1,14 +1,27 @@ const {findFile} = require("../utils/fsUtils"); const chalk = require("chalk"); +const dayjs = require("dayjs"); const {log} = console module.exports = { init: ins => { - return findFile('./src/websocket', file => { - if (!/index\.js/.test(file)) { - const ws = require(file.replace(/src[\\\/]websocket/, '.')) - log(`${chalk.bgBlue.black(" WEBSOCKET ")} ${ws.action}`) - ins.ws(ws.action, ws.execute) - } + ins.ws('/ws', ws => { + log(`${chalk.bgBlue.black(" WEBSOCKET ")} 服务已启动!`) + ins.send('您已成功连接到node websocket') + let heartBeat = setInterval(() => wx.send(`heartBeat at ${dayjs().format("YYYY-MM-DD HH:mm:ss")}`), 5000) + ws.on('close', () => { + log(`${chalk.bgBlue.black(" WEBSOCKET ")} 连接已断开!`) + clearInterval(heartBeat) + }) + let actions = {} + return findFile('./src/websocket', file => { + if (!/index\.js/.test(file)) { + actions[ws.action] = ws.execute + } + }).then(() => ws.on('message', res => { + if (res?.action) { + actions[res.action]?.(ws, JSON.parse(res)) + } + })) }) } }