Comparing version 0.5.6 to 0.6.0
141
index.js
@@ -6,3 +6,2 @@ 'use strict' | ||
* | ||
* Copyright (c) 2014 Yan Qing | ||
* Licensed under the MIT license. | ||
@@ -22,78 +21,54 @@ */ | ||
Client.prototype.gulpId = null | ||
Client.prototype.gulpQueue = null | ||
Client.prototype.gulpConnected = false | ||
Client.prototype.gulpFlushReady = function () { | ||
this.gulpConnected = true | ||
while (this.gulpQueue.length) this.gulpQueue.shift().call(this) | ||
} | ||
Client.prototype.gulpReady = function (cb) { | ||
if (this.gulpConnected) cb.call(this) | ||
else this.gulpQueue.push(cb) | ||
} | ||
function GulpSSH (options) { | ||
if (!(this instanceof GulpSSH)) return new GulpSSH(options) | ||
if (!options || !options.sshConfig) throw new Error('options.sshConfig required!') | ||
this.options = options | ||
this.connections = Object.create(null) | ||
EventEmitter.call(this) | ||
} | ||
util.inherits(GulpSSH, EventEmitter) | ||
var gulpId = 0 | ||
GulpSSH.prototype.getClient = function () { | ||
var ctx = this | ||
var ssh = new Client() | ||
ssh.gulpId = gulpId++ | ||
ssh.gulpQueue = [] | ||
ssh.gulpConnected = false | ||
this.connections[ssh.gulpId] = ssh | ||
this.options = options || {} | ||
this._connecting = false | ||
this._connected = false | ||
this._ended = false | ||
this._readyEvents = [] | ||
this.ssh2 = new Client() | ||
this.ssh2 | ||
.on('connect', function () { | ||
gutil.log(packageName + ' :: Connect...') | ||
ctx.emit('connect') | ||
}) | ||
.on('ready', function () { | ||
gutil.log(packageName + ' :: Ready') | ||
ctx._connecting = false | ||
ctx._connected = true | ||
flushReady(ctx) | ||
ctx.emit('ready') | ||
}) | ||
ssh | ||
.on('error', function (err) { | ||
gutil.colors.red(new gutil.PluginError(packageName, err)) | ||
ctx.emit('error', err) | ||
ctx.emit('error', new gutil.PluginError(packageName, err)) | ||
}) | ||
.on('end', function () { | ||
gutil.log(packageName + ' :: End') | ||
ctx._connecting = false | ||
ctx._connected = false | ||
ctx.emit('end') | ||
delete ctx.connections[this.gulpId] | ||
}) | ||
.on('close', function (hadError) { | ||
gutil.log(packageName + ' :: Close') | ||
ctx._connecting = false | ||
ctx._connected = false | ||
ctx.emit('close', hadError) | ||
.on('close', function () { | ||
delete ctx.connections[this.gulpId] | ||
}) | ||
EventEmitter.call(this) | ||
var gulp = this.options.gulp || require('gulp') | ||
gulp.once('stop', function () { | ||
ctx.close() | ||
}) | ||
.on('ready', ssh.gulpFlushReady) | ||
.connect(this.options.sshConfig) | ||
return ssh | ||
} | ||
util.inherits(GulpSSH, EventEmitter) | ||
function flushReady (ctx) { | ||
if (!ctx._connected) return | ||
while (ctx._readyEvents.length) ctx._readyEvents.shift().call(ctx) | ||
} | ||
GulpSSH.prototype.connect = function (options) { | ||
if (options) this.options.sshConfig = options | ||
if (!this._connecting && !this._connected) { | ||
this._connecting = true | ||
this.ssh2.connect(this.options.sshConfig) | ||
} | ||
return this | ||
} | ||
GulpSSH.prototype.ready = function (fn) { | ||
this._readyEvents.push(fn) | ||
flushReady(this) | ||
} | ||
GulpSSH.prototype.close = function () { | ||
if (this.ended) return | ||
this._connecting = false | ||
this._connected = false | ||
this._ended = true | ||
this._readyEvents.length = 0 | ||
this.ssh2.end() | ||
var connections = this.connections | ||
Object.keys(connections).forEach(function (id) { | ||
connections[id].end() | ||
delete connections[id] | ||
}) | ||
} | ||
@@ -103,3 +78,3 @@ | ||
var ctx = this | ||
var ssh = this.ssh2 | ||
var ssh = this.getClient() | ||
var chunkSize = 0 | ||
@@ -114,3 +89,3 @@ var chunks = [] | ||
this.connect().ready(execCommand) | ||
ssh.gulpReady(execCommand) | ||
@@ -124,2 +99,4 @@ function endStream () { | ||
})) | ||
ssh.end() | ||
outStream.end() | ||
@@ -159,5 +136,4 @@ } | ||
GulpSSH.prototype.sftp = function (command, filePath, options) { | ||
var ctx = this | ||
var ssh = this.ssh2 | ||
var outStream | ||
var ssh = this.getClient() | ||
options = options || {} | ||
@@ -167,7 +143,5 @@ if (!command) throw new gutil.PluginError(packageName, '`command` required.') | ||
this.connect() | ||
if (command === 'write') { | ||
outStream = through.obj(function (file, encoding, callback) { | ||
ctx.ready(function () { | ||
ssh.gulpReady(function () { | ||
ssh.sftp(function (err, sftp) { | ||
@@ -189,3 +163,4 @@ if (err) return callback(new gutil.PluginError(packageName, err)) | ||
if (file.isStream()) file.pipe(write) | ||
else if (file.isBuffer()) write.end(file.contents); else { | ||
else if (file.isBuffer()) write.end(file.contents) | ||
else { | ||
err = new gutil.PluginError(packageName, 'file error!') | ||
@@ -196,2 +171,5 @@ write.end() | ||
}) | ||
}, function (callback) { | ||
ssh.end() | ||
callback() | ||
}) | ||
@@ -203,3 +181,3 @@ } else if (command === 'read') { | ||
outStream = through.obj() | ||
ctx.ready(function () { | ||
ssh.gulpReady(function () { | ||
ssh.sftp(function (err, sftp) { | ||
@@ -225,3 +203,2 @@ if (err) return outStream.emit('error', new gutil.PluginError(packageName, err)) | ||
})) | ||
outStream.end() | ||
this.close() | ||
@@ -231,2 +208,4 @@ }) | ||
sftp.end() | ||
ssh.end() | ||
outStream.end() | ||
}) | ||
@@ -245,5 +224,4 @@ }) | ||
var ctx = this | ||
var ssh = this.ssh2 | ||
var sftpClient = null | ||
var ssh = this.getClient() | ||
options = options || {} | ||
@@ -254,3 +232,3 @@ options.autoClose = false | ||
if (sftpClient) return callback(null, sftpClient) | ||
ctx.connect().ready(function () { | ||
ssh.gulpReady(function () { | ||
ssh.sftp(function (err, sftp) { | ||
@@ -269,2 +247,3 @@ if (err) return callback(err) | ||
} | ||
ssh.end() | ||
if (err) err = new gutil.PluginError(packageName, err) | ||
@@ -306,5 +285,5 @@ callback(err) | ||
GulpSSH.prototype.shell = function (commands, options) { | ||
var ssh = this.ssh2 | ||
var chunkSize = 0 | ||
var chunks = [] | ||
var ssh = this.getClient() | ||
var outStream = through.obj() | ||
@@ -324,6 +303,8 @@ | ||
})) | ||
ssh.end() | ||
outStream.end() | ||
} | ||
this.connect().ready(function () { | ||
ssh.gulpReady(function () { | ||
if (commands.length === 0) return endStream() | ||
@@ -330,0 +311,0 @@ ssh.shell(function (err, stream) { |
{ | ||
"name": "gulp-ssh", | ||
"version": "0.5.6", | ||
"version": "0.6.0", | ||
"description": "SSH and SFTP tasks for gulp", | ||
@@ -39,3 +39,3 @@ "license": "MIT", | ||
"gulp-sequence": "^0.4.5", | ||
"standard": "^6.0.8" | ||
"standard": "^7.1.2" | ||
}, | ||
@@ -42,0 +42,0 @@ "scripts": { |
@@ -6,5 +6,4 @@ gulp-ssh | ||
[![NPM version][npm-image]][npm-url] | ||
[![Build Status][travis-image]][travis-url] | ||
[![Downloads][downloads-image]][downloads-url] | ||
## Install | ||
@@ -30,7 +29,6 @@ | ||
port: 22, | ||
username: 'iojs', | ||
username: 'node', | ||
privateKey: fs.readFileSync('/Users/zensh/.ssh/id_rsa') | ||
} | ||
var gulpSSH = new GulpSSH({ | ||
@@ -68,3 +66,2 @@ ignoreErrors: false, | ||
}) | ||
``` | ||
@@ -99,4 +96,2 @@ | ||
* **gulp** - `gulp` instance. | ||
More [SSH Connection methods](https://github.com/mscdex/ssh2#connection-methods) | ||
@@ -178,9 +173,5 @@ | ||
### gulpSSH.close() | ||
Close the ssh connection. | ||
## License | ||
MIT © [Teambition](http://teambition.com) | ||
MIT © [Teambition](https://www.teambition.com) | ||
@@ -190,3 +181,3 @@ [npm-url]: https://npmjs.org/package/gulp-ssh | ||
[travis-url]: https://travis-ci.org/teambition/gulp-ssh | ||
[travis-image]: http://img.shields.io/travis/teambition/gulp-ssh.svg | ||
[downloads-url]: https://npmjs.org/package/gulp-ssh | ||
[downloads-image]: http://img.shields.io/npm/dm/gulp-ssh.svg?style=flat-square |
@@ -37,8 +37,8 @@ 'use strict' | ||
return gulp | ||
.src(['*.pdf']) | ||
.pipe(gulpSSH.dest('/home/iojs/test/pdf/')) | ||
.src('node_modules/ssh2/**/*') | ||
.pipe(gulpSSH.dest('/home/iojs/test/ssh2/')) | ||
}) | ||
gulp.task('sftp-read', function () { | ||
return gulpSSH.sftp('read', '/home/iojs/test/gulp-ssh/index.js', {filePath: 'test.js'}) | ||
return gulpSSH.sftp('read', '/home/iojs/test/ssh2/package.json', {filePath: 'ssh2-package.json'}) | ||
.pipe(gulp.dest('logs')) | ||
@@ -48,4 +48,4 @@ }) | ||
gulp.task('sftp-write', function () { | ||
return gulp.src('index.js') | ||
.pipe(gulpSSH.sftp('write', '/home/iojs/test/gulp-ssh/test.js')) | ||
return gulp.src('test/index.js') | ||
.pipe(gulpSSH.sftp('write', '/home/iojs/test/gulp-ssh-test.js')) | ||
}) | ||
@@ -55,3 +55,10 @@ | ||
return gulpSSH | ||
.shell(['cd /home/iojs/test/thunks', 'git pull', 'npm install', 'npm update', 'npm test'], {filePath: 'shell.log'}) | ||
.shell([ | ||
'cd /home/iojs/test', | ||
'rm -rf thunks', | ||
'git clone https://github.com/thunks/thunks.git', | ||
'cd thunks', | ||
'npm install', | ||
'npm test' | ||
], {filePath: 'shell.log'}) | ||
.pipe(gulp.dest('logs')) | ||
@@ -58,0 +65,0 @@ }) |
Sorry, the diff of this file is not supported yet
18855
376
178