Skip to content

Commit 80f4c19

Browse files
committed
fix: revert some of the recent changes to @bolt/build-tools sh.js utility -- shell commands are still wired up to use execa under the hood however errors thrown via command line commands are now properly thrown / resolved vs hanging indefinitly
1 parent 2cefbb5 commit 80f4c19

File tree

2 files changed

+557
-542
lines changed

2 files changed

+557
-542
lines changed

packages/build-tools/utils/sh.js

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,6 @@ const chalk = require('chalk');
22
const execa = require('execa');
33
const notifier = require('node-notifier');
44

5-
function handleShResults(results, exitOnError, streamOutput, showCmdOnError, exitImmediately, resolve, reject) {
6-
const { code, stdout, stderr, message } = results;
7-
const output = stderr + '\n\n' + stdout;
8-
if (code > 0) {
9-
const errorMsg = chalk.red(showCmdOnError ? message : `Error with code ${code}`);
10-
if (exitOnError) {
11-
if (exitImmediately) {
12-
console.error(errorMsg + output);
13-
process.exit(1);
14-
}
15-
process.exitCode = 1;
16-
reject(new Error(errorMsg + output));
17-
} else {
18-
notifier.notify({
19-
title: cmd,
20-
message: output,
21-
sound: true,
22-
});
23-
reject(errorMsg + output);
24-
}
25-
}
26-
resolve(output);
27-
}
28-
295
/**
306
* Run shell command
317
* @param cmd {string} - Command to run
@@ -38,16 +14,52 @@ function handleShResults(results, exitOnError, streamOutput, showCmdOnError, exi
3814
async function sh(cmd, args, exitOnError, streamOutput, showCmdOnError = true, exitImmediately = false) {
3915
return new Promise((resolve, reject) => {
4016
const child = execa(cmd, args);
17+
let output = '';
4118

4219
if (streamOutput) {
4320
child.stdout.pipe(process.stdout);
4421
child.stderr.pipe(process.stderr);
4522
}
4623

47-
child.then((results) => handleShResults(results, exitOnError, streamOutput, showCmdOnError, exitImmediately, resolve, reject));
48-
child.catch((results) => handleShResults(results, exitOnError, streamOutput, showCmdOnError, exitImmediately, resolve, reject));
49-
});
24+
child.stdout.on('data', (data) => {
25+
output += data;
26+
if (streamOutput) {
27+
process.stdout.write(data);
28+
}
29+
});
5030

31+
child.stderr.on('data', (data) => {
32+
output += data;
33+
if (streamOutput) {
34+
process.stdout.write(data);
35+
}
36+
});
37+
38+
child.on('close', (code) => {
39+
if (code > 0) {
40+
const errorMsg = chalk.red(`
41+
Error with code ${code}${showCmdOnError ? ` after running: ${cmd}`: ''}:
42+
`);
43+
if (exitOnError) {
44+
if (exitImmediately) {
45+
console.error(errorMsg + output);
46+
process.exit(1);
47+
}
48+
49+
process.exitCode = 1;
50+
reject(new Error(errorMsg + output));
51+
} else {
52+
notifier.notify({
53+
title: cmd,
54+
message: output,
55+
sound: true,
56+
});
57+
reject(errorMsg + output);
58+
}
59+
}
60+
resolve(output);
61+
});
62+
});
5163
}
5264

5365
module.exports = sh;

0 commit comments

Comments
 (0)