Skip to content

Commit f57fdec

Browse files
authoredJul 26, 2023
Add cwd error property (#565)
1 parent a228eda commit f57fdec

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed
 

‎index.d.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,11 @@ export type ExecaReturnBase<StdoutStderrType extends StdoutStderrAll> = {
364364
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.
365365
*/
366366
signalDescription?: string;
367+
368+
/**
369+
The `cwd` of the command if provided in the command options. Otherwise it is `process.cwd()`.
370+
*/
371+
cwd: string;
367372
};
368373

369374
export type ExecaSyncReturnValue<StdoutStderrType extends StdoutStderrAll = string> = {
@@ -596,7 +601,8 @@ try {
596601
failed: true,
597602
timedOut: false,
598603
isCanceled: false,
599-
killed: false
604+
killed: false,
605+
cwd: '/path/to/cwd'
600606
}
601607
\*\/
602608
}
@@ -679,7 +685,8 @@ try {
679685
failed: true,
680686
timedOut: false,
681687
isCanceled: false,
682-
killed: false
688+
killed: false,
689+
cwd: '/path/to/cwd'
683690
}
684691
\*\/
685692
}

‎index.test-d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ try {
7171
expectType<boolean>(unicornsResult.killed);
7272
expectType<string | undefined>(unicornsResult.signal);
7373
expectType<string | undefined>(unicornsResult.signalDescription);
74+
expectType<string>(unicornsResult.cwd);
7475
} catch (error: unknown) {
7576
const execaError = error as ExecaError;
7677

@@ -85,6 +86,7 @@ try {
8586
expectType<boolean>(execaError.killed);
8687
expectType<string | undefined>(execaError.signal);
8788
expectType<string | undefined>(execaError.signalDescription);
89+
expectType<string>(execaError.cwd);
8890
expectType<string>(execaError.shortMessage);
8991
expectType<string | undefined>(execaError.originalMessage);
9092
}
@@ -106,6 +108,7 @@ try {
106108
expectType<boolean>(unicornsResult.killed);
107109
expectType<string | undefined>(unicornsResult.signal);
108110
expectType<string | undefined>(unicornsResult.signalDescription);
111+
expectType<string>(unicornsResult.cwd);
109112
} catch (error: unknown) {
110113
const execaError = error as ExecaSyncError;
111114

@@ -120,6 +123,7 @@ try {
120123
expectType<boolean>(execaError.killed);
121124
expectType<string | undefined>(execaError.signal);
122125
expectType<string | undefined>(execaError.signalDescription);
126+
expectType<string>(execaError.cwd);
123127
expectType<string>(execaError.shortMessage);
124128
expectType<string | undefined>(execaError.originalMessage);
125129
}

‎lib/error.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import process from 'node:process';
12
import {signalsByName} from 'human-signals';
23

34
const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => {
@@ -36,7 +37,7 @@ export const makeError = ({
3637
timedOut,
3738
isCanceled,
3839
killed,
39-
parsed: {options: {timeout}},
40+
parsed: {options: {timeout, cwd = process.cwd()}},
4041
}) => {
4142
// `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`.
4243
// We normalize them to `undefined`
@@ -67,6 +68,7 @@ export const makeError = ({
6768
error.signalDescription = signalDescription;
6869
error.stdout = stdout;
6970
error.stderr = stderr;
71+
error.cwd = cwd;
7072

7173
if (all !== undefined) {
7274
error.all = all;

‎readme.md

+6
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ A human-friendly description of the signal that was used to terminate the proces
461461

462462
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.
463463

464+
#### cwd
465+
466+
Type: `string`
467+
468+
The `cwd` of the command if provided in the [command options](#cwd-1). Otherwise it is `process.cwd()`.
469+
464470
#### message
465471

466472
Type: `string`

‎test/error.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import childProcess from 'node:child_process';
33
import {promisify} from 'node:util';
44
import test from 'ava';
55
import {execa, execaSync} from '../index.js';
6-
import {setFixtureDir} from './helpers/fixtures-dir.js';
6+
import {FIXTURES_DIR, setFixtureDir} from './helpers/fixtures-dir.js';
77

88
const pExec = promisify(childProcess.exec);
99

@@ -212,3 +212,14 @@ test('error.code is defined on failure if applicable', async t => {
212212
const {code} = await t.throwsAsync(execa('noop.js', {cwd: 1}));
213213
t.is(code, 'ERR_INVALID_ARG_TYPE');
214214
});
215+
216+
test('error.cwd is defined on failure if applicable', async t => {
217+
const {cwd} = await t.throwsAsync(execa('noop-throw.js', [], {cwd: FIXTURES_DIR}));
218+
t.is(cwd, FIXTURES_DIR);
219+
});
220+
221+
test('error.cwd is undefined on failure if not passed as options', async t => {
222+
const expectedCwd = process.cwd();
223+
const {cwd} = await t.throwsAsync(execa('noop-throw.js'));
224+
t.is(cwd, expectedCwd);
225+
});

0 commit comments

Comments
 (0)
Please sign in to comment.