Skip to content

Commit

Permalink
add ssh shell
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Nov 22, 2014
1 parent 42261f8 commit 0de8daa
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,4 +2,5 @@
*.log
node_modules
logs
debug
*.node
52 changes: 47 additions & 5 deletions index.js
Expand Up @@ -63,9 +63,8 @@ function flushReady(ctx) {
}

GulpSSH.prototype.connect = function (options) {
var ctx = this;
if (options) this.options.sshConfig = options;
if (!this._connect && !ctx._connected) {
if (!this._connect && !this._connected) {
this._connect = true;
this.ssh2.connect(this.options.sshConfig);
}
Expand All @@ -82,12 +81,12 @@ GulpSSH.prototype.exec = function (commands, options) {

if (!commands) throw new gutil.PluginError(packageName, '`commands` required.');

commands = Array.isArray(commands) ? commands : [commands];
commands = Array.isArray(commands) ? commands.slice() : [commands];

var file = new gutil.File({
cwd: __dirname,
base: __dirname,
path: path.join(__dirname, options.filePath || 'commands.log'),
path: path.join(__dirname, options.filePath || 'gulp-ssh.exec.log'),
contents: through.obj()
});

Expand Down Expand Up @@ -126,7 +125,6 @@ GulpSSH.prototype.exec = function (commands, options) {
}

return outStream;

};

GulpSSH.prototype.sftp = function (command, filePath, options) {
Expand Down Expand Up @@ -175,6 +173,50 @@ GulpSSH.prototype.sftp = function (command, filePath, options) {

};

GulpSSH.prototype.shell = function (commands, options) {
var ctx = this, outStream = through.obj(), ssh = this.ssh2;

if (!commands) throw new gutil.PluginError(packageName, '`commands` required.');

commands = Array.isArray(commands) ? commands.slice() : [commands];

var file = new gutil.File({
cwd: __dirname,
base: __dirname,
path: path.join(__dirname, options.filePath || 'gulp-ssh.shell.log'),
contents: through.obj()
});

outStream.push(file);
this.connect().ready(shellCommand);

function endStream() {
file.contents.end();
outStream.end();
}

function shellCommand() {
if (commands.length === 0) return endStream();
ssh.shell(function (err, stream) {
if (err) return outStream.emit('error', new gutil.PluginError(packageName, err));

stream
.on('end', endStream)
.on('close', endStream)
.stderr.on('data', function (data) {
outStream.emit('error', new gutil.PluginError(packageName, data + ''));
});

stream.pipe(file.contents, {end: true});
commands = (commands.join('\n') + '\n').replace(/\n+/g, '\n').toLowerCase();
if (commands.slice(-6) !== '\nexit\n') commands += 'exit\n';
stream.end(commands);
});
}

return outStream;
};

// compatible with 0.1.x
GulpSSH.exec = function (options) {
if (typeof options.sshConfig !== 'object') {
Expand Down
3 changes: 2 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "gulp-ssh",
"version": "0.2.2",
"version": "0.3.0",
"description": "SSH and SFTP tasks for gulp",
"license": "MIT",
"author": {
Expand All @@ -23,6 +23,7 @@
"ssh2",
"sftp",
"exec",
"shell",
"remote",
"client",
"upload"
Expand Down
27 changes: 25 additions & 2 deletions readme.md
@@ -1,4 +1,4 @@
# gulp-ssh v0.2.2
# gulp-ssh v0.3.0

> SSH and SFTP tasks for gulp
Expand Down Expand Up @@ -44,6 +44,13 @@ gulp.task('sftp-write', function () {
return gulp.src('index.js')
.pipe(gulpSSH.sftp('write', 'test.js'));
});

// execute commands in shell
gulp.task('shell', function () {
return gulpSSH
.shell(['cd /home/thunks', 'git pull', 'npm install', 'npm update', 'npm test'], {filePath: 'shell.log'})
.pipe(gulp.dest('logs'));
});
```

## API
Expand Down Expand Up @@ -87,6 +94,22 @@ Ignore errors when executing commands.. **Default:** (false)

return `gulpSSH`

### gulpSSH.shell(commands, options)

return `stream`

#### commands

*Required*
Type: `String` or `Array`

#### options.filePath

*Option*
Type: `String`

file path to write on local. **Default:** ('gulp-ssh.shell.log')

### gulpSSH.exec(commands, options)

return `stream`
Expand All @@ -101,7 +124,7 @@ Type: `String` or `Array`
*Option*
Type: `String`

file path to write on local. **Default:** ('commands.log')
file path to write on local. **Default:** ('gulp-ssh.exec.log')


### gulpSSH.sftp(command, filePath, options)
Expand Down
8 changes: 7 additions & 1 deletion test/index.js
Expand Up @@ -47,6 +47,12 @@ module.exports = function () {
.pipe(gulpSSH.sftp('write', 'test.js'));
});

gulp.task('test', gulpSequence('exec', 'sftp-read', 'sftp-write'));
gulp.task('shell', function () {
return gulpSSH
.shell(['cd /home/thunks', 'git pull', 'npm install', 'npm update', 'npm test'], {filePath: 'shell.log'})
.pipe(gulp.dest('logs'));
});

gulp.task('test', gulpSequence('exec', 'sftp-read', 'sftp-write', 'shell'));

};

0 comments on commit 0de8daa

Please sign in to comment.