Skip to content

Commit

Permalink
Merge branch 'main' into feat-iterable-processpromise
Browse files Browse the repository at this point in the history
  • Loading branch information
Pulkit1822 authored Feb 8, 2025
2 parents 681055b + 327dd7a commit f7dc376
Showing 1 changed file with 48 additions and 64 deletions.
112 changes: 48 additions & 64 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,7 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
},
}
)
/**
* State machine stages
*/

type ProcessStage = 'initial' | 'halted' | 'running' | 'fulfilled' | 'rejected'

type Resolve = (out: ProcessOutput) => void
Expand Down Expand Up @@ -312,35 +310,23 @@ export class ProcessPromise extends Promise<ProcessOutput> {
$.log({ kind: 'stderr', data, verbose: !self.isQuiet(), id })
},
end: (data, c) => {
const { error, status, signal, duration, ctx } = data
const { stdout, stderr, stdall } = ctx.store
const dto: ProcessOutputLazyDto = {
code: () => status,
signal: () => signal,
duration: () => duration,
stdout: once(() => bufArrJoin(stdout)),
stderr: once(() => bufArrJoin(stderr)),
stdall: once(() => bufArrJoin(stdall)),
message: once(() => ProcessOutput.getExitMessage(
status,
signal,
dto.stderr(),
self._from
)),
...error && {
code: () => null,
signal: () => null,
message: () => ProcessOutput.getErrorMessage(error, self._from)
}
}
const { error, status, signal, duration, ctx: {store} } = data
const { stdout, stderr } = store
const output = self._output = new ProcessOutput({
code: status,
signal,
error,
duration,
store,
from: self._from,
})

$.log({ kind: 'end', signal, exitCode: status, duration, error, verbose: self.isVerbose(), id })

// Ensures EOL
if (stdout.length && getLast(getLast(stdout)) !== BR_CC) c.on.stdout!(EOL, c)
if (stderr.length && getLast(getLast(stderr)) !== BR_CC) c.on.stderr!(EOL, c)

$.log({ kind: 'end', signal, exitCode: status, duration, error, verbose: self.isVerbose(), id })
const output = self._output = new ProcessOutput(dto)

if (error || status !== 0 && !self.isNothrow()) {
self._stage = 'rejected'
self._reject(output)
Expand Down Expand Up @@ -669,76 +655,72 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}
}

type GettersRecord<T extends Record<any, any>> = { [K in keyof T]: () => T[K] }

type ProcessOutputLazyDto = GettersRecord<{
type ProcessDto = {
code: number | null
signal: NodeJS.Signals | null
duration: number
error: any
from: string
store: TSpawnStore
}

type ProcessOutputDto = ProcessDto & {
stdout: string
stderr: string
stdall: string
message: string
duration: number
}>
}

export class ProcessOutput extends Error {
private readonly _code: number | null = null
private readonly _signal: NodeJS.Signals | null
private readonly _stdout: string
private readonly _stderr: string
private readonly _combined: string
private readonly _duration: number

constructor(dto: ProcessOutputLazyDto)
private readonly _dto: ProcessOutputDto
constructor(dto: ProcessDto)
constructor(
code: number | null,
signal: NodeJS.Signals | null,
stdout: string,
stderr: string,
combined: string,
stdall: string,
message: string,
duration?: number
)
constructor(
code: number | null | ProcessOutputLazyDto,
code: number | null | ProcessDto,
signal: NodeJS.Signals | null = null,
stdout: string = '',
stderr: string = '',
combined: string = '',
stdall: string = '',
message: string = '',
duration: number = 0
) {
super(message)
this._signal = signal
this._stdout = stdout
this._stderr = stderr
this._combined = combined
this._duration = duration
if (code !== null && typeof code === 'object') {
Object.defineProperties(this, {
_code: { get: code.code },
_signal: { get: code.signal },
_duration: { get: code.duration },
_stdout: { get: code.stdout },
_stderr: { get: code.stderr },
_combined: { get: code.stdall },
message: { get: code.message },
})
} else {
this._code = code
}
Reflect.deleteProperty(this, 'message')
this._dto =
code !== null && typeof code === 'object'
? ProcessOutput.createLazyDto(code)
: {
code,
signal,
duration,
stdout,
stderr,
stdall,
error: null,
from: '',
message,
store: { stdout: [], stderr: [], stdall: [] },
}
}

toString(): string {
return this._combined
return this._dto.stdall
}

json<T = any>(): T {
return JSON.parse(this._combined)
return JSON.parse(this._dto.stdall)
}

buffer(): Buffer {
return Buffer.from(this._combined)
return Buffer.from(this._dto.stdall)
}

blob(type = 'text/plain'): Blob {
Expand All @@ -756,6 +738,7 @@ export class ProcessOutput extends Error {
}

lines(): string[] {

return [...this]
}

Expand Down Expand Up @@ -793,6 +776,7 @@ export class ProcessOutput extends Error {
for (let i = first; i <= last; i++) {
yield lines[i];
}

}

static getExitMessage = formatExitMessage
Expand Down

0 comments on commit f7dc376

Please sign in to comment.