forked from iceddev/your-first-npm-module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attempt.js
40 lines (35 loc) · 821 Bytes
/
attempt.js
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
var spawn = require('child_process').spawn;
var when = require('when');
function attempt(command){
if(!command){
return;
}
command = command.split(' ');
var cmd = command[0];
if(cmd !== 'npm'){
return;
}
var args = command.slice(1);
if(process.platform === 'win32'){
args.unshift('/c', cmd);
cmd = 'cmd';
}
var defer = when.defer();
var result = '';
var child = spawn(cmd, args, {
stdio: [process.stdin]
});
child.stdout.setEncoding('utf8');
child.stderr.setEncoding('utf8');
child.stdout
.on('data', function(data){
result += data.toString('utf-8');
})
.on('close', function(code){
defer.resolve(result);
});
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
return defer.promise;
}
module.exports = attempt;