Skip to content

Commit 24e9861

Browse files
authored
feat: add exitCode to output (#38)
1 parent f11e42b commit 24e9861

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@ const result = await x('ls', ['-l']);
2222

2323
// result.stdout - the stdout as a string
2424
// result.stderr - the stderr as a string
25+
// result.exitCode - the process exit code as a number
2526
```
2627

2728
You may also iterate over the lines of output via an async loop:
2829

2930
```ts
3031
import {x} from 'tinyexec';
3132

32-
const result = x('ls', ['-l']);
33+
const proc = x('ls', ['-l']);
3334

34-
for await (const line of result) {
35+
for await (const line of proc) {
3536
// line will be from stderr/stdout in the order you'd see it in a term
3637
}
3738
```

src/main.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export {NonZeroExitError};
1313
export interface Output {
1414
stderr: string;
1515
stdout: string;
16+
exitCode: number | undefined;
1617
}
1718

1819
export interface PipeOptions extends Options {}
@@ -238,7 +239,8 @@ export class ExecProcess implements Result {
238239

239240
const result: Output = {
240241
stderr,
241-
stdout
242+
stdout,
243+
exitCode: this.exitCode
242244
};
243245

244246
if (

src/test/main_test.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ test('exec', async (t) => {
1515
await t.test('exitCode is set correctly', async () => {
1616
const proc = x('echo', ['foo']);
1717
assert.equal(proc.exitCode, undefined);
18-
await proc;
18+
const result = await proc;
1919
assert.equal(proc.exitCode, 0);
20+
assert.equal(result.exitCode, 0);
2021
});
2122

2223
await t.test('non-zero exitCode throws when throwOnError=true', async () => {
@@ -107,6 +108,7 @@ if (isWindows) {
107108

108109
assert.equal(result.stderr, '');
109110
assert.equal(result.stdout, 'foo\n');
111+
assert.equal(result.exitCode, 0);
110112
assert.equal(echoProc.exitCode, 0);
111113
assert.equal(grepProc.exitCode, 0);
112114
});
@@ -144,7 +146,7 @@ if (isWindows) {
144146
if (!isWindows) {
145147
test('exec (unix-like)', async (t) => {
146148
await t.test('times out after defined timeout (ms)', async () => {
147-
const proc = x('sleep', ['0.2s'], {timeout: 100});
149+
const proc = x('sleep', ['0.2'], {timeout: 100});
148150
await assert.rejects(async () => {
149151
await proc;
150152
});
@@ -160,7 +162,7 @@ if (!isWindows) {
160162
});
161163

162164
await t.test('kill terminates the process', async () => {
163-
const proc = x('sleep', ['5s']);
165+
const proc = x('sleep', ['5']);
164166
const result = proc.kill();
165167
assert.ok(result);
166168
assert.ok(proc.killed);
@@ -174,13 +176,14 @@ if (!isWindows) {
174176

175177
assert.equal(result.stderr, '');
176178
assert.equal(result.stdout, 'foo\n');
179+
assert.equal(result.exitCode, 0);
177180
assert.equal(echoProc.exitCode, 0);
178181
assert.equal(grepProc.exitCode, 0);
179182
});
180183

181184
await t.test('signal can be used to abort execution', async () => {
182185
const controller = new AbortController();
183-
const proc = x('sleep', ['4s'], {signal: controller.signal});
186+
const proc = x('sleep', ['4'], {signal: controller.signal});
184187
controller.abort();
185188
const result = await proc;
186189
assert.ok(proc.aborted);

0 commit comments

Comments
 (0)