summaryrefslogtreecommitdiff
path: root/utils/run.ts
diff options
context:
space:
mode:
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);
+ }
};