index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 链接成功"));
  29. const commands = [
  30. 'cd /data/node_web/modelAdmin',
  31. 'pwd',
  32. 'pm2 list',
  33. 'pm2 del bigModelAdmin',
  34. 'pm2 start'
  35. ];
  36. conn.exec(commands.join(";"), { pty: true }, function (err, stream) {
  37. if (err) throw err;
  38. stream
  39. .on('close', function (code, signal) {
  40. console.log('Command 执行成功');
  41. conn.end(); // 结束SSH连接
  42. })
  43. .on('data', function (data) {
  44. process.stdout.write(data);
  45. });
  46. stream.stdin.end();
  47. });
  48. }).connect({ ...config });
  49. }
  50. // 部署
  51. const connectShell = async () => {
  52. const sftp = new Client();
  53. const item = getConfig();
  54. let spinner = null;
  55. // 打印信息
  56. const printMsg = ({ color, text }) => {
  57. console.log(chalk.red(`${item.host} --> `) + chalk[color](text));
  58. }
  59. printMsg({ color: 'green', text: '服务器连接中' });
  60. sftp.connect({
  61. host: item.host,
  62. port: item.port,
  63. username: item.username,
  64. password: item.password,
  65. })
  66. .then(() => {
  67. printMsg({ color: 'green', text: '服务器连接成功' });
  68. printMsg({ color: 'yellow', text: '执行删除文件中' });
  69. return sftp.rmdir(item.path, true);
  70. })
  71. .then(() => {
  72. printMsg({ color: 'green', text: '执行删除文件成功' });
  73. printMsg({ color: 'green', text: '即将开始上传' });
  74. spinner = ora().start();
  75. spinner.text = '文件上传中,请等待'
  76. return sftp.uploadDir(path.resolve(__dirname, "../dist"), item.path);
  77. })
  78. .then(() => {
  79. spinner.info('文件上传结束')
  80. spinner.stop();
  81. // if (filterStage === 'prod') {
  82. execRemoteCommand()
  83. // }
  84. printMsg({ color: 'green', text: '上传完成,准备指令中' });
  85. sftp.end();
  86. })
  87. .catch((err) => {
  88. console.error(
  89. err,
  90. chalk.red(`${item.host} -->`) + chalk.red(`上传失败`)
  91. );
  92. sftp.end();
  93. });
  94. }
  95. async function runStart() {
  96. await compileDist();
  97. await connectShell();
  98. }
  99. runStart();