const shell = require('shelljs')
const path = require('path');
const config = {
  ip: "118.190.89.49", // ssh地址
  username: "root", // ssh 用户名
  port: 22,      //端口
  password: "dstech@123", // ssh 密码
  path: '/home/itsm_v2_/web/app', // 上传地址,删除地址
  buildPath: '../unpackage/dist/build/h5' // 本地打包后文件地址
}
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();
  // }
  connectSSh();
}
runTask()