优化打包方法
This commit is contained in:
16
index.js
16
index.js
@@ -1,17 +1,19 @@
|
|||||||
const express = require('express')
|
const express = require('express')
|
||||||
const db = require('./src/utils/dbUitls')
|
const db = require('./src/utils/dbUitls')
|
||||||
const rest = require('./src/rest')
|
const rest = require('./src/rest')
|
||||||
const app = express()
|
const ws = require('./src/websocket')
|
||||||
|
const ews = require('express-ws')
|
||||||
|
const chalk = require("chalk");
|
||||||
|
const log = console.log
|
||||||
|
const app = express();
|
||||||
|
ews(app)
|
||||||
const port = 12525
|
const port = 12525
|
||||||
|
chalk.level = 1
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log('启动数据库连接池...')
|
|
||||||
db.init()
|
db.init()
|
||||||
console.log('启动接口...')
|
|
||||||
app.use(express.json()) // for parsing application/json
|
app.use(express.json()) // for parsing application/json
|
||||||
app.use(express.urlencoded({extended: true})) // for parsing application/x-www-form-urlencoded
|
app.use(express.urlencoded({extended: true})) // for parsing application/x-www-form-urlencoded
|
||||||
rest.init(app).then(()=>{
|
Promise.all([rest.init(app), ws.init(app)]).then(() => {
|
||||||
console.log(`serve is listening on ${port}`)
|
log(`${chalk.bgGreen.black(" DONE ")} serve is listening on ${port}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -5,7 +5,8 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "forever -w --watchDirectory src index.js ",
|
"dev": "forever -w --watchDirectory src index.js ",
|
||||||
"pro": "forever start index.js"
|
"pro": "forever start index.js",
|
||||||
|
"stop": "forever stopall"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -23,10 +24,14 @@
|
|||||||
"axios": "^1.2.1",
|
"axios": "^1.2.1",
|
||||||
"dayjs": "^1.11.0",
|
"dayjs": "^1.11.0",
|
||||||
"express": "^4.17.3",
|
"express": "^4.17.3",
|
||||||
|
"express-ws": "^5.0.2",
|
||||||
"fs-extra": "^10.0.1",
|
"fs-extra": "^10.0.1",
|
||||||
"helmet": "^5.0.2",
|
"helmet": "^5.0.2",
|
||||||
"mysql": "^2.18.1",
|
"mysql": "^2.18.1",
|
||||||
"sql": "^0.78.0",
|
"sql": "^0.78.0",
|
||||||
"uuid": "^8.3.2"
|
"uuid": "^8.3.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"chalk": "^4.1.2"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
const {findFile} = require("../utils/fsUtils");
|
const {findFile} = require("../utils/fsUtils");
|
||||||
|
const chalk = require("chalk");
|
||||||
|
const log = console.log
|
||||||
module.exports = {
|
module.exports = {
|
||||||
init: ins => {
|
init: ins => {
|
||||||
return findFile('./src/rest', file => {
|
return findFile('./src/rest', file => {
|
||||||
if (!/index\.js/.test(file)) {
|
if (!/index\.js/.test(file)) {
|
||||||
let rest = require(file.replace(/src[\\\/]rest/, '.'))
|
let rest = require(file.replace(/src[\\\/]rest/, '.'))
|
||||||
console.log(`初始化接口:${rest.action}`)
|
log(`${chalk.bgBlue.black(" REST ")} ${rest.action}`)
|
||||||
if (rest.method == "post") {
|
if (rest.method == "post") {
|
||||||
ins.post(rest.action, (req, res) => rest.execute(req, res))
|
ins.post(rest.action, (req, res) => rest.execute(req, res))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).then(() => {
|
|
||||||
console.log("接口初始化完毕")
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
28
src/tools/childProcess.js
Normal file
28
src/tools/childProcess.js
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
const {exec} = require("child_process");
|
||||||
|
|
||||||
|
class Process {
|
||||||
|
constructor(cmd) {
|
||||||
|
this.cmd = cmd
|
||||||
|
this.pid = exec(cmd, {windowsHide: true, encoding: "utf8"});
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Process
|
||||||
@@ -2,14 +2,14 @@ const mysql = require("mysql");
|
|||||||
const dbConfig = require("../config/db");
|
const dbConfig = require("../config/db");
|
||||||
const {v4: uuid} = require("uuid");
|
const {v4: uuid} = require("uuid");
|
||||||
const dayjs = require("dayjs");
|
const dayjs = require("dayjs");
|
||||||
|
const chalk = require("chalk");
|
||||||
const query = sql => new Promise((resolve, reject) => {
|
const query = sql => new Promise((resolve, reject) => {
|
||||||
this.pool?.getConnection((err, conn) => {
|
this.pool?.getConnection((err, conn) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err)
|
reject(err)
|
||||||
} else {
|
} else {
|
||||||
conn.query(sql, (err, result) => {
|
conn.query(sql, (err, result) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log(err)
|
|
||||||
reject(err)
|
reject(err)
|
||||||
} else {
|
} else {
|
||||||
conn.release()
|
conn.release()
|
||||||
@@ -23,6 +23,7 @@ module.exports = {
|
|||||||
pool: null,
|
pool: null,
|
||||||
init: () => {
|
init: () => {
|
||||||
this.pool = mysql.createPool(dbConfig)
|
this.pool = mysql.createPool(dbConfig)
|
||||||
|
console.log(`${chalk.bgBlue.black(" DATABASE ")} 数据库已连接`)
|
||||||
},
|
},
|
||||||
query,
|
query,
|
||||||
list: ({table, search, con = ''}) => {
|
list: ({table, search, con = ''}) => {
|
||||||
|
|||||||
60
src/websocket/custom/getZip.js
Normal file
60
src/websocket/custom/getZip.js
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
const db = require("../../utils/dbUitls");
|
||||||
|
const execute = require("../../tools/exec")
|
||||||
|
const process = require("../../tools/childProcess")
|
||||||
|
const dayjs = require("dayjs")
|
||||||
|
const fse = require("fs-extra");
|
||||||
|
|
||||||
|
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, pid
|
||||||
|
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: 30}))
|
||||||
|
.then(() => pid = new process(`cd ${path}&&npm run apps&&node bin/pages.js ${id}&&npm run build`)).then(() => pid.ins())
|
||||||
|
.then(() => fse.emptyDir(dist))
|
||||||
|
.then(() => sendMessage({code: 0, progress: 70}))
|
||||||
|
.then(() => pid = new process(`./shell/move.sh ${info.type} ${path} ${dist}`)).then(() => pid.ins())
|
||||||
|
.then(() => sendMessage({code: 0, progress: 90}))
|
||||||
|
.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) {
|
||||||
|
|
||||||
|
} else if (id == data.id) handleZip(id, ws)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/websocket/index.js
Normal file
14
src/websocket/index.js
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
const {findFile} = require("../utils/fsUtils");
|
||||||
|
const chalk = require("chalk");
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user