development.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const shell = require('shelljs')
  2. const path = require('path');
  3. const config = {
  4. ip: "192.168.3.108", // ssh地址
  5. username: "root", // ssh 用户名
  6. port: 22, //端口
  7. password: "123456", // ssh 密码
  8. path: '/home/itsm/project/spreadSheet', // 上传地址,删除地址
  9. buildPath: '../dist/spreadSheet' // 本地打包后文件地址
  10. }
  11. let Client = require('ssh2-sftp-client');
  12. function connectSSh() {
  13. let sftp = new Client();
  14. sftp.connect({
  15. host: config.ip,
  16. port: config.port,
  17. username: config.username,
  18. password: config.password
  19. }).then(() => {
  20. console.log("-----先执行删除服务器文件-----")
  21. return sftp.rmdir(config.path, true);
  22. }).then(() => {
  23. // 上传文件
  24. console.log("-----开始上传-----")
  25. return sftp.uploadDir(path.resolve(__dirname, config.buildPath), config.path);
  26. }).then((data) => {
  27. console.log("-----上传完成-----");
  28. sftp.end();
  29. }).catch((err) => {
  30. console.log(err, '-----失败-----');
  31. sftp.end();
  32. });
  33. }
  34. function runTask() {
  35. //打包完成
  36. console.log(`当前NodeJs版本是${process.version}`);
  37. const isNodeVersionLt17 = parseInt(process.version.slice(1)) < 17;
  38. const execStr = isNodeVersionLt17 ? 'npm run build' : 'npm run build';
  39. if (shell.exec(execStr).code == 0) {
  40. // if (shell.exec(`find dist -name '*.js' -print0 | xargs -0 gzip -k`).code == 0) {
  41. // if (shell.exec(`find dist -name '*.css' -print0 | xargs -0 gzip -k`).code == 0) {
  42. console.log("-----打包成功-----");
  43. //提交上传
  44. connectSSh();
  45. // }
  46. // }
  47. }
  48. }
  49. runTask()