diff --git a/README.md b/README.md index 2fc882dc81..956d7226db 100644 --- a/README.md +++ b/README.md @@ -218,6 +218,20 @@ if ((await nothrow($`[[ -d path ]]`)).exitCode == 0) { ... } ``` +#### `quiet()` + +Changes behavior of `$` to disable verbose output. + +```ts +function quiet

(p: P): P +``` + +Usage: + +```js +await quiet($`grep something from-file`) +// Command and output will not display +``` ### Packages diff --git a/index.mjs b/index.mjs index cc30a3c51a..2223e8a438 100644 --- a/index.mjs +++ b/index.mjs @@ -46,6 +46,7 @@ export function registerGlobals() { glob, globby, nothrow, + quiet, os, path, question, @@ -75,7 +76,7 @@ export function $(pieces, ...args) { promise._run = () => { if (promise.child) return // The _run() called from two places: then() and setTimeout(). if (promise._prerun) promise._prerun() // In case $1.pipe($2), the $2 returned, and on $2._run() invoke $1._run(). - if (verbose) { + if (verbose && !promise._quiet) { printCmd(cmd) } @@ -100,12 +101,12 @@ export function $(pieces, ...args) { let stdout = '', stderr = '', combined = '' let onStdout = data => { - if (verbose) process.stdout.write(data) + if (verbose && !promise._quiet) process.stdout.write(data) stdout += data combined += data } let onStderr = data => { - if (verbose) process.stderr.write(data) + if (verbose && !promise._quiet) process.stderr.write(data) stderr += data combined += data } @@ -176,9 +177,15 @@ export function nothrow(promise) { return promise } +export function quiet(promise) { + promise._quiet = true + return promise +} + export class ProcessPromise extends Promise { child = undefined _nothrow = false + _quiet = false _resolved = false _inheritStdin = true _piped = false