Skip to content

Commit

Permalink
Throw error in sync method - fixes #66 (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren authored and sindresorhus committed Jan 9, 2017
1 parent 1a41fac commit c8c9c7b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ module.exports.sync = (cmd, args, opts) => {

const result = childProcess.spawnSync(parsed.cmd, parsed.args, parsed.opts);

if (result.error || result.status !== 0) {
throw (result.error || new Error(result.stderr === '' ? result.stdout : result.stderr));
}

result.stdout = handleOutput(parsed.opts, result.stdout);
result.stderr = handleOutput(parsed.opts, result.stderr);

Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ Same options as [`child_process.execFileSync`](https://nodejs.org/api/child_proc

Returns the same result object as [`child_process.spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options).

This method throws an `Error` if the command fails.

### execa.shellSync(file, [options])

Execute a command synchronously through the system shell.
Expand Down
4 changes: 4 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ test('execa.sync()', t => {
t.is(stdout, 'foo');
});

test('execa.sync() throws error if written to stderr', t => {
t.throws(() => m.sync('foo'), process.platform === 'win32' ? /'foo' is not recognized as an internal or external command/ : 'spawnSync foo ENOENT');
});

test('execa.shellSync()', t => {
const {stdout} = m.shellSync('node fixtures/noop foo');
t.is(stdout, 'foo');
Expand Down

0 comments on commit c8c9c7b

Please sign in to comment.