diff --git a/.foreverignore b/.foreverignore new file mode 100644 index 0000000..e051e72 --- /dev/null +++ b/.foreverignore @@ -0,0 +1 @@ +/zips/ diff --git a/index.js b/index.js index fdf22c8..711f06a 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ const express = require('express') -const db = require('./utils/dbUitls') -const rest = require('./rest') +const db = require('./src/utils/dbUitls') +const rest = require('./src/rest') const app = express() const port = 12525 diff --git a/package.json b/package.json index ff2d117..809eb80 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "node服务端", "main": "index.js", "scripts": { - "dev": "forever -w index.js", + "dev": "forever -w --watchDirectory src index.js ", "pro": "forever start index.js" }, "repository": { @@ -20,6 +20,7 @@ }, "author": "kubbo", "dependencies": { + "archiver": "^5.3.0", "express": "^4.17.3", "fs-extra": "^10.0.1", "helmet": "^5.0.2", diff --git a/rest/autodeploy/download.js b/rest/autodeploy/download.js deleted file mode 100644 index 4bc19c9..0000000 --- a/rest/autodeploy/download.js +++ /dev/null @@ -1,30 +0,0 @@ -const dbUtils = require("../../utils/dbUitls"); -const fse = require("fs-extra"); -module.exports = { - action: "/node/autodeploy/download", - method: "post", - execute: (request, response) => { - let id = request.query?.id, sql = `select * from node_autodeploy where id='${id}'` - dbUtils.query(sql).then(res => { - let info = res?.[0] - if (info?.id) { - response.send({code: 0}) - fse.emptyDir('../zips/dist', err => { - if (!err) { - execute(`cd ../zips&&git clone ${info.git} ./dist`) - .then(() => execute(`cd ../zips/dist&&git checkout ${info.branch}`)) - .then(() => execute(`cd ../zips/dist&&npm i&&npm run build`)) - .catch(err => { - console.log(err) - }) - } else { - console.log(err) - } - }) - } else response.send({code: 1, err: "无法找到git信息"}) - }).catch(err => { - console.log(err) - response.send({code: 1, err: err.sqlMessage}) - }) - } -} diff --git a/rest/autodeploy/getZip.js b/rest/autodeploy/getZip.js deleted file mode 100644 index cfb0678..0000000 --- a/rest/autodeploy/getZip.js +++ /dev/null @@ -1,39 +0,0 @@ -const dbUtils = require("../../utils/dbUitls"); -const {exec} = require("child_process"); -const fse = require('fs-extra') -const execute = cmd => new Promise((resolve, reject) => { - exec(cmd, (err, stdout) => { - if (!err) { - console.log(stdout) - resolve() - } else reject(err) - }) -}) -module.exports = { - action: "/node/autodeploy/getZip", - method: "post", - execute: (request, response) => { - let id = request.query?.id, sql = `select * from node_autodeploy where id='${id}'` - dbUtils.query(sql).then(res => { - let info = res?.[0] - if (info?.id) { - response.send({code: 0}) - fse.emptyDir('../zips/' + info.id, err => { - if (!err) { - execute(`cd ../zips&&git clone ${info.git} ./${info.id}`) - .then(() => execute(`cd ../zips/${info.id}&&git checkout ${info.branch}`)) - .then(() => execute(`cd ../zips/${info.id}&&npm i&&npm run build`)) - .catch(err => { - console.log(err) - }) - } else { - console.log(err) - } - }) - } else response.send({code: 1, err: "无法找到git信息"}) - }).catch(err => { - console.log(err) - response.send({code: 1, err: err.sqlMessage}) - }) - } -} diff --git a/config/db.js b/src/config/db.js similarity index 100% rename from config/db.js rename to src/config/db.js diff --git a/rest/autodeploy/add.js b/src/rest/autodeploy/add.js similarity index 100% rename from rest/autodeploy/add.js rename to src/rest/autodeploy/add.js diff --git a/rest/autodeploy/confirmZip.js b/src/rest/autodeploy/confirmZip.js similarity index 74% rename from rest/autodeploy/confirmZip.js rename to src/rest/autodeploy/confirmZip.js index 589b713..66be64c 100644 --- a/rest/autodeploy/confirmZip.js +++ b/src/rest/autodeploy/confirmZip.js @@ -8,7 +8,10 @@ module.exports = { dbUtils.query(sql).then(res => { let info = res?.[0] if (info?.id) { - response.send({code: 0}) + fse.pathExists(`zips/${info.id}/dist`, (err, exists) => { + console.log(`zips/${info.id}/dist=========>${exists}`) + response.send({code: 0, data: exists}) + }) } else response.send({code: 1, err: "无法找到git信息"}) }).catch(err => { console.log(err) diff --git a/rest/autodeploy/delete.js b/src/rest/autodeploy/delete.js similarity index 100% rename from rest/autodeploy/delete.js rename to src/rest/autodeploy/delete.js diff --git a/src/rest/autodeploy/download.js b/src/rest/autodeploy/download.js new file mode 100644 index 0000000..bb95834 --- /dev/null +++ b/src/rest/autodeploy/download.js @@ -0,0 +1,40 @@ +const dbUtils = require("../../utils/dbUitls"); +const archiver = require("archiver") +const fse = require("fs-extra"); +const fs = require("fs"); +module.exports = { + action: "/node/autodeploy/download", + method: "post", + execute: (request, response) => { + let id = request.query?.id, sql = `select * from node_autodeploy where id='${id}'` + dbUtils.query(sql).then(res => { + let info = res?.[0] + if (info?.id) { + let path = `zips/${info.id}/dist`, zipPath = `./zips/${info.id}.zip` + fse.removeSync(zipPath) + fse.pathExists(path, (err, exists) => { + console.log(`${path}=========>${exists}`) + if (exists) { + let output = fs.createWriteStream(zipPath), + arc = archiver('zip') + arc.on('error', err => { + response.send({code: 1, err}) + }) + arc.on('progress', (e) => { + console.log('%s压缩进度...%s/%s', info.id, e.entries.processed, e.entries.total) + }) + arc.pipe(output) + arc.directory(path, false) + arc.finalize().then(() => { + console.log('压缩完成!') + response.download(zipPath, `${info.id}.zip`) + }) + } else response.send({code: 1, err: "没有打包文件!"}) + }) + } else response.send({code: 1, err: "无法找到git信息"}) + }).catch(err => { + console.log(err) + response.send({code: 1, err: err.sqlMessage}) + }) + } +} diff --git a/src/rest/autodeploy/getZip.js b/src/rest/autodeploy/getZip.js new file mode 100644 index 0000000..f767945 --- /dev/null +++ b/src/rest/autodeploy/getZip.js @@ -0,0 +1,25 @@ +const db = require("../../utils/dbUitls"); +const zip = require("../../tools/zipProject") +module.exports = { + action: "/node/autodeploy/getZip", + method: "post", + execute: (request, response) => { + let id = request.query?.id, sql = `select * from node_autodeploy where id='${id}'` + db.query(sql).then(res => { + let info = res?.[0] + if (info?.id) { + setTimeout(() => { + response.send({code: 0}) + }, 2000) + zip(info).then(() => { + db.query(`update node_autodeploy set download='${new Date()}' where id='${info.id}'`) + }).catch(err => { + console.log(err) + }) + } else response.send({code: 1, err: "无法找到git信息"}) + }).catch(err => { + console.log(err) + response.send({code: 1, err: err.sqlMessage}) + }) + } +} diff --git a/rest/autodeploy/list.js b/src/rest/autodeploy/list.js similarity index 100% rename from rest/autodeploy/list.js rename to src/rest/autodeploy/list.js diff --git a/src/rest/autodeploy/updateSystem.js b/src/rest/autodeploy/updateSystem.js new file mode 100644 index 0000000..6b58b13 --- /dev/null +++ b/src/rest/autodeploy/updateSystem.js @@ -0,0 +1,29 @@ +const dbUtils = require("../../utils/dbUitls"); +const zip = require("../../tools/zipProject"); +const db = require("../../utils/dbUitls"); +const exec = require("../../tools/exec") + +module.exports = { + action: "/node/autodeploy/updateSystem", + method: "post", + execute: (request, response) => { + let id = request.query?.id, sql = `select * from node_autodeploy where id='${id}'` + dbUtils.query(sql).then(res => { + let info = res?.[0] + if (info?.id) { + setTimeout(() => { + response.send({code: 0}) + }, 2000) + zip(info).then(() => { + db.query(`update node_autodeploy set download='${new Date()}' where id='${info.id}'`) + exec(`cp -r zips/${info.id}/dist ${info.target}`).then(() => { + console.log("部署完毕!") + }) + }) + } else response.send({code: 1, err: "无法找到git信息"}) + }).catch(err => { + console.log(err) + response.send({code: 1, err: err.sqlMessage}) + }) + } +} diff --git a/rest/index.js b/src/rest/index.js similarity index 66% rename from rest/index.js rename to src/rest/index.js index 9953f58..b6079c8 100644 --- a/rest/index.js +++ b/src/rest/index.js @@ -1,10 +1,10 @@ const {findFile} = require("../utils/fsUtils"); module.exports = { init: ins => { - return findFile('./rest', file => { + return findFile('./src/rest', file => { if (!/index\.js/.test(file)) { - let rest = require(file.replace(/rest/, '.')) - console.log(`初始化接口...${rest.action}`) + let rest = require(file.replace(/src[\\\/]rest/, '.')) + console.log(`初始化接口:${rest.action}`) if (rest.method == "post") { ins.post(rest.action, (req, res) => rest.execute(req, res)) } diff --git a/rest/sysuser/sysuser.js b/src/rest/sysuser/sysuser.js similarity index 100% rename from rest/sysuser/sysuser.js rename to src/rest/sysuser/sysuser.js diff --git a/src/tools/exec.js b/src/tools/exec.js new file mode 100644 index 0000000..95eb9de --- /dev/null +++ b/src/tools/exec.js @@ -0,0 +1,10 @@ +const {exec} = require("child_process"); +const execute = cmd => new Promise((resolve, reject) => { + exec(cmd, (err, stdout) => { + if (!err) { + console.log(stdout) + resolve() + } else reject(err) + }) +}) +module.exports = execute diff --git a/src/tools/zipProject.js b/src/tools/zipProject.js new file mode 100644 index 0000000..e2afbc0 --- /dev/null +++ b/src/tools/zipProject.js @@ -0,0 +1,21 @@ +const fse = require("fs-extra"); +const execute = require("./exec") +module.exports = info => { + return new Promise((resolve, reject) => { + fse.emptyDir(`zips/${info.id}`, err => { + if (!err) { + execute(`cd zips&&git clone ${info.git} ./${info.id}`) + .then(() => execute(`cd zips/${info.id}&&git checkout ${info.branch}`)) + .then(() => execute(`cd zips/${info.id}&&npm i&&npm run build`)) + .then(() => resolve()) + .catch(errs => { + console.log(errs) + reject(errs) + }) + } else { + reject(err) + console.log(err) + } + }) + }) +} diff --git a/utils/dbUitls.js b/src/utils/dbUitls.js similarity index 100% rename from utils/dbUitls.js rename to src/utils/dbUitls.js diff --git a/utils/fsUtils.js b/src/utils/fsUtils.js similarity index 100% rename from utils/fsUtils.js rename to src/utils/fsUtils.js