From 1384a8c6fc5695d796754cc58ddef8e8c69b792c Mon Sep 17 00:00:00 2001 From: Anton Golub Date: Thu, 3 Oct 2024 10:52:42 +0300 Subject: [PATCH] style: group `ProcessPromise` methods (#913) Co-authored-by: Anton Medvedev --- src/core.ts | 220 +++++++++++++++++++++++++++------------------------- src/deps.ts | 2 +- 2 files changed, 114 insertions(+), 108 deletions(-) diff --git a/src/core.ts b/src/core.ts index 5ad895e486..ea5048cfad 100644 --- a/src/core.ts +++ b/src/core.ts @@ -188,20 +188,20 @@ type Resolve = (out: ProcessOutput) => void export class ProcessPromise extends Promise { private _command = '' private _from = '' - private _resolve: Resolve = noop - private _reject: Resolve = noop private _snapshot = getStore() private _stdio?: StdioOptions private _nothrow?: boolean private _quiet?: boolean private _verbose?: boolean private _timeout?: number - private _timeoutSignal = $.timeoutSignal + private _timeoutSignal?: NodeJS.Signals private _resolved = false private _halted = false private _piped = false private _zurk: ReturnType | null = null private _output: ProcessOutput | null = null + private _reject: Resolve = noop + private _resolve: Resolve = noop _prerun = noop _postrun = noop @@ -322,91 +322,7 @@ export class ProcessPromise extends Promise { return this } - get cmd() { - return this._command - } - - get child() { - return this._zurk?.child - } - - get stdin(): Writable { - this.stdio('pipe') - this.run() - assert(this.child) - if (this.child.stdin == null) - throw new Error('The stdin of subprocess is null.') - return this.child.stdin - } - - get stdout(): Readable { - this.run() - assert(this.child) - if (this.child.stdout == null) - throw new Error('The stdout of subprocess is null.') - return this.child.stdout - } - - get stderr(): Readable { - this.run() - assert(this.child) - if (this.child.stderr == null) - throw new Error('The stderr of subprocess is null.') - return this.child.stderr - } - - get exitCode(): Promise { - return this.then( - (p) => p.exitCode, - (p) => p.exitCode - ) - } - - json(): Promise { - return this.then((p) => p.json()) - } - - text(encoding?: Encoding): Promise { - return this.then((p) => p.text(encoding)) - } - - lines(): Promise { - return this.then((p) => p.lines()) - } - - buffer(): Promise { - return this.then((p) => p.buffer()) - } - - blob(type?: string): Promise { - return this.then((p) => p.blob(type)) - } - - then( - onfulfilled?: - | ((value: ProcessOutput) => PromiseLike | R) - | undefined - | null, - onrejected?: - | ((reason: ProcessOutput) => PromiseLike | E) - | undefined - | null - ): Promise { - if (this.isHalted() && !this.child) { - throw new Error('The process is halted!') - } - return super.then(onfulfilled, onrejected) - } - - catch( - onrejected?: - | ((reason: ProcessOutput) => PromiseLike | T) - | undefined - | null - ): Promise { - return super.catch(onrejected) - } - + // Essentials pipe( dest: Writable | ProcessPromise | TemplateStringsArray, ...args: any[] @@ -450,11 +366,7 @@ export class ProcessPromise extends Promise { this._zurk?.ac.abort(reason) } - get signal() { - return this._snapshot.signal || this._snapshot.ac?.signal - } - - async kill(signal = $.killSignal): Promise { + kill(signal = $.killSignal): Promise { if (!this.child) throw new Error('Trying to kill a process without creating one.') if (!this.child.pid) throw new Error('The process pid is undefined.') @@ -462,6 +374,56 @@ export class ProcessPromise extends Promise { return $.kill(this.child.pid, signal) } + // Getters + get cmd() { + return this._command + } + + get child() { + return this._zurk?.child + } + + get stdin(): Writable { + this.stdio('pipe') + this.run() + assert(this.child) + if (this.child.stdin == null) + throw new Error('The stdin of subprocess is null.') + return this.child.stdin + } + + get stdout(): Readable { + this.run() + assert(this.child) + if (this.child.stdout == null) + throw new Error('The stdout of subprocess is null.') + return this.child.stdout + } + + get stderr(): Readable { + this.run() + assert(this.child) + if (this.child.stderr == null) + throw new Error('The stderr of subprocess is null.') + return this.child.stderr + } + + get exitCode(): Promise { + return this.then( + (p) => p.exitCode, + (p) => p.exitCode + ) + } + + get signal() { + return this._snapshot.signal || this._snapshot.ac?.signal + } + + get output() { + return this._output + } + + // Configurators stdio( stdin: IOType, stdout: IOType = 'pipe', @@ -486,18 +448,6 @@ export class ProcessPromise extends Promise { return this } - isQuiet(): boolean { - return this._quiet ?? this._snapshot.quiet - } - - isVerbose(): boolean { - return (this._verbose ?? this._snapshot.verbose) && !this.isQuiet() - } - - isNothrow(): boolean { - return this._nothrow ?? this._snapshot.nothrow - } - timeout(d: Duration, signal = $.timeoutSignal): ProcessPromise { this._timeout = parseDuration(d) this._timeoutSignal = signal @@ -509,12 +459,68 @@ export class ProcessPromise extends Promise { return this } + // Output formatters + json(): Promise { + return this.then((p) => p.json()) + } + + text(encoding?: Encoding): Promise { + return this.then((p) => p.text(encoding)) + } + + lines(): Promise { + return this.then((p) => p.lines()) + } + + buffer(): Promise { + return this.then((p) => p.buffer()) + } + + blob(type?: string): Promise { + return this.then((p) => p.blob(type)) + } + + // Status checkers isHalted(): boolean { return this._halted } - get output() { - return this._output + isQuiet(): boolean { + return this._quiet ?? this._snapshot.quiet + } + + isVerbose(): boolean { + return (this._verbose ?? this._snapshot.verbose) && !this.isQuiet() + } + + isNothrow(): boolean { + return this._nothrow ?? this._snapshot.nothrow + } + + // Promise API + then( + onfulfilled?: + | ((value: ProcessOutput) => PromiseLike | R) + | undefined + | null, + onrejected?: + | ((reason: ProcessOutput) => PromiseLike | E) + | undefined + | null + ): Promise { + if (this.isHalted() && !this.child) { + throw new Error('The process is halted!') + } + return super.then(onfulfilled, onrejected) + } + + catch( + onrejected?: + | ((reason: ProcessOutput) => PromiseLike | T) + | undefined + | null + ): Promise { + return super.catch(onrejected) } } diff --git a/src/deps.ts b/src/deps.ts index 186b93da66..d506713d79 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -20,10 +20,10 @@ export async function installDeps( dependencies: Record, prefix?: string ) { + const flags = prefix ? `--prefix=${prefix}` : '' const packages = Object.entries(dependencies).map( ([name, version]) => `${name}@${version}` ) - const flags = prefix ? `--prefix=${prefix}` : '' if (packages.length === 0) { return }