Skip to content

Commit

Permalink
Quiet commands on a per invocation basis
Browse files Browse the repository at this point in the history
This adds another wrapper to disable verbose output on a specific command
  • Loading branch information
Whoaa512 committed Feb 24, 2022
1 parent 6e5bebb commit 12a347e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -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): P
```

Usage:

```js
await quiet($`grep something from-file`)
// Command and output will not display
```

### Packages

Expand Down
13 changes: 10 additions & 3 deletions index.mjs
Expand Up @@ -46,6 +46,7 @@ export function registerGlobals() {
glob,
globby,
nothrow,
quiet,
os,
path,
question,
Expand Down Expand Up @@ -75,7 +76,7 @@ export function $(pieces, ...args) {
promise._run = () => {
if (promise.child) return
if (promise._prerun) promise._prerun()
if (verbose) {
if (verbose && !promise._quiet) {
printCmd(cmd)
}

Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 12a347e

Please sign in to comment.