浏览代码

feat: 集成ssh2用于项目构建后自动推送部署

sunxiao 9 月之前
父节点
当前提交
b555e6dd00
共有 4 个文件被更改,包括 273 次插入91 次删除
  1. 44 89
      deploy/index.js
  2. 223 0
      package-lock.json
  3. 3 1
      package.json
  4. 3 1
      src/views/answer/AnswerView.vue

+ 44 - 89
deploy/index.js

@@ -1,19 +1,20 @@
 import Client from 'ssh2-sftp-client';
 import chalk from 'chalk';
-import shelljs from 'shelljs';
-import fs from 'fs';
+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.green("打包成功"));
+    console.log(chalk.blue("项目构建成功"));
   }
 }
 
@@ -26,98 +27,52 @@ const getConfig = () => {
 // 部署
 const connectShell = async (params) => {
   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(() => {
-
   })
-
-  // 判断需要上传的服务器
-  config
-    .filter((item) => item.nodeEnv == filterStage)
-    .map((item, index) => {
-      const sftp = new Client();
-      sftp
-        .connect({
-          host: item.host,
-          port: item.port,
-          username: item.username,
-          password: item.password,
-        })
-        // 备份
-        .then(() => {
-          if (index > 0) {
-            return "ok";
-          }
-          console.log(
-            chalk.red(`${item.host}--`) + chalk.blue(`---执行下拉文件备份---`)
-          );
-          console.log(chalk.blue(`---创建备份文件夹中···---`));
-          let newFile = `/${item.host}/${dayjs().format(
-            "YYYY-MM-DD"
-          )}/dist-${dayjs().format("HH_mm_ss")}`;
-          // 创建本地文件夹
-          fs.mkdir(
-            path.resolve(__dirname, `../distbak${newFile}`),
-            { recursive: true },
-            (err) => {
-              if (err) throw err;
-              console.log(chalk.green("---创建备份文件夹成功---"));
-            }
-          );
-          //执行服务器下拉操作
-          return sftp.downloadDir(
-            item.path, //服务器路径
-            path.resolve(__dirname, `../distbak${newFile}`) //写入的本地路径地址
-          );
-        })
-        // 删除
-        .then(() => {
-          if (index === 0) {
-            console.log(
-              chalk.red(`${item.host}--`) + chalk.blue(`---备份完成---`)
-            );
-          }
-          console.log(
-            chalk.yellow(`${item.host}--`) +
-            chalk.red(`---执行删除文件中···---`)
-          );
-          // 删除路径 递归删除
-          return sftp.rmdir(item.path, true);
-        })
-        // 上传
-        .then(() => {
-          console.log(
-            chalk.red(`${item.host}--`) + chalk.green(`执行删除文件成功---`)
-          );
-          console.log(
-            chalk.hex("#DEADED").bold(`---${item.host}执行上传文件中···---`)
-          );
-          return sftp.uploadDir(path.resolve(__dirname, "../dist"), item.path);
-        })
-        // 上传成功 关闭链接
-        .then(() => {
-          console.log(
-            chalk.yellow(`${item.host}--`) + chalk.green(`上传完成,部署成功---`)
-          );
-          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();
+    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();
+
+runStart();

+ 223 - 0
package-lock.json

@@ -38,6 +38,7 @@
         "chalk": "^5.3.0",
         "dayjs": "^1.11.11",
         "npm-run-all2": "^6.1.2",
+        "ora": "^8.0.1",
         "postcss": "^8.4.38",
         "shelljs": "^0.8.5",
         "ssh2-sftp-client": "^10.0.3",
@@ -2408,6 +2409,33 @@
       "resolved": "https://registry.npmmirror.com/claygl/-/claygl-1.3.0.tgz",
       "integrity": "sha512-+gGtJjT6SSHD2l2yC3MCubW/sCV40tZuSs5opdtn79vFSGUgp/lH139RNEQ6Jy078/L0aV8odCw8RSrUcMfLaQ=="
     },
+    "node_modules/cli-cursor": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmmirror.com/cli-cursor/-/cli-cursor-4.0.0.tgz",
+      "integrity": "sha512-VGtlMu3x/4DOtIUwEkRezxUZ2lBacNJCHash0N0WeZDBS+7Ux1dm3XWAgWYxLJFMMdOeXMHXorshEFhbMSGelg==",
+      "dev": true,
+      "dependencies": {
+        "restore-cursor": "^4.0.0"
+      },
+      "engines": {
+        "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/cli-spinners": {
+      "version": "2.9.2",
+      "resolved": "https://registry.npmmirror.com/cli-spinners/-/cli-spinners-2.9.2.tgz",
+      "integrity": "sha512-ywqV+5MmyL4E7ybXgKys4DugZbX0FC6LnwrhjuykIjnK9k8OQacQ7axGKnjDXWNhns0xot3bZI5h55H8yo9cJg==",
+      "dev": true,
+      "engines": {
+        "node": ">=6"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
     "node_modules/clone": {
       "version": "2.1.2",
       "resolved": "https://registry.npmmirror.com/clone/-/clone-2.1.2.tgz",
@@ -3618,6 +3646,18 @@
         "node": ">=6.9.0"
       }
     },
+    "node_modules/get-east-asian-width": {
+      "version": "1.2.0",
+      "resolved": "https://registry.npmmirror.com/get-east-asian-width/-/get-east-asian-width-1.2.0.tgz",
+      "integrity": "sha512-2nk+7SIVb14QrgXFHcm84tD4bKQz0RxPuMT8Ag5KPOq7J5fEmAg0UbXdTOSHqNuHSU28k55qnceesxXRZGzKWA==",
+      "dev": true,
+      "engines": {
+        "node": ">=18"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
     "node_modules/get-intrinsic": {
       "version": "1.2.4",
       "resolved": "https://registry.npmmirror.com/get-intrinsic/-/get-intrinsic-1.2.4.tgz",
@@ -4251,6 +4291,18 @@
         "url": "https://github.com/sponsors/sindresorhus"
       }
     },
+    "node_modules/is-interactive": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmmirror.com/is-interactive/-/is-interactive-2.0.0.tgz",
+      "integrity": "sha512-qP1vozQRI+BMOPcjFzrjXuQvdak2pHNUMZoeG2eRbiSqyvbEf/wQtEOTOX1guk6E3t36RkaqiSt8A/6YElNxLQ==",
+      "dev": true,
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
     "node_modules/is-negative-zero": {
       "version": "2.0.3",
       "resolved": "https://registry.npmmirror.com/is-negative-zero/-/is-negative-zero-2.0.3.tgz",
@@ -4395,6 +4447,18 @@
         "url": "https://github.com/sponsors/ljharb"
       }
     },
+    "node_modules/is-unicode-supported": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmmirror.com/is-unicode-supported/-/is-unicode-supported-2.0.0.tgz",
+      "integrity": "sha512-FRdAyx5lusK1iHG0TWpVtk9+1i+GjrzRffhDg4ovQ7mcidMQ6mj+MhKPmvh7Xwyv5gIS06ns49CA7Sqg7lC22Q==",
+      "dev": true,
+      "engines": {
+        "node": ">=18"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
     "node_modules/is-weakref": {
       "version": "1.0.2",
       "resolved": "https://registry.npmmirror.com/is-weakref/-/is-weakref-1.0.2.tgz",
@@ -4626,6 +4690,34 @@
       "resolved": "https://registry.npmmirror.com/lodash-es/-/lodash-es-4.17.21.tgz",
       "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw=="
     },
+    "node_modules/log-symbols": {
+      "version": "6.0.0",
+      "resolved": "https://registry.npmmirror.com/log-symbols/-/log-symbols-6.0.0.tgz",
+      "integrity": "sha512-i24m8rpwhmPIS4zscNzK6MSEhk0DUWa/8iYQWxhffV8jkI4Phvs3F+quL5xvS0gdQR0FyTCMMH33Y78dDTzzIw==",
+      "dev": true,
+      "dependencies": {
+        "chalk": "^5.3.0",
+        "is-unicode-supported": "^1.3.0"
+      },
+      "engines": {
+        "node": ">=18"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/log-symbols/node_modules/is-unicode-supported": {
+      "version": "1.3.0",
+      "resolved": "https://registry.npmmirror.com/is-unicode-supported/-/is-unicode-supported-1.3.0.tgz",
+      "integrity": "sha512-43r2mRvz+8JRIKnWJ+3j8JtjRKZ6GmjzfaE/qiBJnikNnYv/6bagRJ1kUhNk8R5EX/GkobD+r+sfxCPJsiKBLQ==",
+      "dev": true,
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
     "node_modules/lru-cache": {
       "version": "5.1.1",
       "resolved": "https://registry.npmmirror.com/lru-cache/-/lru-cache-5.1.1.tgz",
@@ -5375,6 +5467,79 @@
         "url": "https://github.com/sponsors/sindresorhus"
       }
     },
+    "node_modules/ora": {
+      "version": "8.0.1",
+      "resolved": "https://registry.npmmirror.com/ora/-/ora-8.0.1.tgz",
+      "integrity": "sha512-ANIvzobt1rls2BDny5fWZ3ZVKyD6nscLvfFRpQgfWsythlcsVUC9kL0zq6j2Z5z9wwp1kd7wpsD/T9qNPVLCaQ==",
+      "dev": true,
+      "dependencies": {
+        "chalk": "^5.3.0",
+        "cli-cursor": "^4.0.0",
+        "cli-spinners": "^2.9.2",
+        "is-interactive": "^2.0.0",
+        "is-unicode-supported": "^2.0.0",
+        "log-symbols": "^6.0.0",
+        "stdin-discarder": "^0.2.1",
+        "string-width": "^7.0.0",
+        "strip-ansi": "^7.1.0"
+      },
+      "engines": {
+        "node": ">=18"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/ora/node_modules/ansi-regex": {
+      "version": "6.0.1",
+      "resolved": "https://registry.npmmirror.com/ansi-regex/-/ansi-regex-6.0.1.tgz",
+      "integrity": "sha512-n5M855fKb2SsfMIiFFoVrABHJC8QtHwVx+mHWP3QcEqBHYienj5dHSgjbxtC0WEZXYt4wcD6zrQElDPhFuZgfA==",
+      "dev": true,
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://github.com/chalk/ansi-regex?sponsor=1"
+      }
+    },
+    "node_modules/ora/node_modules/emoji-regex": {
+      "version": "10.3.0",
+      "resolved": "https://registry.npmmirror.com/emoji-regex/-/emoji-regex-10.3.0.tgz",
+      "integrity": "sha512-QpLs9D9v9kArv4lfDEgg1X/gN5XLnf/A6l9cs8SPZLRZR3ZkY9+kwIQTxm+fsSej5UMYGE8fdoaZVIBlqG0XTw==",
+      "dev": true
+    },
+    "node_modules/ora/node_modules/string-width": {
+      "version": "7.2.0",
+      "resolved": "https://registry.npmmirror.com/string-width/-/string-width-7.2.0.tgz",
+      "integrity": "sha512-tsaTIkKW9b4N+AEj+SVA+WhJzV7/zMhcSu78mLKWSk7cXMOSHsBKFWUs0fWwq8QyK3MgJBQRX6Gbi4kYbdvGkQ==",
+      "dev": true,
+      "dependencies": {
+        "emoji-regex": "^10.3.0",
+        "get-east-asian-width": "^1.0.0",
+        "strip-ansi": "^7.1.0"
+      },
+      "engines": {
+        "node": ">=18"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/ora/node_modules/strip-ansi": {
+      "version": "7.1.0",
+      "resolved": "https://registry.npmmirror.com/strip-ansi/-/strip-ansi-7.1.0.tgz",
+      "integrity": "sha512-iq6eVVI64nQQTRYq2KtEg2d2uU7LElhTJwsH4YzIHZshxlgZms/wIc4VoDQTlG/IvVIrBKG06CrZnp0qv7hkcQ==",
+      "dev": true,
+      "dependencies": {
+        "ansi-regex": "^6.0.1"
+      },
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://github.com/chalk/strip-ansi?sponsor=1"
+      }
+    },
     "node_modules/pascalcase": {
       "version": "0.1.1",
       "resolved": "https://registry.npmmirror.com/pascalcase/-/pascalcase-0.1.1.tgz",
@@ -5991,6 +6156,52 @@
       "deprecated": "https://github.com/lydell/resolve-url#deprecated",
       "dev": true
     },
+    "node_modules/restore-cursor": {
+      "version": "4.0.0",
+      "resolved": "https://registry.npmmirror.com/restore-cursor/-/restore-cursor-4.0.0.tgz",
+      "integrity": "sha512-I9fPXU9geO9bHOt9pHHOhOkYerIMsmVaWB0rA2AI9ERh/+x/i7MV5HKBNrg+ljO5eoPVgCcnFuRjJ9uH6I/3eg==",
+      "dev": true,
+      "dependencies": {
+        "onetime": "^5.1.0",
+        "signal-exit": "^3.0.2"
+      },
+      "engines": {
+        "node": "^12.20.0 || ^14.13.1 || >=16.0.0"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/restore-cursor/node_modules/mimic-fn": {
+      "version": "2.1.0",
+      "resolved": "https://registry.npmmirror.com/mimic-fn/-/mimic-fn-2.1.0.tgz",
+      "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==",
+      "dev": true,
+      "engines": {
+        "node": ">=6"
+      }
+    },
+    "node_modules/restore-cursor/node_modules/onetime": {
+      "version": "5.1.2",
+      "resolved": "https://registry.npmmirror.com/onetime/-/onetime-5.1.2.tgz",
+      "integrity": "sha512-kbpaSSGJTWdAY5KPVeMOKXSrPtr8C8C7wodJbcsd51jRnmD+GZu8Y0VoU6Dm5Z4vWr0Ig/1NKuWRKf7j5aaYSg==",
+      "dev": true,
+      "dependencies": {
+        "mimic-fn": "^2.1.0"
+      },
+      "engines": {
+        "node": ">=6"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
+    "node_modules/restore-cursor/node_modules/signal-exit": {
+      "version": "3.0.7",
+      "resolved": "https://registry.npmmirror.com/signal-exit/-/signal-exit-3.0.7.tgz",
+      "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==",
+      "dev": true
+    },
     "node_modules/ret": {
       "version": "0.1.15",
       "resolved": "https://registry.npmmirror.com/ret/-/ret-0.1.15.tgz",
@@ -6657,6 +6868,18 @@
         "node": ">=0.10.0"
       }
     },
+    "node_modules/stdin-discarder": {
+      "version": "0.2.2",
+      "resolved": "https://registry.npmmirror.com/stdin-discarder/-/stdin-discarder-0.2.2.tgz",
+      "integrity": "sha512-UhDfHmA92YAlNnCfhmq0VeNL5bDbiZGg7sZ2IvPsXubGkiNa9EC+tUTsjBRsYUAz87btI6/1wf4XoVvQ3uRnmQ==",
+      "dev": true,
+      "engines": {
+        "node": ">=18"
+      },
+      "funding": {
+        "url": "https://github.com/sponsors/sindresorhus"
+      }
+    },
     "node_modules/strict-uri-encode": {
       "version": "1.1.0",
       "resolved": "https://registry.npmmirror.com/strict-uri-encode/-/strict-uri-encode-1.1.0.tgz",

+ 3 - 1
package.json

@@ -11,7 +11,8 @@
     "preview": "vite preview",
     "build-only": "vite build",
     "type-check": "vue-tsc --build --force",
-    "deploy": "node deploy/index.js --test"
+    "deploy:prod": "node deploy/index.js --prod",
+    "deploy:test": "node deploy/index.js --test"
   },
   "dependencies": {
     "@vueuse/core": "^10.9.0",
@@ -44,6 +45,7 @@
     "chalk": "^5.3.0",
     "dayjs": "^1.11.11",
     "npm-run-all2": "^6.1.2",
+    "ora": "^8.0.1",
     "postcss": "^8.4.38",
     "shelljs": "^0.8.5",
     "ssh2-sftp-client": "^10.0.3",

+ 3 - 1
src/views/answer/AnswerView.vue

@@ -79,7 +79,9 @@ const onRegenerate = async ({ question, realQuestion }) => {
       showVal: question,
       question: realQuestion || question,
       module: 0,
-      isStrong: Number(unref(switchActive))
+      isStrong: Number(unref(switchActive)),
+      topP: 0.9,
+      temperature: 0.7
     },
     signal: controller.signal,
     onDownloadProgress: ({ event }) => {