Skip to content

Instantly share code, notes, and snippets.

@zandaqo

zandaqo/index.ts Secret

Created May 7, 2021 08:49
Show Gist options
  • Save zandaqo/93004fb265146a95aadb28ec851a1d72 to your computer and use it in GitHub Desktop.
Save zandaqo/93004fb265146a95aadb28ec851a1d72 to your computer and use it in GitHub Desktop.
Deno CLI Template Literal
import { $ } from "./template.ts";
const p = await $`cmd /c echo hello`;
console.log(new TextDecoder().decode(p));
//=> hello
export async function $(pieces: TemplateStringsArray, ...args: Array<unknown>) {
let compiled = pieces[0], i = 0;
for (; i < args.length; i++) compiled += args[i] + pieces[i + 1];
for (++i; i < pieces.length; i++) compiled += pieces[i];
const cmd = compiled.split(/\s+/);
const process = Deno.run({ cmd, stdout: "piped", stderr: "piped" });
const { code } = await process.status();
if (code === 0) {
return await process.output();
} else {
return await process.stderrOutput();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment