Skip to content

Commit

Permalink
Add cwd error property (#565)
Browse files Browse the repository at this point in the history
  • Loading branch information
homersimpsons authored Jul 26, 2023
1 parent a228eda commit f57fdec
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
11 changes: 9 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ export type ExecaReturnBase<StdoutStderrType extends StdoutStderrAll> = {
If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. It is also `undefined` when the signal is very uncommon which should seldomly happen.
*/
signalDescription?: string;

/**
The `cwd` of the command if provided in the command options. Otherwise it is `process.cwd()`.
*/
cwd: string;
};

export type ExecaSyncReturnValue<StdoutStderrType extends StdoutStderrAll = string> = {
Expand Down Expand Up @@ -596,7 +601,8 @@ try {
failed: true,
timedOut: false,
isCanceled: false,
killed: false
killed: false,
cwd: '/path/to/cwd'
}
\*\/
}
Expand Down Expand Up @@ -679,7 +685,8 @@ try {
failed: true,
timedOut: false,
isCanceled: false,
killed: false
killed: false,
cwd: '/path/to/cwd'
}
\*\/
}
Expand Down
4 changes: 4 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ try {
expectType<boolean>(unicornsResult.killed);
expectType<string | undefined>(unicornsResult.signal);
expectType<string | undefined>(unicornsResult.signalDescription);
expectType<string>(unicornsResult.cwd);
} catch (error: unknown) {
const execaError = error as ExecaError;

Expand All @@ -85,6 +86,7 @@ try {
expectType<boolean>(execaError.killed);
expectType<string | undefined>(execaError.signal);
expectType<string | undefined>(execaError.signalDescription);
expectType<string>(execaError.cwd);
expectType<string>(execaError.shortMessage);
expectType<string | undefined>(execaError.originalMessage);
}
Expand All @@ -106,6 +108,7 @@ try {
expectType<boolean>(unicornsResult.killed);
expectType<string | undefined>(unicornsResult.signal);
expectType<string | undefined>(unicornsResult.signalDescription);
expectType<string>(unicornsResult.cwd);
} catch (error: unknown) {
const execaError = error as ExecaSyncError;

Expand All @@ -120,6 +123,7 @@ try {
expectType<boolean>(execaError.killed);
expectType<string | undefined>(execaError.signal);
expectType<string | undefined>(execaError.signalDescription);
expectType<string>(execaError.cwd);
expectType<string>(execaError.shortMessage);
expectType<string | undefined>(execaError.originalMessage);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/error.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import process from 'node:process';
import {signalsByName} from 'human-signals';

const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => {
Expand Down Expand Up @@ -36,7 +37,7 @@ export const makeError = ({
timedOut,
isCanceled,
killed,
parsed: {options: {timeout}},
parsed: {options: {timeout, cwd = process.cwd()}},
}) => {
// `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`.
// We normalize them to `undefined`
Expand Down Expand Up @@ -67,6 +68,7 @@ export const makeError = ({
error.signalDescription = signalDescription;
error.stdout = stdout;
error.stderr = stderr;
error.cwd = cwd;

if (all !== undefined) {
error.all = all;
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,12 @@ A human-friendly description of the signal that was used to terminate the proces

If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. It is also `undefined` when the signal is very uncommon which should seldomly happen.

#### cwd

Type: `string`

The `cwd` of the command if provided in the [command options](#cwd-1). Otherwise it is `process.cwd()`.

#### message

Type: `string`
Expand Down
13 changes: 12 additions & 1 deletion test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import childProcess from 'node:child_process';
import {promisify} from 'node:util';
import test from 'ava';
import {execa, execaSync} from '../index.js';
import {setFixtureDir} from './helpers/fixtures-dir.js';
import {FIXTURES_DIR, setFixtureDir} from './helpers/fixtures-dir.js';

const pExec = promisify(childProcess.exec);

Expand Down Expand Up @@ -212,3 +212,14 @@ test('error.code is defined on failure if applicable', async t => {
const {code} = await t.throwsAsync(execa('noop.js', {cwd: 1}));
t.is(code, 'ERR_INVALID_ARG_TYPE');
});

test('error.cwd is defined on failure if applicable', async t => {
const {cwd} = await t.throwsAsync(execa('noop-throw.js', [], {cwd: FIXTURES_DIR}));
t.is(cwd, FIXTURES_DIR);
});

test('error.cwd is undefined on failure if not passed as options', async t => {
const expectedCwd = process.cwd();
const {cwd} = await t.throwsAsync(execa('noop-throw.js'));
t.is(cwd, expectedCwd);
});

0 comments on commit f57fdec

Please sign in to comment.