forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processes.js
124 lines (101 loc) · 3.17 KB
/
processes.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
var Future = require('fibers/future');
var _ = require('underscore');
var child_process = require('child_process');
var Console = require('./console.js').Console;
var processes = exports;
var RunCommand = function (command, args, options) {
var self = this;
var defaultOptions = {};
// Make stdin,stdout & stderr pipes (not shared)
defaultOptions.stdio = ['pipe', 'pipe', 'pipe'];
defaultOptions.env = process.env;
defaultOptions.checkExitCode = true;
options = _.extend(defaultOptions, options);
self.command = command;
self.args = args;
self.options = options;
self.exitFuture = new Future();
self.exitCode = undefined;
self.stdout = '';
self.stderr = '';
};
_.extend(RunCommand.prototype, {
start: function () {
var self = this;
if (self.process) {
throw new Error("Process already started");
}
if (Console.isDebugEnabled()) {
var envString = '';
var defaultEnv = process.env;
if (self.options.env) {
_.each(self.options.env, function (v,k) {
var defaultV = defaultEnv[k];
if (v !== defaultV) {
envString += k + "=" + v + " ";
}
});
}
Console.debug("Running command", envString, self.command, self.args.join(' '));
}
self.process = child_process.spawn( self.command,
self.args,
self.options);
self.process.on('close', function (exitCode) {
self.exitCode = exitCode;
if (self.options.checkExitCode && exitCode != 0) {
console.log("Unexpected exit code", exitCode, "from", self.command, self.args, "\nstdout:\n", self.stdout, "\nstderr:\n", self.stderr);
}
self.exitFuture.isResolved() || self.exitFuture['return'](exitCode);
});
self.process.on('error', function (err) {
Console.debug("Error while running command", err);
self.exitError = err;
self.exitFuture.isResolved() || self.exitFuture['throw'](err);
});
self.process.stdout.on('data', function (data) {
self.stdout = self.stdout + data;
if (self.options.pipeOutput) {
Console.stdout.write(data);
}
if (self.options.onStdout) {
self.options.onStdout(data);
}
});
self.process.stderr.on('data', function (data) {
self.stderr = self.stderr + data;
if (self.options.pipeOutput) {
Console.stderr.write(data);
}
if (self.options.onStderr) {
self.options.onStderr(data);
}
});
self.stdin = self.process.stdin;
if (self.options.stdin) {
self.stdin.write(self.options.stdin);
}
if (self.options.detached) {
self.process.unref();
}
},
waitForExit: function () {
var self = this;
return self.exitFuture.wait();
},
kill: function () {
var self = this;
self.process.kill();
},
run: function () {
var self = this;
self.start();
if (self.options.detached) {
Console.debug("run called on detached process; won't wait");
return undefined;
}
self.waitForExit();
return { stdout: self.stdout, stderr: self.stderr, exitCode: self.exitCode };
}
});
exports.RunCommand = RunCommand;