cross-spawn-cb 0.6.3 → 0.6.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/spawnCallback.js +23 -28
- package/lib/spawnSyncCallback.js +18 -11
- package/package.json +1 -1
package/lib/spawnCallback.js
CHANGED
|
@@ -15,15 +15,15 @@ module.exports = function spawnCallback(command, args, options, callback) {
|
|
|
15
15
|
var cp = spawn(command, args, options);
|
|
16
16
|
|
|
17
17
|
// collect output
|
|
18
|
-
var stdout, stderr;
|
|
18
|
+
var res = { stdout: null, stderr: null };
|
|
19
19
|
if (options.encoding) {
|
|
20
20
|
if (cp.stdout) {
|
|
21
|
-
stdout = [];
|
|
22
|
-
cp.stdout.on('data', stdout.push.bind(stdout));
|
|
21
|
+
res.stdout = [];
|
|
22
|
+
cp.stdout.on('data', res.stdout.push.bind(res.stdout));
|
|
23
23
|
}
|
|
24
24
|
if (cp.stderr) {
|
|
25
|
-
stderr = [];
|
|
26
|
-
cp.stderr.on('data', stderr.push.bind(stderr));
|
|
25
|
+
res.stderr = [];
|
|
26
|
+
cp.stderr.on('data', res.stderr.push.bind(res.stderr));
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -35,34 +35,29 @@ module.exports = function spawnCallback(command, args, options, callback) {
|
|
|
35
35
|
// done
|
|
36
36
|
cp.on('close', function (status, signal) {
|
|
37
37
|
nextTick(function closeNextTick() {
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
38
|
+
// prepare result
|
|
39
|
+
res.pid = cp.pid;
|
|
40
|
+
res.status = status;
|
|
41
|
+
res.signal = signal;
|
|
42
|
+
if (res.stdout) {
|
|
43
|
+
res.stdout = Buffer.concat(res.stdout);
|
|
44
|
+
if (options.encoding !== 'buffer') res.stdout = res.stdout.toString(options.encoding);
|
|
45
|
+
}
|
|
46
|
+
if (res.stderr) {
|
|
47
|
+
res.stderr = Buffer.concat(res.stderr);
|
|
48
|
+
if (options.encoding !== 'buffer') res.stderr = res.stderr.toString(options.encoding);
|
|
47
49
|
}
|
|
50
|
+
res.stdio = [null, res.stdout, res.stderr];
|
|
48
51
|
|
|
49
52
|
// process errors
|
|
50
|
-
var err = status !== 0 ? new Error('Non-zero exit code: ' + status) : null;
|
|
51
|
-
if (stderr && stderr.length) {
|
|
53
|
+
var err = res.status !== 0 ? new Error('Non-zero exit code: ' + res.status) : null;
|
|
54
|
+
if (res.stderr && res.stderr.length) {
|
|
52
55
|
err = err || new Error('stderr has content');
|
|
53
|
-
|
|
56
|
+
for (var key in res) {
|
|
57
|
+
err[key] = Buffer.isBuffer(res[key]) ? res[key].toString('utf8') : res[key];
|
|
58
|
+
}
|
|
54
59
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
// non error result
|
|
58
|
-
callback(null, {
|
|
59
|
-
pid: cp.pid,
|
|
60
|
-
output: [null, stdout, stderr],
|
|
61
|
-
stdout: stdout,
|
|
62
|
-
stderr: stderr,
|
|
63
|
-
status: status,
|
|
64
|
-
signal: signal,
|
|
65
|
-
});
|
|
60
|
+
err ? callback(err) : callback(null, res);
|
|
66
61
|
});
|
|
67
62
|
});
|
|
68
63
|
|
package/lib/spawnSyncCallback.js
CHANGED
|
@@ -10,20 +10,27 @@ module.exports = function spawnSyncCallback(command, args, options) {
|
|
|
10
10
|
stdio: 'pipe',
|
|
11
11
|
encoding: 'utf8',
|
|
12
12
|
});
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
|
|
14
|
+
var res = spawnSync(command, args, syncOptions);
|
|
15
|
+
|
|
16
|
+
// pipe if inherited
|
|
17
|
+
if (res.stdout && (options.stdout === 'inherit' || options.stdio === 'inherit')) {
|
|
18
|
+
process.stdout.write(res.stdout);
|
|
19
|
+
res.stdout = null;
|
|
17
20
|
}
|
|
18
|
-
if (
|
|
19
|
-
process.stderr.write(
|
|
20
|
-
|
|
21
|
+
if (res.stderr && (options.stderr === 'inherit' || options.stdio === 'inherit')) {
|
|
22
|
+
process.stderr.write(res.stderr);
|
|
23
|
+
res.stderr = null;
|
|
21
24
|
}
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
|
|
26
|
+
// process errors
|
|
27
|
+
var err = res.status !== 0 ? new Error('Non-zero exit code: ' + res.status) : null;
|
|
28
|
+
if (res.stderr && res.stderr.length) {
|
|
24
29
|
err = err || new Error('stderr has content');
|
|
25
|
-
|
|
30
|
+
for (var key in res) {
|
|
31
|
+
err[key] = Buffer.isBuffer(res[key]) ? res[key].toString('utf8') : res[key];
|
|
32
|
+
}
|
|
26
33
|
}
|
|
27
34
|
if (err) throw err;
|
|
28
|
-
return
|
|
35
|
+
return res;
|
|
29
36
|
};
|