Skip to content

Commit

Permalink
chore: relax TSpawnStore type
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Jun 12, 2024
1 parent b1e98de commit 40207b6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ const getFixedSizeArray = (size: number) => {
const store: TSpawnStore = {
stdout: getFixedSizeArray(1),
stderr: getFixedSizeArray(2),
stdall: getFixedSizeArray(0),
getStdout() { return this.stdout.join('') },
getStderr() { return this.stdout.join('') },
getStdall() { return '' },
stdall: getFixedSizeArray(0)
}

const result = await $({store})`echo hello`
Expand Down
41 changes: 19 additions & 22 deletions src/main/ts/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ export * from './util.js'

export type TSpawnError = any

export type TPushable<T = any> = { push(...args: T[]): number }

export type TJoinable = { join(sep?: string): string }

export type TSpawnStoreChunks = Iterable<string| Buffer> & TPushable<string| Buffer> & TJoinable

export type TSpawnStore = {
stdout: Array<string | Buffer>
stderr: Array<string | Buffer>
stdall: Array<string | Buffer>
getStdout: () => string
getStderr: () => string
getStdall: () => string
stdout: TSpawnStoreChunks
stderr: TSpawnStoreChunks
stdall: TSpawnStoreChunks
}

export type TSpawnResult = {
Expand Down Expand Up @@ -136,17 +139,11 @@ export const attachListeners = (ee: EventEmitter, on: Partial<TSpawnListeners> =
}
}

export const createStore = (): TSpawnStore => {
const store: TSpawnStore = {
stdout: [],
stderr: [],
stdall: [],
getStdout() { return store.stdout.join('') },
getStderr() { return store.stderr.join('') },
getStdall() { return store.stdall.join('') }
}
return store
}
export const createStore = (): TSpawnStore => ({
stdout: [],
stderr: [],
stdall: [],
})

// eslint-disable-next-line sonarjs/cognitive-complexity
export const invoke = (c: TSpawnCtxNormalized): TSpawnCtxNormalized => {
Expand All @@ -171,8 +168,8 @@ export const invoke = (c: TSpawnCtxNormalized): TSpawnCtxNormalized => {
}
c.callback(null, c.fulfilled = {
...result,
get stdout() { return c.store.getStdout() },
get stderr() { return c.store.getStderr() },
get stdout() { return c.store.stdout.join('') },
get stderr() { return c.store.stderr.join('') },
stdio,
get stdall() { return this.stdout + this.stderr },
duration: Date.now() - now,
Expand Down Expand Up @@ -224,9 +221,9 @@ export const invoke = (c: TSpawnCtxNormalized): TSpawnCtxNormalized => {
error,
status,
signal,
get stdout() { return c.store.getStdout() },
get stderr() { return c.store.getStderr() },
get stdall() { return c.store.getStdall() },
get stdout() { return c.store.stdout.join('') },
get stderr() { return c.store.stderr.join('') },
get stdall() { return c.store.stdall.join('') },
stdio: [c.stdin, c.stdout, c.stderr],
duration: Date.now() - now,
ctx: c
Expand Down
10 changes: 4 additions & 6 deletions src/test/ts/spawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,15 @@ describe('exec()', () => {
const store: TSpawnStore = {
stdout: getFixedSizeArray(1),
stderr: getFixedSizeArray(2),
stdall: getFixedSizeArray(0),
getStdout() { return store.stdout.join('')},
getStderr() { return store.stderr.join('')},
getStdall() { return store.stdall.join('')},
stdall: getFixedSizeArray(0)
}

const ctx = exec({sync: false, callback, store, cmd: 'echo', args: ['hello']})
const result = await promise

assert.equal(ctx.store.getStdall(), '')
assert.equal(ctx.store.getStdout().trim(), 'hello')
assert.equal(ctx.store.stdall.join(''), '')
assert.equal(ctx.store.stdout.join('').trim(), 'hello')
assert.equal([...ctx.store.stdout].length, 1)
assert.equal(result.stdout.trim(), 'hello')
assert.equal(result.stdall, '')
})
Expand Down

0 comments on commit 40207b6

Please sign in to comment.