Skip to content

Commit

Permalink
fix: revert some of the recent changes to @bolt/build-tools sh.js uti…
Browse files Browse the repository at this point in the history
…lity -- 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
  • Loading branch information
bolt-bot committed Jul 16, 2018
1 parent 2cefbb5 commit 80f4c19
Show file tree
Hide file tree
Showing 2 changed files with 557 additions and 542 deletions.
66 changes: 39 additions & 27 deletions packages/build-tools/utils/sh.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,6 @@ const chalk = require('chalk');
const execa = require('execa');
const notifier = require('node-notifier');

function handleShResults(results, exitOnError, streamOutput, showCmdOnError, exitImmediately, resolve, reject) {
const { code, stdout, stderr, message } = results;
const output = stderr + '\n\n' + stdout;
if (code > 0) {
const errorMsg = chalk.red(showCmdOnError ? message : `Error with code ${code}`);
if (exitOnError) {
if (exitImmediately) {
console.error(errorMsg + output);
process.exit(1);
}
process.exitCode = 1;
reject(new Error(errorMsg + output));
} else {
notifier.notify({
title: cmd,
message: output,
sound: true,
});
reject(errorMsg + output);
}
}
resolve(output);
}

/**
* Run shell command
* @param cmd {string} - Command to run
Expand All @@ -38,16 +14,52 @@ function handleShResults(results, exitOnError, streamOutput, showCmdOnError, exi
async function sh(cmd, args, exitOnError, streamOutput, showCmdOnError = true, exitImmediately = false) {
return new Promise((resolve, reject) => {
const child = execa(cmd, args);
let output = '';

if (streamOutput) {
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
}

child.then((results) => handleShResults(results, exitOnError, streamOutput, showCmdOnError, exitImmediately, resolve, reject));
child.catch((results) => handleShResults(results, exitOnError, streamOutput, showCmdOnError, exitImmediately, resolve, reject));
});
child.stdout.on('data', (data) => {
output += data;
if (streamOutput) {
process.stdout.write(data);
}
});

child.stderr.on('data', (data) => {
output += data;
if (streamOutput) {
process.stdout.write(data);
}
});

child.on('close', (code) => {
if (code > 0) {
const errorMsg = chalk.red(`
Error with code ${code}${showCmdOnError ? ` after running: ${cmd}`: ''}:
`);
if (exitOnError) {
if (exitImmediately) {
console.error(errorMsg + output);
process.exit(1);
}

process.exitCode = 1;
reject(new Error(errorMsg + output));
} else {
notifier.notify({
title: cmd,
message: output,
sound: true,
});
reject(errorMsg + output);
}
}
resolve(output);
});
});
}

module.exports = sh;
Loading

0 comments on commit 80f4c19

Please sign in to comment.