Skip to content

Commit

Permalink
feat(runtime): add noThrow property (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar authored Jul 10, 2022
1 parent 66d9c6a commit dcaa0e8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/runtime/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class Process implements Promise<ProcessOutput> {
#baseError: ProcessError;
#maxRetries = 0;
#retries = 0;
#throwErrors = true;

constructor(cmd: string, { errorContext }: ProcessOptions = {}) {
this.#cmd = cmd;
Expand Down Expand Up @@ -50,6 +51,11 @@ export class Process implements Promise<ProcessOutput> {
return this.#process.pid;
}

get noThrow(): this {
this.#throwErrors = false;
return this;
}

retry(retries: number): this {
this.#maxRetries = retries;
return this;
Expand Down Expand Up @@ -117,21 +123,31 @@ export class Process implements Promise<ProcessOutput> {
});

if (!status.success) {
throw ProcessError.merge(
const error = ProcessError.merge(
this.#baseError,
new ProcessError(output),
);

if (this.#throwErrors) {
throw error;
}
this.#close();

return error;
}
this.#close();

return output;
} catch (error) {
this.#close();

if (this.#retries < this.#maxRetries) {
this.#retries++;
this.#proc = null;

return this.#run();
}

throw error;
}
}
Expand Down
6 changes: 6 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,9 @@ Deno.test("markdown files can be executed as scripts", async () => {
assertStringIncludes(output.stdout, `$ echo "Hello World!"`);
assertStringIncludes(output.stdout, `$ echo "Hello again World!"`);
});

Deno.test("$ should not throw with noThrow", async () => {
const result = await $`exit 1`.noThrow;

assertEquals(result.status.code, 1);
});

0 comments on commit dcaa0e8

Please sign in to comment.