summaryrefslogtreecommitdiff
path: root/utils/run.ts
diff options
context:
space:
mode:
authorElizabeth Alexander Hunt <me@liz.coffee>2025-05-12 23:05:27 -0700
committerElizabeth Alexander Hunt <me@liz.coffee>2025-05-12 23:33:52 -0700
commite49fda41176d025a671802be76c219d66167276f (patch)
tree6791f0ffe26b2405e13436a7e0c8d05d60e2b3bc /utils/run.ts
parentbbaea13ee7125a9d289a74f0c173e7e75177e53c (diff)
downloadci-e49fda41176d025a671802be76c219d66167276f.tar.gz
ci-e49fda41176d025a671802be76c219d66167276f.zip
snapshot
Diffstat (limited to 'utils/run.ts')
-rw-r--r--utils/run.ts28
1 files changed, 20 insertions, 8 deletions
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<string> => {
+): Promise<Either<ProcessError, string>> => {
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<ProcessError, string>(
+ new ProcessError(`command failed\n${stderrText}`),
+ );
+ }
+ return Either.right<ProcessError, string>(stdoutText);
+ } catch (e) {
+ if (e instanceof Error) {
+ return Either.left<ProcessError, string>(e);
+ }
+ throw new Error("unknown error " + e);
+ }
};