index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Client from 'ssh2-sftp-client';
  2. import ssh2 from 'ssh2';
  3. import chalk from 'chalk';
  4. import ora from 'ora';
  5. import shell from 'shelljs';
  6. import path from 'path';
  7. import { fileURLToPath } from 'url';
  8. import { config } from './config.js';
  9. const rawArgv = process.argv.slice(2);
  10. const filterStage = rawArgv.includes("--prod") ? "prod" : "test";
  11. const __dirname = path.dirname(fileURLToPath(import.meta.url));
  12. // 构建
  13. const compileDist = async () => {
  14. console.log(chalk.blue("项目开始构建"));
  15. if (shell.exec(`npm run build:${filterStage}`).code === 0) {
  16. console.log(chalk.blue("项目构建成功"));
  17. }
  18. }
  19. // 获取配置
  20. const getConfig = () => {
  21. const [result] = config.filter(({ nodeEnv }) => nodeEnv === filterStage);
  22. return result;
  23. }
  24. const execRemoteCommand = async () => {
  25. const config = getConfig();
  26. const conn = new ssh2.Client();
  27. conn.on("ready", function () {
  28. console.log(chalk.blue("ssh2 链接成功 - 执行 pm2 restart bigModelAdmin"));
  29. conn.exec("pm2 restart bigModelAdmin", function (err, stream) {
  30. if (err) throw err;
  31. console.log(
  32. chalk.blue(``) + chalk.green(`---执行成功---`),
  33. );
  34. console.log(
  35. chalk.blue(`✅`) + chalk.green(`---部署完成---`),
  36. );
  37. conn.end();
  38. });
  39. }).connect({ ...config });
  40. }
  41. // 部署
  42. const connectShell = async () => {
  43. const sftp = new Client();
  44. const item = getConfig();
  45. let spinner = null;
  46. // 打印信息
  47. const printMsg = ({ color, text }) => {
  48. console.log(chalk.red(`${item.host} --> `) + chalk[color](text));
  49. }
  50. printMsg({ color: 'green', text: '服务器连接中' });
  51. sftp.connect({
  52. host: item.host,
  53. port: item.port,
  54. username: item.username,
  55. password: item.password,
  56. })
  57. .then(() => {
  58. printMsg({ color: 'green', text: '服务器连接成功' });
  59. printMsg({ color: 'yellow', text: '执行删除文件中' });
  60. return sftp.rmdir(item.path, true);
  61. })
  62. .then(() => {
  63. printMsg({ color: 'green', text: '执行删除文件成功' });
  64. printMsg({ color: 'green', text: '即将开始上传' });
  65. spinner = ora().start();
  66. spinner.text = '文件上传中,请等待'
  67. return sftp.uploadDir(path.resolve(__dirname, "../dist"), item.path);
  68. })
  69. .then(() => {
  70. spinner.info('文件上传结束')
  71. spinner.stop();
  72. if (filterStage === 'prod') {
  73. execRemoteCommand()
  74. }
  75. printMsg({ color: 'green', text: '上传完成,部署成功' });
  76. sftp.end();
  77. })
  78. .catch((err) => {
  79. console.error(
  80. err,
  81. chalk.red(`${item.host} -->`) + chalk.red(`上传失败`)
  82. );
  83. sftp.end();
  84. });
  85. }
  86. async function runStart() {
  87. await compileDist();
  88. await connectShell();
  89. }
  90. runStart();