Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exitCode to awaited result / Output type #38

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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