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.
@@ -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
- // process output
39
- if (options.encoding) {
40
- if (stdout) stdout = Buffer.concat(stdout);
41
- if (stderr) stderr = Buffer.concat(stderr);
42
-
43
- if (options.encoding !== 'buffer') {
44
- if (stdout) stdout = stdout.toString(options.encoding);
45
- if (stderr) stderr = stderr.toString(options.encoding);
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
- err.stderr = Buffer.isBuffer(stderr) ? stderr.toString('utf8') : stderr;
56
+ for (var key in res) {
57
+ err[key] = Buffer.isBuffer(res[key]) ? res[key].toString('utf8') : res[key];
58
+ }
54
59
  }
55
- if (err) return callback(err);
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
 
@@ -10,20 +10,27 @@ module.exports = function spawnSyncCallback(command, args, options) {
10
10
  stdio: 'pipe',
11
11
  encoding: 'utf8',
12
12
  });
13
- var result = spawnSync(command, args, syncOptions);
14
- if (result.stdout && (options.stdout === 'inherit' || options.stdio === 'inherit')) {
15
- process.stdout.write(result.stdout);
16
- result.stdout = null;
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 (result.stderr && (options.stderr === 'inherit' || options.stdio === 'inherit')) {
19
- process.stderr.write(result.stderr);
20
- result.stderr = null;
21
+ if (res.stderr && (options.stderr === 'inherit' || options.stdio === 'inherit')) {
22
+ process.stderr.write(res.stderr);
23
+ res.stderr = null;
21
24
  }
22
- var err = result.status ? new Error('Non-zero exit code: ' + result.status) : null;
23
- if (result.stderr && result.stderr.length) {
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
- err.stderr = result.stderr;
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 result;
35
+ return res;
29
36
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cross-spawn-cb",
3
- "version": "0.6.3",
3
+ "version": "0.6.4",
4
4
  "description": "Cross spawn with a completion callback",
5
5
  "keywords": [
6
6
  "cross-spawn",