Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw error in sync method - fixes #66 #67

Merged
merged 1 commit into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,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 @@ -95,6 +95,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