1 | var spawn = require('child_process').spawn;
|
2 |
|
3 | var when = require('when');
|
4 |
|
5 | function attempt(command){
|
6 | if(!command){
|
7 | return;
|
8 | }
|
9 | command = command.split(' ');
|
10 | var cmd = command[0];
|
11 | var args = command.slice(1);
|
12 | if(process.platform === 'win32'){
|
13 | args.unshift('/c', cmd);
|
14 | cmd = 'cmd';
|
15 | }
|
16 |
|
17 | if(cmd !== 'npm'){
|
18 | return;
|
19 | }
|
20 |
|
21 | var defer = when.defer();
|
22 | var result = '';
|
23 | var child = spawn(cmd, args, {
|
24 | stdio: [process.stdin]
|
25 | });
|
26 | child.stdout.setEncoding('utf8');
|
27 | child.stderr.setEncoding('utf8');
|
28 | child.stdout
|
29 | .on('data', function(data){
|
30 | result += data.toString('utf-8');
|
31 | })
|
32 | .on('close', function(code){
|
33 | defer.resolve(result);
|
34 | });
|
35 | child.stdout.pipe(process.stdout);
|
36 | child.stderr.pipe(process.stderr);
|
37 | return defer.promise;
|
38 | }
|
39 |
|
40 | module.exports = attempt;
|