production.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const shell = require('shelljs')
  2. const path = require('path');
  3. const config = {
  4. ip: "47.115.219.94", // ssh地址
  5. username: "root", // ssh 用户名
  6. port: 22, //端口
  7. password: "DStech@123", // ssh 密码
  8. // path: '/home/md2/newweb/pc', // 上传地址,删除地址(第一次部署)
  9. path: '/home/itsm/project/html/pc/assets', // 上传地址,删除地址(bower_components太大,第二次以后的更新只更新assets,具体的灵活应变)
  10. // buildPath: '../dist' // 本地打包后文件地址(第一次部署)
  11. buildPath: '../dist/assets' // 本地打包后文件地址(bower_components太大,第二次以后的更新只更新assets,具体的灵活应变)
  12. }
  13. let Client = require('ssh2-sftp-client');
  14. function connectSSh() {
  15. let sftp = new Client();
  16. sftp.connect({
  17. host: config.ip,
  18. port: config.port,
  19. username: config.username,
  20. password: config.password
  21. }).then(() => {
  22. console.log("-----先执行删除服务器文件-----")
  23. return sftp.rmdir(config.path, true);
  24. }).then(() => {
  25. // 上传文件
  26. console.log("-----开始上传-----")
  27. return sftp.uploadDir(path.resolve(__dirname, config.buildPath), config.path);
  28. }).then((data) => {
  29. console.log("-----上传完成-----");
  30. sftp.end();
  31. }).catch((err) => {
  32. console.log(err, '-----失败-----');
  33. sftp.end();
  34. });
  35. }
  36. function runTask() {
  37. //打包完成
  38. if (shell.exec(`npm run build`).code == 0) {
  39. console.log("-----打包成功-----");
  40. //提交上传
  41. connectSSh();
  42. }
  43. }
  44. runTask()