development.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. const shell = require('shelljs')
  2. const path = require('path');
  3. const config = {
  4. ip: "192.168.3.111", // ssh地址
  5. username: "root", // ssh 用户名
  6. port: 22, //端口
  7. password: "123456", // ssh 密码
  8. path: '/home/itsm/project/html/reqApp', // 上传地址,删除地址
  9. buildPath: '../dist' // 本地打包后文件地址
  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. if (shell.exec(`npm run build`).code == 0) {
  37. console.log("-----打包成功-----");
  38. //提交上传
  39. connectSSh();
  40. }
  41. }
  42. runTask()