const shell = require('shelljs') const path = require('path'); const config = { ip: "192.168.3.111", // ssh地址 username: "root", // ssh 用户名 port: 22, //端口 password: "123456", // ssh 密码 path: '/home/itsm/project/html/reqApp', // 上传地址,删除地址 buildPath: '../dist' // 本地打包后文件地址 } let Client = require('ssh2-sftp-client'); function connectSSh() { let sftp = new Client(); sftp.connect({ host: config.ip, port: config.port, username: config.username, password: config.password }).then(() => { console.log("-----先执行删除服务器文件-----") return sftp.rmdir(config.path, true); }).then(() => { // 上传文件 console.log("-----开始上传-----") return sftp.uploadDir(path.resolve(__dirname, config.buildPath), config.path); }).then((data) => { console.log("-----上传完成-----"); sftp.end(); }).catch((err) => { console.log(err, '-----失败-----'); sftp.end(); }); } function runTask() { //打包完成 if (shell.exec(`npm run build`).code == 0) { console.log("-----打包成功-----"); //提交上传 connectSSh(); } } runTask()