RoXuSR
Denoβ€’3y agoβ€’
6 replies
RoXuS

Usage of Deno.Command?

Hey guys we use Deno.run to launch shell cmd and pipe the result to log.
But Deno.run will be deprecated and I am not able to write a good equivalent with Deno.Command, do you have a suggestion?

Here is my simple Deno.run:

import * as log from "https://deno.land/std@0.204.0/log/mod.ts";

export default async (
  cmd: string,
  args: Array<string>,
  cwd?: string,
  dry = false,
) => {
  const logger = log.getLogger();
  if (dry) {
    logger.debug([`dry ${cwd} ${cmd}`, ...args].join(" "));
    return Promise.resolve("");
  }
  logger.debug([cwd, cmd, ...args].join(" "));
  const p = Deno.run({
    cmd: [cmd, ...args],
    cwd,
    stdout: "piped",
    stderr: "piped",
  });

  const [stderr, stdout, status] = await Promise.all([
    p.stderrOutput(),
    p.output(),
    p.status(),
  ]);

  p.close();

  if (status.code === 0) {
    return new TextDecoder().decode(stdout);
  } else {
    const errorString = new TextDecoder().decode(stderr);
    throw new Error(
      `\n${Deno.cwd()} ${cwd} ${cmd} ${args} ${errorString}`,
    );
  }
};
Was this page helpful?