@abtnode/util 1.16.6-beta-79e0bbcc → 1.16.6-beta-56be9f01

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/lib/run-script.js +28 -7
  2. package/package.json +5 -5
package/lib/run-script.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable prefer-const */
2
2
  /* eslint-disable no-nested-ternary */
3
- const fs = require('fs');
3
+ const fs = require('fs-extra');
4
4
  const which = require('which');
5
5
  const stream = require('stream');
6
6
  const spawn = require('cross-spawn');
@@ -44,6 +44,8 @@ const wrapStream = (next, prefix, state) => {
44
44
  * @param {string} options.cwd
45
45
  * @param {number} options.timeout
46
46
  * @param {boolean} options.silent
47
+ * @param {string} options.output
48
+ * @param {string} options.error
47
49
  * @return {Promise}
48
50
  */
49
51
  const runScript = (script, label, options = {}) => {
@@ -63,6 +65,9 @@ const runScript = (script, label, options = {}) => {
63
65
  }
64
66
  };
65
67
 
68
+ const { output: outputFile, error: errorFile, silent, env, cwd, ...opts } = options;
69
+ const timeout = opts.timeout ?? 1000 * 120;
70
+
66
71
  const promise = new Promise((resolve, reject) => {
67
72
  process.stdout.setMaxListeners(0);
68
73
  process.stderr.setMaxListeners(0);
@@ -81,19 +86,28 @@ const runScript = (script, label, options = {}) => {
81
86
  lastIsLineBreak: true,
82
87
  };
83
88
 
84
- const stdout = wrapStream(process.stdout, label, state);
85
- const stderr = wrapStream(process.stderr, label, state);
89
+ [outputFile, errorFile].forEach((file) => {
90
+ if (file) {
91
+ fs.ensureFileSync(file);
92
+ }
93
+ });
94
+
95
+ const stdout = wrapStream(outputFile ? fs.createWriteStream(outputFile) : process.stdout, label, state);
96
+ const stderr = wrapStream(errorFile ? fs.createWriteStream(errorFile) : process.stderr, label, state);
86
97
 
98
+ const now = Date.now();
87
99
  child = spawn(command, args, {
88
- timeout: 2 * 60 * 1000,
100
+ ...opts,
89
101
  windowsHide: true, // required for Windows
90
102
  detached: false,
91
103
  shell: false,
92
104
  stdio: ['ignore', 'pipe', 'pipe'],
93
- ...options,
105
+ timeout,
106
+ env,
107
+ cwd,
94
108
  });
95
109
 
96
- if (!options.silent) {
110
+ if (!silent) {
97
111
  child.stdout.pipe(stdout, { end: false });
98
112
  child.stderr.pipe(stderr, { end: false });
99
113
  }
@@ -113,7 +127,14 @@ const runScript = (script, label, options = {}) => {
113
127
  return reject(new Error(errorMessages.join('\r\n')));
114
128
  });
115
129
 
116
- child.on('close', (code) => {
130
+ child.on('close', (code, signal) => {
131
+ if (signal) {
132
+ if (Date.now() - now > timeout) {
133
+ return reject(new Error(`Process timeout after ${timeout / 1000} seconds`));
134
+ }
135
+ return reject(new Error(`Process was killed with signal: ${signal}`));
136
+ }
137
+
117
138
  if (errorMessages.length > 0) {
118
139
  if (code !== 0 || hasUnhandledRejection) {
119
140
  return reject(new Error(errorMessages.join('\r\n')));
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.16.6-beta-79e0bbcc",
6
+ "version": "1.16.6-beta-56be9f01",
7
7
  "description": "ArcBlock's JavaScript utility",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -18,9 +18,9 @@
18
18
  "author": "polunzh <polunzh@gmail.com> (http://github.com/polunzh)",
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@abtnode/constant": "1.16.6-beta-79e0bbcc",
22
- "@abtnode/logger": "1.16.6-beta-79e0bbcc",
23
- "@blocklet/constant": "1.16.6-beta-79e0bbcc",
21
+ "@abtnode/constant": "1.16.6-beta-56be9f01",
22
+ "@abtnode/logger": "1.16.6-beta-56be9f01",
23
+ "@blocklet/constant": "1.16.6-beta-56be9f01",
24
24
  "@ocap/client": "1.18.76",
25
25
  "@ocap/mcrypto": "1.18.76",
26
26
  "@ocap/util": "1.18.76",
@@ -71,5 +71,5 @@
71
71
  "jest": "^27.5.1",
72
72
  "unzipper": "^0.10.11"
73
73
  },
74
- "gitHead": "a14091393bf4c1a7b430155cf27343a0f23ffa2c"
74
+ "gitHead": "f93c24693ed27c4a2821ec33c345696afe6f001a"
75
75
  }