@naturalcycles/nodejs-lib 13.34.0 → 13.34.1
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/dist/util/exec2.d.ts +5 -0
- package/dist/util/exec2.js +21 -10
- package/package.json +1 -1
- package/src/util/exec2.ts +27 -8
package/dist/util/exec2.d.ts
CHANGED
|
@@ -145,6 +145,11 @@ export interface SpawnOptions {
|
|
|
145
145
|
* Set to true to pass `process.env` to the spawned process.
|
|
146
146
|
*/
|
|
147
147
|
passProcessEnv?: boolean;
|
|
148
|
+
/**
|
|
149
|
+
* Defaults to "auto detect colors".
|
|
150
|
+
* Set to false or true to override.
|
|
151
|
+
*/
|
|
152
|
+
forceColor?: boolean;
|
|
148
153
|
}
|
|
149
154
|
export interface ExecOptions {
|
|
150
155
|
/**
|
package/dist/util/exec2.js
CHANGED
|
@@ -50,9 +50,11 @@ class Exec2 {
|
|
|
50
50
|
async spawnAsync(cmd, opt = {}) {
|
|
51
51
|
const started = Date.now();
|
|
52
52
|
this.logStart(cmd, opt);
|
|
53
|
-
const { shell = true, printWhileRunning = true, collectOutputWhileRunning = true, throwOnNonZeroCode = true, cwd, env, } = opt;
|
|
53
|
+
const { shell = true, printWhileRunning = true, collectOutputWhileRunning = true, throwOnNonZeroCode = true, cwd, env, forceColor = colors_1.hasColors, } = opt;
|
|
54
54
|
let stdout = '';
|
|
55
55
|
let stderr = '';
|
|
56
|
+
if (printWhileRunning)
|
|
57
|
+
console.log(''); // 1-line padding before the output
|
|
56
58
|
return await new Promise((resolve, reject) => {
|
|
57
59
|
const p = node_child_process_1.default.spawn(cmd, opt.args || [], {
|
|
58
60
|
shell,
|
|
@@ -60,6 +62,7 @@ class Exec2 {
|
|
|
60
62
|
env: {
|
|
61
63
|
...env,
|
|
62
64
|
...(opt.passProcessEnv ? process.env : {}),
|
|
65
|
+
...(forceColor ? { FORCE_COLOR: '1' } : {}),
|
|
63
66
|
},
|
|
64
67
|
});
|
|
65
68
|
p.stdout.on('data', data => {
|
|
@@ -81,7 +84,10 @@ class Exec2 {
|
|
|
81
84
|
}
|
|
82
85
|
});
|
|
83
86
|
p.on('close', code => {
|
|
84
|
-
|
|
87
|
+
if (printWhileRunning)
|
|
88
|
+
console.log(''); // 1-line padding after the output
|
|
89
|
+
const isSuccessful = !code;
|
|
90
|
+
this.logFinish(cmd, opt, started, isSuccessful);
|
|
85
91
|
const exitCode = code || 0;
|
|
86
92
|
const o = {
|
|
87
93
|
exitCode,
|
|
@@ -111,7 +117,8 @@ class Exec2 {
|
|
|
111
117
|
spawn(cmd, opt = {}) {
|
|
112
118
|
const started = Date.now();
|
|
113
119
|
this.logStart(cmd, opt);
|
|
114
|
-
const { shell = true, cwd, env } = opt;
|
|
120
|
+
const { shell = true, cwd, env, forceColor = colors_1.hasColors } = opt;
|
|
121
|
+
console.log(''); // 1-line padding before the output
|
|
115
122
|
const r = node_child_process_1.default.spawnSync(cmd, opt.args, {
|
|
116
123
|
encoding: 'utf8',
|
|
117
124
|
stdio: 'inherit',
|
|
@@ -120,9 +127,12 @@ class Exec2 {
|
|
|
120
127
|
env: {
|
|
121
128
|
...env,
|
|
122
129
|
...(opt.passProcessEnv ? process.env : {}),
|
|
130
|
+
...(forceColor ? { FORCE_COLOR: '1' } : {}),
|
|
123
131
|
},
|
|
124
132
|
});
|
|
125
|
-
|
|
133
|
+
console.log(''); // 1-line padding after the output
|
|
134
|
+
const isSuccessful = !r.error && !r.status;
|
|
135
|
+
this.logFinish(cmd, opt, started, isSuccessful);
|
|
126
136
|
if (r.error) {
|
|
127
137
|
throw r.error;
|
|
128
138
|
}
|
|
@@ -149,7 +159,7 @@ class Exec2 {
|
|
|
149
159
|
this.logStart(cmd, opt);
|
|
150
160
|
const { cwd, env, timeout } = opt;
|
|
151
161
|
try {
|
|
152
|
-
|
|
162
|
+
const s = node_child_process_1.default
|
|
153
163
|
.execSync(cmd, {
|
|
154
164
|
encoding: 'utf8',
|
|
155
165
|
// stdio: 'inherit', // no, otherwise we don't get the output returned
|
|
@@ -163,6 +173,8 @@ class Exec2 {
|
|
|
163
173
|
},
|
|
164
174
|
})
|
|
165
175
|
.trim();
|
|
176
|
+
this.logFinish(cmd, opt, started, true);
|
|
177
|
+
return s;
|
|
166
178
|
}
|
|
167
179
|
catch (err) {
|
|
168
180
|
// Not logging stderr, as it's printed by execSync by default (somehow)
|
|
@@ -173,11 +185,9 @@ class Exec2 {
|
|
|
173
185
|
if (err.stdout) {
|
|
174
186
|
process.stdout.write(err.stdout);
|
|
175
187
|
}
|
|
188
|
+
this.logFinish(cmd, opt, started, false);
|
|
176
189
|
throw new Error(`exec exited with code ${err.status}: ${cmd}`);
|
|
177
190
|
}
|
|
178
|
-
finally {
|
|
179
|
-
this.logFinish(cmd, opt, started);
|
|
180
|
-
}
|
|
181
191
|
}
|
|
182
192
|
throwOnNonZeroExitCode(o) {
|
|
183
193
|
if (o.exitCode) {
|
|
@@ -195,13 +205,14 @@ class Exec2 {
|
|
|
195
205
|
.filter(Boolean)
|
|
196
206
|
.join(' '));
|
|
197
207
|
}
|
|
198
|
-
logFinish(cmd, opt, started) {
|
|
199
|
-
if (!opt.logFinish && !opt.log)
|
|
208
|
+
logFinish(cmd, opt, started, isSuccessful) {
|
|
209
|
+
if (isSuccessful && !opt.logFinish && !opt.log)
|
|
200
210
|
return;
|
|
201
211
|
console.log([
|
|
202
212
|
(0, colors_1.white)(opt.name || cmd),
|
|
203
213
|
...((!opt.name && opt.args) || []),
|
|
204
214
|
(0, colors_1.dimGrey)('took ' + (0, js_lib_1._since)(started)),
|
|
215
|
+
!isSuccessful && (0, colors_1.dimGrey)('and ') + (0, colors_1.dimRed)('failed'),
|
|
205
216
|
]
|
|
206
217
|
.filter(Boolean)
|
|
207
218
|
.join(' '));
|
package/package.json
CHANGED
package/src/util/exec2.ts
CHANGED
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
NumberOfMilliseconds,
|
|
7
7
|
UnixTimestampMillisNumber,
|
|
8
8
|
} from '@naturalcycles/js-lib'
|
|
9
|
-
import { dimGrey, white } from '../colors/colors'
|
|
9
|
+
import { dimGrey, dimRed, hasColors, white } from '../colors/colors'
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Set of utility functions to work with Spawn / Exec.
|
|
@@ -60,10 +60,13 @@ class Exec2 {
|
|
|
60
60
|
throwOnNonZeroCode = true,
|
|
61
61
|
cwd,
|
|
62
62
|
env,
|
|
63
|
+
forceColor = hasColors,
|
|
63
64
|
} = opt
|
|
64
65
|
let stdout = ''
|
|
65
66
|
let stderr = ''
|
|
66
67
|
|
|
68
|
+
if (printWhileRunning) console.log('') // 1-line padding before the output
|
|
69
|
+
|
|
67
70
|
return await new Promise<SpawnOutput>((resolve, reject) => {
|
|
68
71
|
const p = cp.spawn(cmd, opt.args || [], {
|
|
69
72
|
shell,
|
|
@@ -71,6 +74,7 @@ class Exec2 {
|
|
|
71
74
|
env: {
|
|
72
75
|
...env,
|
|
73
76
|
...(opt.passProcessEnv ? process.env : {}),
|
|
77
|
+
...(forceColor ? { FORCE_COLOR: '1' } : {}),
|
|
74
78
|
},
|
|
75
79
|
})
|
|
76
80
|
|
|
@@ -94,7 +98,9 @@ class Exec2 {
|
|
|
94
98
|
})
|
|
95
99
|
|
|
96
100
|
p.on('close', code => {
|
|
97
|
-
|
|
101
|
+
if (printWhileRunning) console.log('') // 1-line padding after the output
|
|
102
|
+
const isSuccessful = !code
|
|
103
|
+
this.logFinish(cmd, opt, started, isSuccessful)
|
|
98
104
|
const exitCode = code || 0
|
|
99
105
|
const o: SpawnOutput = {
|
|
100
106
|
exitCode,
|
|
@@ -125,7 +131,8 @@ class Exec2 {
|
|
|
125
131
|
spawn(cmd: string, opt: SpawnOptions = {}): void {
|
|
126
132
|
const started = Date.now()
|
|
127
133
|
this.logStart(cmd, opt)
|
|
128
|
-
const { shell = true, cwd, env } = opt
|
|
134
|
+
const { shell = true, cwd, env, forceColor = hasColors } = opt
|
|
135
|
+
console.log('') // 1-line padding before the output
|
|
129
136
|
|
|
130
137
|
const r = cp.spawnSync(cmd, opt.args, {
|
|
131
138
|
encoding: 'utf8',
|
|
@@ -135,10 +142,13 @@ class Exec2 {
|
|
|
135
142
|
env: {
|
|
136
143
|
...env,
|
|
137
144
|
...(opt.passProcessEnv ? process.env : {}),
|
|
145
|
+
...(forceColor ? { FORCE_COLOR: '1' } : {}),
|
|
138
146
|
},
|
|
139
147
|
})
|
|
140
148
|
|
|
141
|
-
|
|
149
|
+
console.log('') // 1-line padding after the output
|
|
150
|
+
const isSuccessful = !r.error && !r.status
|
|
151
|
+
this.logFinish(cmd, opt, started, isSuccessful)
|
|
142
152
|
|
|
143
153
|
if (r.error) {
|
|
144
154
|
throw r.error
|
|
@@ -168,7 +178,7 @@ class Exec2 {
|
|
|
168
178
|
const { cwd, env, timeout } = opt
|
|
169
179
|
|
|
170
180
|
try {
|
|
171
|
-
|
|
181
|
+
const s = cp
|
|
172
182
|
.execSync(cmd, {
|
|
173
183
|
encoding: 'utf8',
|
|
174
184
|
// stdio: 'inherit', // no, otherwise we don't get the output returned
|
|
@@ -182,6 +192,9 @@ class Exec2 {
|
|
|
182
192
|
},
|
|
183
193
|
})
|
|
184
194
|
.trim()
|
|
195
|
+
|
|
196
|
+
this.logFinish(cmd, opt, started, true)
|
|
197
|
+
return s
|
|
185
198
|
} catch (err) {
|
|
186
199
|
// Not logging stderr, as it's printed by execSync by default (somehow)
|
|
187
200
|
// stdout is not printed by execSync though, therefor we print it here
|
|
@@ -191,9 +204,8 @@ class Exec2 {
|
|
|
191
204
|
if ((err as any).stdout) {
|
|
192
205
|
process.stdout.write((err as any).stdout)
|
|
193
206
|
}
|
|
207
|
+
this.logFinish(cmd, opt, started, false)
|
|
194
208
|
throw new Error(`exec exited with code ${(err as any).status}: ${cmd}`)
|
|
195
|
-
} finally {
|
|
196
|
-
this.logFinish(cmd, opt, started)
|
|
197
209
|
}
|
|
198
210
|
}
|
|
199
211
|
|
|
@@ -221,14 +233,16 @@ class Exec2 {
|
|
|
221
233
|
cmd: string,
|
|
222
234
|
opt: SpawnOptions | ExecOptions,
|
|
223
235
|
started: UnixTimestampMillisNumber,
|
|
236
|
+
isSuccessful: boolean,
|
|
224
237
|
): void {
|
|
225
|
-
if (!opt.logFinish && !opt.log) return
|
|
238
|
+
if (isSuccessful && !opt.logFinish && !opt.log) return
|
|
226
239
|
|
|
227
240
|
console.log(
|
|
228
241
|
[
|
|
229
242
|
white(opt.name || cmd),
|
|
230
243
|
...((!opt.name && (opt as SpawnOptions).args) || []),
|
|
231
244
|
dimGrey('took ' + _since(started)),
|
|
245
|
+
!isSuccessful && dimGrey('and ') + dimRed('failed'),
|
|
232
246
|
]
|
|
233
247
|
.filter(Boolean)
|
|
234
248
|
.join(' '),
|
|
@@ -316,6 +330,11 @@ export interface SpawnOptions {
|
|
|
316
330
|
* Set to true to pass `process.env` to the spawned process.
|
|
317
331
|
*/
|
|
318
332
|
passProcessEnv?: boolean
|
|
333
|
+
/**
|
|
334
|
+
* Defaults to "auto detect colors".
|
|
335
|
+
* Set to false or true to override.
|
|
336
|
+
*/
|
|
337
|
+
forceColor?: boolean
|
|
319
338
|
}
|
|
320
339
|
|
|
321
340
|
export interface ExecOptions {
|