c4se記:さっちゃんですよ☆

.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programming に分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡)

音樂は SoundCloud に公開中です。

考察は現在は主に Scrapbox で公表中です。

Programming は GitHub で開發中です。

更新したgulp-sshが余計な機能をつけた挙句動かないので、SSHでコマンドを打つのを自作した

更新したgulp-sshが余計な機能をつけた挙句動かないので、SSHでコマンドを打つのを自作した。gulp-sshは中でssh2を叩いてゐる。それを直接使ふ。gulp-sshは捨てた。

/* global -Promise */
/* jshint node:true */
'use strict';
var Promise = require('bluebird'),
    Ssh     = require('ssh2');

function promisefy(value) {
  return new Promise(function (resolve) { resolve(value); });
}

function PromiseSsh(config) {
  this.connection = new Ssh();
  this.config = config;
}

PromiseSsh.prototype.ready = function () {
  var me = this;

  return new Promise(function (resolve) {
    me.connection.on('ready', resolve).connect(me.config);
  });
};

PromiseSsh.prototype.end = function () {
  this.connection.end();
};

PromiseSsh.prototype.execOne = function (command) {
  var me = this;

  return new Promise(function (resolve, reject) {
    console.log('START:: ' + command);
    me.connection.exec(command, function (err, stream) {
      if (err) {
        console.error(err);
        return reject(err);
      }
      stream.on('exit', function (code, signal) {
        console.log('EXIT:: code: ' + code + ' signal: ' + signal);
      }).on('close', function () {
        console.log('CLOSE::');
        resolve();
      }).on('data', function (data) {
        console.log(data.toString());
      }).stderr.on('data', function (data) {
        console.error(data.toString());
      });
    });
  });
};

PromiseSsh.prototype.exec = function (commands) {
  var me = this;

  return commands.
    reduce(function (promise, command) {
      return promise.then(function () { return me.execOne(command); });
    }, promisefy(void 0));
};

function promiseSsh(config, commands) {
  var ssh = new PromiseSsh(config);

  return ssh.ready().
    then(function () { return ssh.exec(commands); }).
    then(function () { ssh.end(); }).
    catch(function (err) {
      console.error(err);
      ssh.end();
    });
}

module.exports = promiseSsh;

このやうに使ふ。

var promiseSsh = require('./lib/promiseSsh');

gulp.task('deploy', function () {
  var sshConfig = {
        host:     ホスト,
        port:     '22',
        username: ユーザー名,
        password: process.env.SSH_PASSWORD,
      },
      commands = [
        コマンド,
        コマンド,
      ];

  return promiseSsh(sshConfig, commands);
});

単機能のほうがいい。

ちなみにChildProcessもPromiseに変換してる。

var cp      = require('child_process'),
    Promise = require('bluebird');

/**
 * @param {string} cmd
 * @param {boolean=} doseIgnoreError
 * @return {Promise.<string>} stdout
 */
function promiseProcess(cmd, doseIgnoreError) {
  doseIgnoreError = !!doseIgnoreError;
  return new Promise(function (resolve, reject) {
    cp.exec(cmd, function (err, stdout, stderr) {
      if (err && !doseIgnoreError) {
        console.log(stdout);
        console.error(stderr);
        return reject(err);
      }
      resolve(stdout);
    });
  });
}