1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
import { Either } from "./mod.ts";
export class ProcessError extends Error {}
export const getStdout = async (
cmd: string[] | string,
options: Deno.CommandOptions = {},
): Promise<Either<ProcessError, string>> => {
const [exec, ...args] = (typeof cmd === "string") ? cmd.split(" ") : cmd;
const command = new Deno.Command(exec, {
args,
stdout: "piped",
stderr: "piped",
...options,
});
try {
const { code, stdout, stderr } = await command.output();
const stdoutText = new TextDecoder().decode(stdout);
const stderrText = new TextDecoder().decode(stderr);
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);
}
};
|