Skip to content

Commit cca4238

Browse files
authoredJun 19, 2024
refactor: simplify setup checks (#852)
* refactor: simplify setup checks * style: make type imports explicit * chore: update no-shell err message
1 parent 7b3046b commit cca4238

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed
 

‎src/core.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
// limitations under the License.
1414

1515
import assert from 'node:assert'
16-
import { spawn, spawnSync, StdioOptions, IOType } from 'node:child_process'
16+
import {
17+
type StdioOptions,
18+
type IOType,
19+
spawn,
20+
spawnSync,
21+
} from 'node:child_process'
1722
import { type Encoding } from 'node:crypto'
18-
import { AsyncHook, AsyncLocalStorage, createHook } from 'node:async_hooks'
19-
import { Readable, Writable } from 'node:stream'
23+
import { type AsyncHook, AsyncLocalStorage, createHook } from 'node:async_hooks'
24+
import { type Readable, type Writable } from 'node:stream'
2025
import { inspect } from 'node:util'
2126
import { EOL } from 'node:os'
2227
import {
@@ -31,7 +36,7 @@ import {
3136
type TSpawnStore,
3237
} from './vendor.js'
3338
import {
34-
Duration,
39+
type Duration,
3540
errnoMessage,
3641
exitCodeInfo,
3742
formatCmd,
@@ -40,7 +45,6 @@ import {
4045
parseDuration,
4146
quote,
4247
quotePowerShell,
43-
noquote,
4448
preferNmBin,
4549
} from './util.js'
4650

@@ -74,7 +78,7 @@ export interface Options {
7478
nothrow: boolean
7579
prefix: string
7680
postfix: string
77-
quote: typeof quote
81+
quote?: typeof quote
7882
quiet: boolean
7983
detached: boolean
8084
preferLocal: boolean
@@ -114,7 +118,6 @@ export const defaults: Options = {
114118
quiet: false,
115119
prefix: '',
116120
postfix: '',
117-
quote: noquote,
118121
detached: false,
119122
preferLocal: false,
120123
spawn,
@@ -145,9 +148,13 @@ export function useBash() {
145148
}
146149

147150
function checkShell() {
148-
if (!$.shell) {
149-
throw new Error(`shell is not available: setup guide goes here`)
150-
}
151+
if (!$.shell)
152+
throw new Error(`No shell is available: https://ï.at/zx-no-shell`)
153+
}
154+
155+
function checkQuote() {
156+
if (!$.quote)
157+
throw new Error('No quote function is defined: https://ï.at/no-quote-func')
151158
}
152159

153160
function getStore() {
@@ -156,8 +163,6 @@ function getStore() {
156163

157164
export const $: Shell & Options = new Proxy<Shell & Options>(
158165
function (pieces, ...args) {
159-
checkShell()
160-
161166
if (!Array.isArray(pieces)) {
162167
return function (this: any, ...args: any) {
163168
const self = this
@@ -170,10 +175,13 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
170175
if (pieces.some((p) => p == undefined)) {
171176
throw new Error(`Malformed command at ${from}`)
172177
}
178+
checkShell()
179+
checkQuote()
180+
173181
let resolve: Resolve, reject: Resolve
174182
const promise = new ProcessPromise((...args) => ([resolve, reject] = args))
175183
const cmd = buildCmd(
176-
$.quote,
184+
$.quote as typeof quote,
177185
pieces as TemplateStringsArray,
178186
args
179187
) as string

‎src/goods.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import {
2020
chalk,
2121
minimist,
2222
nodeFetch,
23-
RequestInfo,
24-
RequestInit,
23+
type RequestInfo,
24+
type RequestInit,
2525
} from './vendor.js'
2626

2727
export { default as path } from 'node:path'

‎src/util.ts

-4
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ export function normalizeMultilinePieces(
8585
)
8686
}
8787

88-
export function noquote(): string {
89-
throw new Error('No quote function is defined: https://ï.at/no-quote-func')
90-
}
91-
9288
export function quote(arg: string) {
9389
if (/^[a-z0-9/_.\-@:=]+$/i.test(arg) || arg === '') {
9490
return arg

0 commit comments

Comments
 (0)