Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c557891

Browse files
committedMar 13, 2024·
feat: process quiet as preset option
1 parent 52822cc commit c557891

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed
 

‎src/core.ts

+19-8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface Options {
5555
nothrow: boolean
5656
prefix: string
5757
quote: typeof quote
58+
quiet: boolean
5859
spawn: typeof spawn
5960
log: typeof log
6061
}
@@ -75,6 +76,7 @@ export const defaults: Options = {
7576
env: process.env,
7677
shell: true,
7778
nothrow: false,
79+
quiet: false,
7880
prefix: '',
7981
quote: () => {
8082
throw new Error('No quote function is defined: https://ï.at/no-quote-func')
@@ -161,7 +163,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
161163
private _snapshot = getStore()
162164
private _stdio: [IO, IO, IO] = ['inherit', 'pipe', 'pipe']
163165
private _nothrow?: boolean
164-
private _quiet = false
166+
private _quiet?: boolean
165167
private _timeout?: number
166168
private _timeoutSignal?: string
167169
private _resolved = false
@@ -194,7 +196,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
194196
$.log({
195197
kind: 'cmd',
196198
cmd: this._command,
197-
verbose: $.verbose && !this._quiet,
199+
verbose: self.isVerbose(),
198200
})
199201

200202
this._zurk = zurk$({
@@ -210,10 +212,13 @@ export class ProcessPromise extends Promise<ProcessOutput> {
210212
nohandle: true,
211213
detached: !isWin,
212214
onStdout(data: any) {
213-
$.log({ kind: 'stdout', data, verbose: $.verbose && !self._quiet })
215+
// If process is piped, don't print output.
216+
if (self._piped) return
217+
$.log({ kind: 'stdout', data, verbose: self.isVerbose() })
214218
},
215219
onStderr(data: any) {
216-
$.log({ kind: 'stderr', data, verbose: $.verbose && !self._quiet })
220+
// Stderr should be printed regardless of piping.
221+
$.log({ kind: 'stderr', data, verbose: self.isVerbose() })
217222
},
218223
run: (cb) => cb(),
219224
timeout: self._timeout,
@@ -232,7 +237,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
232237
new ProcessOutput(null, null, stdout, stderr, stdall, message)
233238
)
234239
} else {
235-
const message = ProcessOutput.getMessage(
240+
const message = ProcessOutput.getExitMessage(
236241
status,
237242
signal,
238243
stderr,
@@ -255,8 +260,6 @@ export class ProcessPromise extends Promise<ProcessOutput> {
255260
}
256261
})
257262

258-
// if (!this._piped) this.child.stdout?.on('data', onStdout) // If process is piped, don't collect or print output.
259-
// this.child.stderr?.on('data', onStderr) // Stderr should be printed regardless of piping.
260263
this._postrun() // In case $1.pipe($2), after both subprocesses are running, we can pipe $1.stdout to $2.stdin.
261264

262265
return this
@@ -352,6 +355,9 @@ export class ProcessPromise extends Promise<ProcessOutput> {
352355
if (!this.child.pid) throw new Error('The process pid is undefined.')
353356

354357
await this._zurk?.kill(signal as NodeJS.Signals)
358+
// zurk uses detached + process.kill(-p.pid)
359+
// Do we still need this?
360+
355361
// let children = await psTree(this.child.pid)
356362
// for (const p of children) {
357363
// try {
@@ -378,6 +384,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
378384
return this
379385
}
380386

387+
isVerbose(): boolean {
388+
const { verbose, quiet } = this._snapshot
389+
return verbose && !(this._quiet ?? quiet)
390+
}
391+
381392
timeout(d: Duration, signal = 'SIGTERM'): ProcessPromise {
382393
this._timeout = parseDuration(d)
383394
this._timeoutSignal = signal
@@ -437,7 +448,7 @@ export class ProcessOutput extends Error {
437448
return this._signal
438449
}
439450

440-
static getMessage(
451+
static getExitMessage(
441452
code: number | null,
442453
signal: NodeJS.Signals | null,
443454
stderr: string,

0 commit comments

Comments
 (0)
Please sign in to comment.