Browse Source

feat: 自动化部署配置修改以及部分样式调整

sunxiao 5 months ago
parent
commit
f0c7c53cf6
7 changed files with 83 additions and 35 deletions
  1. 2 2
      .env.development
  2. 65 27
      deploy/index.js
  3. 6 2
      package.json
  4. 4 0
      src/assets/styles/sidebar.scss
  5. 2 2
      src/router/index.js
  6. 2 0
      src/utils/request.js
  7. 2 2
      src/views/login.vue

+ 2 - 2
.env.development

@@ -7,5 +7,5 @@ VITE_APP_ENV = 'development'
 # 管理系统/开发环境
 VITE_APP_BASE_API =  http://10.0.0.28:8889/
 
-VITE_APP_BASE_TEST = http://10.0.0.28:8080/
-VITE_APP_BASE_PROD = http://192.168.9.54:8080/
+# VITE_APP_BASE_TEST = http://10.0.0.28:8080/
+# VITE_APP_BASE_PROD = http://192.168.9.54:8080/

+ 65 - 27
deploy/index.js

@@ -1,4 +1,5 @@
 import Client from 'ssh2-sftp-client';
+import ssh2 from 'ssh2';
 import chalk from 'chalk';
 import ora from 'ora';
 import shell from 'shelljs';
@@ -20,10 +21,42 @@ const compileDist = async () => {
 
 // 获取配置
 const getConfig = () => {
-  const [ result ] = config.filter(({ nodeEnv }) => nodeEnv === filterStage);
+  const [result] = config.filter(({ nodeEnv }) => nodeEnv === filterStage);
   return result;
 }
 
+
+const execRemoteCommand = async () => {
+  const config = getConfig();
+  const conn = new ssh2.Client();
+
+  conn.on("ready", function () {
+    console.log(chalk.blue("ssh2 链接成功"));
+
+    const commands = [
+      'cd /data/node_web/smartRobot',
+      'pwd',
+      'pm2 list',
+      'pm2 del smartRobot',
+      'pm2 start'
+    ];
+
+    conn.exec(commands.join(";"), { pty: true }, function (err, stream) {
+      if (err) throw err;
+      stream
+        .on('close', function (code, signal) {
+          console.log('Command 执行成功');
+          conn.end(); // 结束SSH连接
+        })
+        .on('data', function (data) {
+          process.stdout.write(data);
+        });
+
+      stream.stdin.end();
+    });
+  }).connect({ ...config });
+}
+
 // 部署
 const connectShell = async () => {
   const sftp = new Client();
@@ -35,7 +68,7 @@ const connectShell = async () => {
     console.log(chalk.red(`${item.host} --> `) + chalk[color](text));
   }
 
-  printMsg({color: 'green', text: '服务器连接中'});
+  printMsg({ color: 'green', text: '服务器连接中' });
 
   sftp.connect({
     host: item.host,
@@ -43,31 +76,36 @@ const connectShell = async () => {
     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();
-  });
+    .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();
+
+      // if (filterStage === 'prod') {
+      execRemoteCommand()
+      // }
+
+      printMsg({ color: 'green', text: '上传完成,准备指令中' });
+      sftp.end();
+    })
+    .catch((err) => {
+      console.error(
+        err,
+        chalk.red(`${item.host} -->`) + chalk.red(`上传失败`)
+      );
+      sftp.end();
+    });
 }
 
 async function runStart() {

+ 6 - 2
package.json

@@ -7,8 +7,8 @@
   "type": "module",
   "scripts": {
     "dev": "vite",
-    "build:prod": "vite build",
-    "build:test": "vite build --mode test",
+    "build:prod": "vite build --mode production",
+    "build:test": "vite build --mode development",
     "preview": "vite preview",
     "deploy:prod": "node deploy/index.js --prod",
     "deploy:test": "node deploy/index.js --test"
@@ -38,7 +38,11 @@
   },
   "devDependencies": {
     "@vitejs/plugin-vue": "5.0.5",
+    "ora": "^8.1.0",
     "sass": "1.77.5",
+    "shelljs": "^0.8.5",
+    "ssh2": "^1.16.0",
+    "ssh2-sftp-client": "^11.0.0",
     "unplugin-auto-import": "0.17.6",
     "unplugin-vue-setup-extend-plus": "1.0.1",
     "vite": "5.3.2",

+ 4 - 0
src/assets/styles/sidebar.scss

@@ -64,6 +64,10 @@
       margin-right: 16px;
     }
 
+    .el-tooltip__trigger {
+      padding-top: 20px;
+    }
+
     .el-menu {
       border: none;
       height: 100%;

+ 2 - 2
src/router/index.js

@@ -1,4 +1,4 @@
-import { createWebHistory, createRouter } from 'vue-router'
+import { createWebHashHistory, createRouter } from 'vue-router'
 /* Layout */
 import Layout from '@/layout'
 
@@ -161,7 +161,7 @@ export const dynamicRoutes = [
 ]
 
 const router = createRouter({
-  history: createWebHistory(),
+  history: createWebHashHistory(),
   routes: constantRoutes,
   scrollBehavior(to, from, savedPosition) {
     if (savedPosition) {

+ 2 - 0
src/utils/request.js

@@ -12,6 +12,7 @@ let downloadLoadingInstance;
 export let isRelogin = { show: false };
 
 axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
+
 // 创建axios实例
 const service = axios.create({
   // axios中请求配置有baseURL选项,表示请求URL公共部分
@@ -22,6 +23,7 @@ const service = axios.create({
 
 // request拦截器
 service.interceptors.request.use(config => {
+  console.log("import.meta.env.VITE_APP_BASE_API", import.meta.env.VITE_APP_BASE_API);
   // 是否需要设置 token
   const isToken = (config.headers || {}).isToken === false
   // 是否需要防止数据重复提交

+ 2 - 2
src/views/login.vue

@@ -7,8 +7,8 @@ const router = useRouter();
 const { proxy } = getCurrentInstance();
 
 const loginForm = ref({
-  username: "admin",
-  password: "admin123",
+  username: "",
+  password: "",
 });
 
 const loginRules = {