Skip to content
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
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);
});