/*
 * @Author: zhangyu
 * @Date: 2019-12-18 16:18:30
 * @Info: 自动化部署
 * @LastEditTime : 2020-02-11 17:47:56
 */

const Client = require("node-ssh");
const ssh = new Client(); 

ssh.connect({
  host: process.env.npm_package_remote_host,
  port: "22",
  // privateKey: `${process.env.HOME}\\.ssh\\id_rsa_2048`,
  username: process.env.npm_package_remote_user,
  password: process.env.npm_package_remote_password
}).then(() => {
  const failedList = [];
  ssh.putDirectory(
    process.env.npm_package_remote_local,
    process.env.npm_package_remote_path,
    {
      recursive: true,
      concurrency: 1,
      tick: function(localPath, remotePath, error) {
        if (error) {
          failedList.push(localPath);
        }
      }
    }
  ).then(status => {
    if (failedList.length > 0) {
      console.log("发布失败");
      console.log("failed transfers", failedList.join(", "));
    } else {
      console.log(status ? "发布成功" : "发布失败");
    }
    ssh.dispose();
  });
});
