Skip to content

Commit

Permalink
refactor: enhance Shell result type
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Oct 3, 2024
1 parent 1384a8c commit 9f7dda2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
24 changes: 16 additions & 8 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,7 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
}
}
const from = getCallerLocation()
if (pieces.some((p) => p == undefined)) {
throw new Error(`Malformed command at ${from}`)
}
checkCmd(pieces as TemplateStringsArray, from)
checkShell()
checkQuote()

Expand Down Expand Up @@ -719,6 +721,12 @@ function checkQuote() {
throw new Error('No quote function is defined: https://ï.at/no-quote-func')
}

function checkCmd(pieces: TemplateStringsArray, from: string) {
if (pieces.some((p) => p == undefined)) {
throw new Error(`Malformed command at ${from}`)
}
}

let cwdSyncHook: AsyncHook

export function syncProcessCwd(flag: boolean = true) {
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 9f7dda2

Please sign in to comment.