Skip to content

Commit

Permalink
feat: provide signal opt
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Apr 6, 2024
1 parent df274d7 commit 5ef1675
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/ts/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface TSpawnCtxNormalized {
ee: EventEmitter
on: Partial<TSpawnListeners>
ac: AbortController
signal: AbortController['signal']
shell: string | true | undefined
spawn: typeof cp.spawn
spawnSync: typeof cp.spawnSync
Expand All @@ -73,6 +74,7 @@ export const normalizeCtx = (...ctxs: TSpawnCtx[]): TSpawnCtxNormalized => assig
env: process.env,
ee: new EventEmitter(),
ac: new AbortController(),
get signal() { return this.ac.signal },
on: {},
detached: process.platform !== 'win32',
shell: true,
Expand Down Expand Up @@ -105,7 +107,7 @@ export class VoidWritable extends Transform {
}
}

export const buildSpawnOpts = ({spawnOpts, stdio, cwd, shell, input, env, detached, ac: {signal}}: TSpawnCtxNormalized) => ({
export const buildSpawnOpts = ({spawnOpts, stdio, cwd, shell, input, env, detached, signal}: TSpawnCtxNormalized) => ({
...spawnOpts,
env,
cwd,
Expand Down
3 changes: 3 additions & 0 deletions src/test/ts/spawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ describe('invoke()', () => {

describe('normalizeCtx()', () => {
it('normalizes ctx', () => {
const signal = new AbortController().signal
const cwds = ['a', 'b', 'c']
const ctx = {
cmd: 'foo',
signal,
get cwd () {
return cwds.shift() || process.cwd()
},
Expand All @@ -62,6 +64,7 @@ describe('normalizeCtx()', () => {
assert.equal(normalized.cwd, 'a')
assert.equal(normalized.cwd, 'b')
assert.equal(normalized.cwd, 'c')
assert.equal(normalized.signal, signal)
assert.ok(normalized.ee instanceof EventEmitter)
assert.ok(normalized.ac instanceof AbortController)
})
Expand Down

0 comments on commit 5ef1675

Please sign in to comment.