/**
 * Connect by SFTP and deploy static files
 *
 * @param {Object} config username, password, host, port for SFTP connection
 */
function doDeploy(config) {
  const sftp = new Client();
  sftp.connect(config).then(() => {
    if (args['test-connection'] || config.testConnection) {
      console.log(`Connected to ${config.host}; exiting`);
      process.exit(0);
    }
    console.log('Removing existing JS dir');
    return sftp.rmdir(remoteJSPath, true);
  }).then(() => {
    console.log('Creating empty JS dir');
    return sftp.mkdir(remoteJSPath);
  }).then(() => {
    const localFiles = fs.readdirSync(path.join(__dirname, 'static'))
    localFiles.forEach((filename) => {
      console.log(`Uploading ${filename}`);
      sftp.put(
        path.join(__dirname, 'static', filename),
        path.join(remoteJSPath, filename)
      ).then(() => {
        return sftp.list(remoteJSPath);
      }).then((list) => {
        // Exit when all files are uploaded
        if (list.length === localFiles.length) {
          console.log('Uploaded files:');
          console.log(list.map(({ name }) => name));
          process.exit(0);
        }
      });
    });
  }).catch((err) => {
      console.log(err);
      process.exit(1);
  });
}
Ejemplo n.º 2
0
const put = (localPath, remotePath) => {
  const sftp = new Client()
  sftp.connect({
    host: '10.0.201.210',
    port: '932',
    username: '******',
    password: '******',
  }).then(() => {
    // return sftp.list('/www/web/')
    return sftp.put(localPath, remotePath);
  }).then((data) => {
    // console.log(data)
    console.log(`>>>>>> Upload finished <<<<<<\n`)
  }).catch((err) => {
    console.log(err, 'catch err.')
  })
}