Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose ProcessPromise fullCmd and unique id #1035

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{
"name": "dts libdefs",
"path": "build/*.d.ts",
"limit": "37.1 kB",
"limit": "37.5 kB",
"brotli": false,
"gzip": false
},
Expand Down
45 changes: 27 additions & 18 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import {
quote,
quotePowerShell,
toCamelCase,
randomId,
} from './util.js'

export { log, type LogEntry } from './util.js'
Expand Down Expand Up @@ -209,6 +210,7 @@ type PipeMethod = {
}

export class ProcessPromise extends Promise<ProcessOutput> {
private _id = randomId()
private _command = ''
private _from = ''
private _snapshot = getStore()
Expand Down Expand Up @@ -251,9 +253,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
this._run = true
this._pipedFrom?.run()

const $ = this._snapshot
const self = this
const input = ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input
const $ = this._snapshot

if ($.timeout) this.timeout($.timeout, $.timeoutSignal)
if ($.preferLocal) {
Expand All @@ -268,22 +269,24 @@ export class ProcessPromise extends Promise<ProcessOutput> {
verbose: self.isVerbose(),
})

// prettier-ignore
this._zurk = exec({
input,
cmd: $.prefix + self._command + $.postfix,
cwd: $.cwd ?? $[CWD],
ac: $.ac,
signal: $.signal,
shell: isString($.shell) ? $.shell : true,
env: $.env,
spawn: $.spawn,
spawnSync: $.spawnSync,
store: $.store,
stdin: self._stdin,
stdio: self._stdio ?? $.stdio,
sync: $[SYNC],
id: self.id,
cmd: self.fullCmd,
cwd: $.cwd ?? $[CWD],
input: ($.input as ProcessPromise | ProcessOutput)?.stdout ?? $.input,
ac: $.ac,
signal: $.signal,
shell: isString($.shell) ? $.shell : true,
env: $.env,
spawn: $.spawn,
spawnSync:$.spawnSync,
store: $.store,
stdin: self._stdin,
stdio: self._stdio ?? $.stdio,
sync: $[SYNC],
detached: $.detached,
ee: self._ee,
ee: self._ee,
run: (cb) => cb(),
on: {
start: () => {
Expand All @@ -298,13 +301,11 @@ export class ProcessPromise extends Promise<ProcessOutput> {
// Stderr should be printed regardless of piping.
$.log({ kind: 'stderr', data, verbose: !self.isQuiet() })
},
// prettier-ignore
end: (data, c) => {
self._resolved = true
const { error, status, signal, duration, ctx } = data
const { stdout, stderr, stdall } = ctx.store
const dto: ProcessOutputLazyDto = {
// Lazy getters
code: () => status,
signal: () => signal,
duration: () => duration,
Expand Down Expand Up @@ -439,6 +440,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}

// Getters
get id() {
return this._id
}

get pid(): number | undefined {
return this.child?.pid
}
Expand All @@ -447,6 +452,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return this._command
}

get fullCmd(): string {
return this._snapshot.prefix + this.cmd + this._snapshot.postfix
}

get child(): ChildProcess | undefined {
return this._zurk?.child
}
Expand Down
4 changes: 3 additions & 1 deletion test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,13 @@ describe('core', () => {
const baz = 1
const p = $`echo ${foo} --t ${baz}`
assert.equal(p.cmd, "echo $'#bar' --t 1")
assert.equal(p.fullCmd, "set -euo pipefail;echo $'#bar' --t 1")
})

test('exposes pid', () => {
test('exposes pid & id', () => {
const p = $`echo foo`
assert.ok(p.pid > 0)
assert.ok(typeof p.id === 'string')
})

test('stdio() works', async () => {
Expand Down
Loading