summaryrefslogtreecommitdiff
path: root/utils/run.ts
blob: f06ef97c1a366ced5c7a9eedb3f9f006467db935 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
export const getStdout = async (
  cmd: string[] | string,
  options: Deno.CommandOptions = {},
): Promise<string> => {
  const [exec, ...args] = (typeof cmd === "string") ? cmd.split(" ") : cmd;
  const command = new Deno.Command(exec, {
    args,
    stdout: "piped",
    stderr: "piped",
    ...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}`);

  return stdoutText;
};