Skip to content

Commit

Permalink
feat: enable stringLiterals for pipe() API (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Sep 17, 2024
1 parent d814096 commit 5038ec5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{
"name": "zx/index",
"path": "build/*.{js,cjs}",
"limit": "795 kB",
"limit": "796 kB",
"brotli": false,
"gzip": false
},
Expand Down
10 changes: 8 additions & 2 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
chalk,
which,
ps,
isStringLiteral,
type ChalkInstance,
type RequestInfo,
type RequestInit,
Expand Down Expand Up @@ -461,7 +462,12 @@ export class ProcessPromise extends Promise<ProcessOutput> {
return super.catch(onrejected)
}

pipe(dest: Writable | ProcessPromise): ProcessPromise {
pipe(
dest: Writable | ProcessPromise | TemplateStringsArray,
...args: any[]
): ProcessPromise {
if (isStringLiteral(dest))
return this.pipe($(dest as TemplateStringsArray, ...args))
if (isString(dest))
throw new Error('The pipe() method does not take strings. Forgot $?')
if (this._resolved) {
Expand All @@ -488,7 +494,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
}
return dest
} else {
this._postrun = () => this.stdout.pipe(dest)
this._postrun = () => this.stdout.pipe(dest as Writable)
return this
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import path from 'node:path'
import fs from 'node:fs'
import { chalk } from './vendor-core.js'

export { isStringLiteral } from './vendor-core.js'

export function tempdir(prefix = `zx-${randomId()}`) {
const dirpath = path.join(os.tmpdir(), prefix)
fs.mkdirSync(dirpath, { recursive: true })
Expand Down
2 changes: 1 addition & 1 deletion src/vendor-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

export { exec, buildCmd, type TSpawnStore } from 'zurk/spawn'
export { exec, buildCmd, isStringLiteral, type TSpawnStore } from 'zurk/spawn'

export type RequestInfo = Parameters<typeof globalThis.fetch>[0]
export type RequestInit = Parameters<typeof globalThis.fetch>[1]
Expand Down
66 changes: 38 additions & 28 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,34 +317,6 @@ describe('core', () => {
})

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

test('propagates rejection', async () => {
const p1 = $`exit 1`
const p2 = p1.pipe($`echo hello`)

try {
await p1
} catch (e) {
assert.equal(e.exitCode, 1)
}

try {
await p2
} catch (e) {
assert.equal(e.exitCode, 1)
}

const p3 = await $({ nothrow: true })`echo hello && exit 1`.pipe($`cat`)
assert.equal(p3.exitCode, 0)
assert.equal(p3.stdout.trim(), 'hello')
})

test('accepts Writable', async () => {
let contents = ''
let stream = new Writable({
Expand Down Expand Up @@ -376,6 +348,16 @@ describe('core', () => {
}
})

test('accepts ProcessPromise', async () => {
const p = await $`echo foo`.pipe($`cat`)
assert.equal(p.stdout.trim(), 'foo')
})

test('accepts $ template literal', async () => {
const p = await $`echo foo`.pipe`cat`
assert.equal(p.stdout.trim(), 'foo')
})

test('checks argument type', async () => {
let err
try {
Expand All @@ -389,6 +371,13 @@ describe('core', () => {
)
})

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

test('throws if already resolved', async (t) => {
let ok = true
let p = $`echo "Hello"`
Expand All @@ -404,6 +393,27 @@ describe('core', () => {
}
assert.ok(ok, 'Expected failure!')
})

test('propagates rejection', async () => {
const p1 = $`exit 1`
const p2 = p1.pipe($`echo hello`)

try {
await p1
} catch (e) {
assert.equal(e.exitCode, 1)
}

try {
await p2
} catch (e) {
assert.equal(e.exitCode, 1)
}

const p3 = await $({ nothrow: true })`echo hello && exit 1`.pipe($`cat`)
assert.equal(p3.exitCode, 0)
assert.equal(p3.stdout.trim(), 'hello')
})
})

describe('abort()', () => {
Expand Down

0 comments on commit 5038ec5

Please sign in to comment.