/
spawn.js
executable file
·52 lines (43 loc) · 1.22 KB
/
spawn.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
41
42
43
44
45
46
47
48
49
50
51
52
/*!
* node.io
* Copyright(c) 2010 Chris O'Hara <cohara87@gmail.com>
* MIT Licensed
*/
var child = require('child_process'),
Job = require('./job').JobProto;
Job.prototype.spawn = function(args, stdin, callback) {
if (typeof stdin === 'function') {
callback = stdin;
stdin = undefined;
}
args = args.split(' ');
var cmd = args.shift(),
stdout = '', stderr = '',
called = false,
proc = child.spawn(cmd, args, {cwd: process.cwd()});
proc.stdout.on('data', function(data) {
stdout += data;
});
proc.stderr.on('data', function(data) {
if (/^execvp\(\)/.test(data.asciiSlice(0,data.length))) {
callback('Failed to start child process.');
called = true;
//proc.kill();
} else {
stderr += data;
}
});
proc.on('exit', function() {
if (!called) callback(null, stdout, stderr);
});
if (stdin) {
proc.stdin.write(stdin);
}
}
Job.prototype.exec = function(cmd, callback) {
var ops = {cwd:process.cwd()};
if (this.options.timeout) {
ops.timeout = this.options.timeout;
}
child.exec(cmd, ops, callback);
}