From e49fda41176d025a671802be76c219d66167276f Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Mon, 12 May 2025 23:05:27 -0700 Subject: snapshot --- utils/run.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'utils/run.ts') diff --git a/utils/run.ts b/utils/run.ts index 60ae1e6..06e7d9f 100644 --- a/utils/run.ts +++ b/utils/run.ts @@ -1,7 +1,10 @@ +import { Either } from "./mod.ts"; + +export class ProcessError extends Error {} export const getStdout = async ( cmd: string[] | string, options: Deno.CommandOptions = {}, -): Promise => { +): Promise> => { const [exec, ...args] = (typeof cmd === "string") ? cmd.split(" ") : cmd; const command = new Deno.Command(exec, { args, @@ -10,12 +13,21 @@ export const getStdout = async ( ...options, }); - const { code, stdout, stderr } = await command.output(); - - const stdoutText = new TextDecoder().decode(stdout); - const stderrText = new TextDecoder().decode(stderr); - - if (code !== 0) throw new Error(`command failed\n${stderrText}`); + try { + const { code, stdout, stderr } = await command.output(); + const stdoutText = new TextDecoder().decode(stdout); + const stderrText = new TextDecoder().decode(stderr); - return stdoutText; + if (code !== 0) { + return Either.left( + new ProcessError(`command failed\n${stderrText}`), + ); + } + return Either.right(stdoutText); + } catch (e) { + if (e instanceof Error) { + return Either.left(e); + } + throw new Error("unknown error " + e); + } }; -- cgit v1.2.3-70-g09d2