cross-spawn-cb 0.6.0 → 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.
@@ -13,48 +13,51 @@ module.exports = function spawnCallback(command, args, options, callback) {
13
13
  callback = once(callback);
14
14
 
15
15
  var cp = spawn(command, args, options);
16
- var stdout, stderr;
17
- if (cp.stdout) {
18
- stdout = [];
19
- cp.stdout.on('data', stdout.push.bind(stdout));
20
- }
21
- if (cp.stderr) {
22
- stderr = [];
23
- cp.stderr.on('data', stderr.push.bind(stderr));
16
+
17
+ // collect output
18
+ var res = { stdout: null, stderr: null };
19
+ if (options.encoding) {
20
+ if (cp.stdout) {
21
+ res.stdout = [];
22
+ cp.stdout.on('data', res.stdout.push.bind(res.stdout));
23
+ }
24
+ if (cp.stderr) {
25
+ res.stderr = [];
26
+ cp.stderr.on('data', res.stderr.push.bind(res.stderr));
27
+ }
24
28
  }
29
+
30
+ // some versions of node emit both an error and close
25
31
  cp.on('error', function error(err) {
26
- // some versions of node emit both an error and close
27
32
  err.code === 'OK' || callback(err);
28
33
  });
34
+
35
+ // done
29
36
  cp.on('close', function (status, signal) {
30
37
  nextTick(function closeNextTick() {
31
- // format results - buffer
32
- if (stdout) stdout = Buffer.concat(stdout);
33
- if (stderr) stderr = Buffer.concat(stderr);
34
-
35
- // format results - string
36
- if ((options.encoding && options.encoding !== 'buffer')) {
37
- stdout = stdout.toString(options.encoding);
38
- stderr = stderr.toString(options.encoding);
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);
39
49
  }
50
+ res.stdio = [null, res.stdout, res.stderr];
40
51
 
41
52
  // process errors
42
- var err = status !== 0 ? new Error('Non-zero exit code: ' + status) : null;
43
- 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) {
44
55
  err = err || new Error('stderr has content');
45
- 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
+ }
46
59
  }
47
- if (err) return callback(err);
48
-
49
- // non error result
50
- callback(null, {
51
- pid: cp.pid,
52
- output: [null, stdout, stderr],
53
- stdout: stdout,
54
- stderr: stderr,
55
- status: status,
56
- signal: signal,
57
- });
60
+ err ? callback(err) : callback(null, res);
58
61
  });
59
62
  });
60
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.0",
3
+ "version": "0.6.4",
4
4
  "description": "Cross spawn with a completion callback",
5
5
  "keywords": [
6
6
  "cross-spawn",
@@ -16,7 +16,7 @@
16
16
  "scripts": {
17
17
  "format": "prettier --write .",
18
18
  "lint": "eslint .",
19
- "prepublishOnly": "dtd \"npm run lint\" \"depcheck\"",
19
+ "prepublishOnly": "npm run lint && depcheck",
20
20
  "test": "mocha-compat test/spec/**/*.test.js",
21
21
  "build": "rollup -c config/rollup.cs.config.js"
22
22
  },
@@ -37,7 +37,6 @@
37
37
  "@rollup/plugin-node-resolve": "^13.3.0",
38
38
  "@typescript-eslint/parser": "^5.30.6",
39
39
  "depcheck": "^1.4.3",
40
- "dis-dat": "^0.1.7",
41
40
  "eslint": "^8.19.0",
42
41
  "eslint-config-prettier": "^8.5.0",
43
42
  "eslint-config-standard": "^17.0.0",