Skip to content

Commit

Permalink
Stream stdout/err logging in sh.exec process (fixes #727
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jan 24, 2021
1 parent 55375bb commit 80241e1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 8 deletions.
11 changes: 3 additions & 8 deletions lib/shell.js
Expand Up @@ -53,22 +53,17 @@ class Shell {

execStringCommand(command, options, { isExternal }) {
return new Promise((resolve, reject) => {
sh.exec(command, { async: true }, (code, stdout, stderr) => {
const childProcess = sh.exec(command, { async: true }, (code, stdout, stderr) => {
stdout = stdout.toString().trim();
this.log.verbose(stdout, { isExternal });
debug({ command, options, code, stdout, stderr });
if (code === 0) {
if (stderr) {
this.log.verbose(stderr.toString().trim(), { isExternal });
}
resolve(stdout);
} else {
if (stdout && stderr) {
this.log.log(`\n${stdout}`);
}
reject(new Error(stderr || stdout));
}
});
childProcess.stdout.on('data', stdout => this.log.verbose(stdout.toString().trim(), { isExternal }));
childProcess.stderr.on('data', stderr => this.log.verbose(stderr.toString().trim(), { isExternal }));
});
}

Expand Down
2 changes: 2 additions & 0 deletions test/shell.js
Expand Up @@ -50,7 +50,9 @@ test('exec (verbose)', async t => {
const shell = factory(Shell, { options: { verbose: true } });
const actual = await shell.exec('echo foo');
t.is(shell.log.exec.firstCall.args[0], 'echo foo');
t.is(shell.log.exec.callCount, 1);
t.is(shell.log.verbose.firstCall.args[0], 'foo');
t.is(shell.log.verbose.callCount, 1);
t.is(actual, 'foo');
});

Expand Down

0 comments on commit 80241e1

Please sign in to comment.