Skip to content

Commit

Permalink
refactor: enhance Shell result type (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub authored Oct 4, 2024
1 parent 1384a8c commit ac2f1dd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .size-limit.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{
"name": "all",
"path": "build/*",
"limit": "832 kB",
"limit": "833 kB",
"brotli": false,
"gzip": false
}
Expand Down
18 changes: 11 additions & 7 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,21 @@ export const defaults: Options = {
timeoutSignal: 'SIGTERM',
}

export interface Shell {
(pieces: TemplateStringsArray, ...args: any[]): ProcessPromise
(opts: Partial<Options>): Shell
// prettier-ignore
export interface Shell<
S = false,
R = S extends true ? ProcessOutput : ProcessPromise,
> {
(pieces: TemplateStringsArray, ...args: any[]): R
<O extends Partial<Options> = Partial<Options>, R = O extends { sync: true } ? Shell<true> : Shell>(opts: O): R
sync: {
(pieces: TemplateStringsArray, ...args: any[]): ProcessOutput
(opts: Partial<Options>): Shell
(opts: Partial<Omit<Options, 'sync'>>): Shell<true>
}
}

export const $: Shell & Options = new Proxy<Shell & Options>(
function (pieces, ...args) {
function (pieces: TemplateStringsArray | Partial<Options>, ...args: any) {
const snapshot = getStore()
if (!Array.isArray(pieces)) {
return function (this: any, ...args: any) {
Expand All @@ -136,9 +140,9 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
}
}
const from = getCallerLocation()
if (pieces.some((p) => p == undefined)) {
if (pieces.some((p) => p == undefined))
throw new Error(`Malformed command at ${from}`)
}

checkShell()
checkQuote()

Expand Down
5 changes: 5 additions & 0 deletions test-d/core.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,8 @@ expectType<ProcessOutput>(new ProcessOutput(null, null, '', '', '', ''))
expectError(new ProcessOutput(null, null))

expectType<'banana'>(within(() => 'apple' as 'banana'))

expectType<ProcessPromise>($`cmd`)
expectType<ProcessPromise>($({ sync: false })`cmd`)
expectType<ProcessOutput>($({ sync: true })`cmd`)
expectType<ProcessOutput>($.sync`cmd`)
8 changes: 7 additions & 1 deletion test/smoke/ts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
import * as assert from 'node:assert'
import 'zx/globals'
;(async () => {
// smoke test
// smoke test async
{
const p = await $`echo foo`
assert.match(p.stdout, /foo/)
}

// smoke test sync
{
const p = $.sync`echo foo`
assert.match(p.stdout, /foo/)
}

// captures err stack
{
const p = await $({ nothrow: true })`echo foo; exit 3`
Expand Down

0 comments on commit ac2f1dd

Please sign in to comment.