From 24e98613798c024ccd7742f538e0958ebe6efd2d Mon Sep 17 00:00:00 2001 From: Ivan Tanev Date: Tue, 15 Oct 2024 14:12:41 +0300 Subject: [PATCH] feat: add exitCode to output (#38) --- README.md | 5 +++-- src/main.ts | 4 +++- src/test/main_test.ts | 11 +++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 92d7b1a..dc17106 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ const result = await x('ls', ['-l']); // result.stdout - the stdout as a string // result.stderr - the stderr as a string +// result.exitCode - the process exit code as a number ``` You may also iterate over the lines of output via an async loop: @@ -29,9 +30,9 @@ You may also iterate over the lines of output via an async loop: ```ts import {x} from 'tinyexec'; -const result = x('ls', ['-l']); +const proc = x('ls', ['-l']); -for await (const line of result) { +for await (const line of proc) { // line will be from stderr/stdout in the order you'd see it in a term } ``` diff --git a/src/main.ts b/src/main.ts index 685db51..f6fc0d5 100644 --- a/src/main.ts +++ b/src/main.ts @@ -13,6 +13,7 @@ export {NonZeroExitError}; export interface Output { stderr: string; stdout: string; + exitCode: number | undefined; } export interface PipeOptions extends Options {} @@ -238,7 +239,8 @@ export class ExecProcess implements Result { const result: Output = { stderr, - stdout + stdout, + exitCode: this.exitCode }; if ( diff --git a/src/test/main_test.ts b/src/test/main_test.ts index 9f4fc96..14e8d9a 100644 --- a/src/test/main_test.ts +++ b/src/test/main_test.ts @@ -15,8 +15,9 @@ test('exec', async (t) => { await t.test('exitCode is set correctly', async () => { const proc = x('echo', ['foo']); assert.equal(proc.exitCode, undefined); - await proc; + const result = await proc; assert.equal(proc.exitCode, 0); + assert.equal(result.exitCode, 0); }); await t.test('non-zero exitCode throws when throwOnError=true', async () => { @@ -107,6 +108,7 @@ if (isWindows) { assert.equal(result.stderr, ''); assert.equal(result.stdout, 'foo\n'); + assert.equal(result.exitCode, 0); assert.equal(echoProc.exitCode, 0); assert.equal(grepProc.exitCode, 0); }); @@ -144,7 +146,7 @@ if (isWindows) { if (!isWindows) { test('exec (unix-like)', async (t) => { await t.test('times out after defined timeout (ms)', async () => { - const proc = x('sleep', ['0.2s'], {timeout: 100}); + const proc = x('sleep', ['0.2'], {timeout: 100}); await assert.rejects(async () => { await proc; }); @@ -160,7 +162,7 @@ if (!isWindows) { }); await t.test('kill terminates the process', async () => { - const proc = x('sleep', ['5s']); + const proc = x('sleep', ['5']); const result = proc.kill(); assert.ok(result); assert.ok(proc.killed); @@ -174,13 +176,14 @@ if (!isWindows) { assert.equal(result.stderr, ''); assert.equal(result.stdout, 'foo\n'); + assert.equal(result.exitCode, 0); assert.equal(echoProc.exitCode, 0); assert.equal(grepProc.exitCode, 0); }); await t.test('signal can be used to abort execution', async () => { const controller = new AbortController(); - const proc = x('sleep', ['4s'], {signal: controller.signal}); + const proc = x('sleep', ['4'], {signal: controller.signal}); controller.abort(); const result = await proc; assert.ok(proc.aborted);