diff --git a/README.md b/README.md index 9087de2..cf91c41 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/src/main/ts/spawn.ts b/src/main/ts/spawn.ts index a9bd6e0..f7128fc 100644 --- a/src/main/ts/spawn.ts +++ b/src/main/ts/spawn.ts @@ -8,13 +8,16 @@ export * from './util.js' export type TSpawnError = any +export type TPushable = { push(...args: T[]): number } + +export type TJoinable = { join(sep?: string): string } + +export type TSpawnStoreChunks = Iterable & TPushable & TJoinable + export type TSpawnStore = { - stdout: Array - stderr: Array - stdall: Array - getStdout: () => string - getStderr: () => string - getStdall: () => string + stdout: TSpawnStoreChunks + stderr: TSpawnStoreChunks + stdall: TSpawnStoreChunks } export type TSpawnResult = { @@ -136,17 +139,11 @@ export const attachListeners = (ee: EventEmitter, on: Partial = } } -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 => { @@ -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, @@ -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 diff --git a/src/test/ts/spawn.test.ts b/src/test/ts/spawn.test.ts index 1360d60..62a1cde 100644 --- a/src/test/ts/spawn.test.ts +++ b/src/test/ts/spawn.test.ts @@ -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, '') })