Skip to content

Commit

Permalink
refactor: do not hack stream behavior (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored Oct 30, 2024
1 parent 5f0874e commit 7ffb44e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
16 changes: 7 additions & 9 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return super.catch(onrejected)
}

private static promisifyStream<D extends Writable>(
dest: D
): D & PromiseLike<void> {
return new Proxy(dest as D & PromiseLike<void>, {
private static promisifyStream<S extends Writable>(
stream: S
): S & PromiseLike<void> {
return new Proxy(stream as S & PromiseLike<void>, {
get(target, key) {
if (key === 'then') {
return (res: any = noop, rej: any = noop) =>
Expand All @@ -548,11 +548,9 @@ export class ProcessPromise extends Promise<ProcessOutput> {
const pipe = Reflect.get(target, key)
if (typeof pipe === 'function')
return function (...args: any) {
return (
args[0] instanceof ProcessPromise
? noop
: ProcessPromise.promisifyStream
)(pipe.apply(target, args) as Writable)
return ProcessPromise.promisifyStream(
pipe.apply(target, args) as S
)
}
}
return Reflect.get(target, key)
Expand Down
52 changes: 27 additions & 25 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,33 +397,35 @@ describe('core', () => {
)
})

test('is chainable ($)', async () => {
let { stdout: o1 } = await $`echo "hello"`
.pipe($`awk '{print $1" world"}'`)
.pipe($`tr '[a-z]' '[A-Z]'`)
assert.equal(o1, 'HELLO WORLD\n')

let { stdout: o2 } = await $`echo "hello"`
.pipe`awk '{print $1" world"}'`.pipe`tr '[a-z]' '[A-Z]'`
assert.equal(o2, 'HELLO WORLD\n')
})
describe('is chainable', () => {
test('$ to $', async () => {
let { stdout: o1 } = await $`echo "hello"`
.pipe($`awk '{print $1" world"}'`)
.pipe($`tr '[a-z]' '[A-Z]'`)
assert.equal(o1, 'HELLO WORLD\n')

let { stdout: o2 } = await $`echo "hello"`
.pipe`awk '{print $1" world"}'`.pipe`tr '[a-z]' '[A-Z]'`
assert.equal(o2, 'HELLO WORLD\n')
})

test('is chainable (Transform/Duplex)', async () => {
const file = tempfile()
const p = $`echo "hello"`
.pipe(
new Transform({
transform(chunk, encoding, callback) {
callback(null, String(chunk).toUpperCase())
},
})
)
.pipe(fs.createWriteStream(file))
test('Transform/Duplex', async () => {
const file = tempfile()
const p = $`echo "hello"`
.pipe(
new Transform({
transform(chunk, encoding, callback) {
callback(null, String(chunk).toUpperCase())
},
})
)
.pipe(fs.createWriteStream(file))

assert.ok(p instanceof WriteStream)
assert.equal(await p, undefined)
assert.equal((await fs.readFile(file)).toString(), 'HELLO\n')
await fs.rm(file)
assert.ok(p instanceof WriteStream)
assert.equal(await p, undefined)
assert.equal((await fs.readFile(file)).toString(), 'HELLO\n')
await fs.rm(file)
})
})

it('supports multipiping', async () => {
Expand Down

0 comments on commit 7ffb44e

Please sign in to comment.