forked from maksrom/javascript-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshExec.js
More file actions
51 lines (38 loc) · 1.18 KB
/
sshExec.js
File metadata and controls
51 lines (38 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
var gutil = require('gulp-util');
/**
* Executes ssh command
* NB: DOES NOT KEEP STATE BETWEEN COMMANDS
* WILL NOT WORK: `cd` ... `do smth`
* @param cmd command string to execute
* @param client ssh2 client (must be connected)
* @returns {Promise} rejects if code!=0
*/
module.exports = function*(client, cmd, options) {
options = options || {};
// certain commands like `git clone` require pty
if (!("pty" in options)) options.pty = true;
var stream = yield function(callback) {
gutil.log('sshExec', cmd);
client.exec(cmd, options, callback);
};
return yield new Promise(function(resolve, reject) {
var output = '';
stream.on('close', function(code, signal) {
if (code) {
var error = new Error(`SSH command exited, ${signal ? 'signal:' + signal : ''} code:${code}`);
error.signal = signal;
error.code = code;
reject(error);
}
else resolve(output);
});
stream.on('data', function(data) {
output += data.toString();
console.log(data.toString());
});
stream.stderr.on('data', function(data) {
output += data.toString();
console.error(data.toString());
});
});
};