Skip to content

Commit

Permalink
feat: add exitCode to output (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
VanTanev authored Oct 15, 2024
1 parent f11e42b commit 24e9861
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ 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:

```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
}
```
Expand Down
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {NonZeroExitError};
export interface Output {
stderr: string;
stdout: string;
exitCode: number | undefined;
}

export interface PipeOptions extends Options {}
Expand Down Expand Up @@ -238,7 +239,8 @@ export class ExecProcess implements Result {

const result: Output = {
stderr,
stdout
stdout,
exitCode: this.exitCode
};

if (
Expand Down
11 changes: 7 additions & 4 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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;
});
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 24e9861

Please sign in to comment.