Skip to content

Commit

Permalink
Add exitCode to Output type
Browse files Browse the repository at this point in the history
  • Loading branch information
VanTanev committed Oct 14, 2024
1 parent 3d0653c commit aac2178
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 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;
}

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
5 changes: 4 additions & 1 deletion 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 @@ -174,6 +176,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

0 comments on commit aac2178

Please sign in to comment.