Browse Source

feat: 增加项目自动化部署配置

sunxiao 5 months ago
parent
commit
5481e43f30

+ 26 - 0
deploy/config.js

@@ -0,0 +1,26 @@
+export const config = [
+  {
+    id: 0,
+    nodeEnv: "test",
+    name: "测试环境",
+    domain: "",
+    host: "10.0.0.28",
+    port: "22",
+    username: "root",
+    password: "hsmysql",
+    path: "/data/node_web/smartRobot",
+    removepath: "/data/node_web/smartRobot"
+  },
+  {
+    id: 1,
+    nodeEnv: "prod",
+    name: "生产环境",
+    domain: "",
+    host: "192.168.9.54",
+    port: "22",
+    username: "root",
+    password: "admin1,xxh",
+    path: "/data/node_web/smartRobot",
+    removepath: "/data/node_web/smartRobot"
+  },
+];

+ 78 - 0
deploy/index.js

@@ -0,0 +1,78 @@
+import Client from 'ssh2-sftp-client';
+import chalk from 'chalk';
+import ora from 'ora';
+import shell from 'shelljs';
+import path from 'path';
+import { fileURLToPath } from 'url';
+import { config } from './config.js';
+
+const rawArgv = process.argv.slice(2);
+const filterStage = rawArgv.includes("--prod") ? "prod" : "test";
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+
+// 构建
+const compileDist = async () => {
+  console.log(chalk.blue("项目开始构建"));
+  if (shell.exec(`npm run build:${filterStage}`).code === 0) {
+    console.log(chalk.blue("项目构建成功"));
+  }
+}
+
+// 获取配置
+const getConfig = () => {
+  const [ result ] = config.filter(({ nodeEnv }) => nodeEnv === filterStage);
+  return result;
+}
+
+// 部署
+const connectShell = async () => {
+  const sftp = new Client();
+  const item = getConfig();
+  let spinner = null;
+
+  // 打印信息
+  const printMsg = ({ color, text }) => {
+    console.log(chalk.red(`${item.host} --> `) + chalk[color](text));
+  }
+
+  printMsg({color: 'green', text: '服务器连接中'});
+
+  sftp.connect({
+    host: item.host,
+    port: item.port,
+    username: item.username,
+    password: item.password,
+  })
+  .then(() => {
+    printMsg({color: 'green', text: '服务器连接成功'});
+    printMsg({color: 'yellow', text: '执行删除文件中'});
+    return sftp.rmdir(item.path, true);
+  })
+  .then(() => {
+    printMsg({color: 'green', text: '执行删除文件成功'});
+    printMsg({color: 'green', text: '即将开始上传'});
+    spinner = ora().start();
+    spinner.text = '文件上传中,请等待'
+    return sftp.uploadDir(path.resolve(__dirname, "../dist"), item.path);
+  })
+  .then(() => {
+    spinner.info('文件上传结束')
+    spinner.stop();
+    printMsg({color: 'green', text: '上传完成,部署成功'});
+    sftp.end();
+  })
+  .catch((err) => {
+    console.error(
+      err,
+      chalk.red(`${item.host} -->`) + chalk.red(`上传失败`)
+    );
+    sftp.end();
+  });
+}
+
+async function runStart() {
+  await compileDist();
+  await connectShell();
+}
+
+runStart();

+ 3 - 1
package.json

@@ -9,7 +9,9 @@
     "dev": "vite",
     "build:prod": "vite build",
     "build:test": "vite build --mode test",
-    "preview": "vite preview"
+    "preview": "vite preview",
+    "deploy:prod": "node deploy/index.js --prod",
+    "deploy:test": "node deploy/index.js --test"
   },
   "repository": {
     "type": "git",

+ 11 - 0
public/ecosystem.config.js

@@ -0,0 +1,11 @@
+module.exports = {
+  apps: [{
+    name: 'smartRobot',
+    port: '8887',
+    output: './pm2-log/nitro-back-out.log',
+    error: './pm2-log/nitro-back-error.log',
+    ignore_watch: ["node_modules", "logs"],
+    script: 'serve ./ --listen 8887',
+    args: '.'
+  }]
+}

BIN
public/favicon.ico


+ 1 - 1
src/views/report/laboratory-month/index.vue

@@ -135,7 +135,7 @@ const handleExport = () => {
   
   const dateResult = getStartTimeAndEndTime();
 
-  proxy.getDownload("/business/exportAssayPageListByDeviceNoAndDate", {
+  proxy.getDownload("/business/exportAssayCountListByDates", {
     ...dateResult,
   }, `${new Date().getTime()}.xlsx`);
 }

+ 1 - 1
src/views/report/record/index.vue

@@ -123,7 +123,7 @@ const handleReset = () => {
 const handleExport = () => {
   const [ timeBegin, timeEnd ] = datePieckerValue.value;
   
-  proxy.getDownload("/business/exportAssayCountListByDates", {
+  proxy.getDownload("/business/exportAssayPageListByDeviceNoAndDate", {
     timeBegin, timeEnd, deviceNo: queryParams.value.deviceNo
   }, `${new Date().getTime()}.xlsx`);
 }