-
-
Save zandaqo/93004fb265146a95aadb28ec851a1d72 to your computer and use it in GitHub Desktop.
Deno CLI Template Literal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { $ } from "./template.ts"; | |
const p = await $`cmd /c echo hello`; | |
console.log(new TextDecoder().decode(p)); | |
//=> hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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