@vbarbarosh/node-helpers 3.23.6 → 3.24.0

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/shell_spawn.js +27 -21
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Vladimir Barbarosh <vladimir.barbarosh@gmail.com>",
3
3
  "license": "MIT",
4
4
  "name": "@vbarbarosh/node-helpers",
5
- "version": "3.23.6",
5
+ "version": "3.24.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -1,33 +1,39 @@
1
+ const Promise = require('bluebird');
1
2
  const child_process = require('child_process');
2
3
 
3
4
  function shell_spawn(args, options)
4
5
  {
5
- let promise;
6
- const proc = child_process.spawn(args[0], args.slice(1), options);
7
- proc.promise = function () {
8
- let is_pending = true;
6
+ let init, promise;
7
+ const out = child_process.spawn(args[0], args.slice(1), options);
8
+ out.init = function () {
9
+ return init = init || new Promise(function (resolve, reject) {
10
+ out.once('error', init_error);
11
+ out.once('spawn', init_spawn);
12
+ function init_error(error) {
13
+ out.off('spawn', init_spawn);
14
+ reject(error);
15
+ }
16
+ function init_spawn() {
17
+ out.off('error', init_error);
18
+ resolve(out);
19
+ }
20
+ });
21
+ };
22
+ out.promise = function () {
9
23
  return promise = promise || new Promise(function (resolve, reject) {
10
- proc.on('error', on_error);
11
- proc.on('exit', on_exit);
12
- function on_error(error) {
13
- if (is_pending) {
14
- is_pending = false;
15
- proc.off('error', on_error);
16
- proc.off('exit', on_exit);
17
- reject(error);
18
- }
24
+ out.once('error', promise_error);
25
+ out.once('exit', promise_exit);
26
+ function promise_error(error) {
27
+ out.off('exit', promise_exit);
28
+ reject(error);
19
29
  }
20
- function on_exit(code, signal) {
21
- if (is_pending) {
22
- is_pending = false;
23
- proc.off('error', on_error);
24
- proc.off('exit', on_exit);
25
- resolve({code, signal});
26
- }
30
+ function promise_exit(code, signal) {
31
+ out.off('error', promise_error);
32
+ resolve({code, signal});
27
33
  }
28
34
  });
29
35
  };
30
- return proc;
36
+ return out;
31
37
  }
32
38
 
33
39
  module.exports = shell_spawn;