Skip to content

Commit c81e471

Browse files
committed
chore: rename setupSmth helpers to useSmth
1 parent 23e4de7 commit c81e471

File tree

6 files changed

+24
-30
lines changed

6 files changed

+24
-30
lines changed

src/core.ts

+9-14
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ export interface Options {
5757
[processCwd]: string
5858
[syncExec]: boolean
5959
cwd?: string
60-
cwdHook?: boolean
6160
ac?: AbortController
6261
input?: string | Buffer | Readable | ProcessOutput | ProcessPromise
6362
verbose: boolean
@@ -76,14 +75,19 @@ export interface Options {
7675
}
7776

7877
const storage = new AsyncLocalStorage<Options>()
79-
const cwdHook: AsyncHook & { enabled?: boolean } = createHook({
78+
const cwdSyncHook: AsyncHook & { enabled?: boolean } = createHook({
8079
init: syncCwd,
8180
before: syncCwd,
8281
promiseResolve: syncCwd,
8382
after: syncCwd,
8483
destroy: syncCwd,
8584
})
8685

86+
export function syncProcessCwd(flag: boolean = true) {
87+
if (flag) cwdSyncHook.enable()
88+
else cwdSyncHook.disable()
89+
}
90+
8791
export const defaults: Options = {
8892
[processCwd]: process.cwd(),
8993
[syncExec]: false,
@@ -103,14 +107,14 @@ export const defaults: Options = {
103107
}
104108
const isWin = process.platform == 'win32'
105109

106-
export function setupPowerShell() {
110+
export function usePowerShell() {
107111
$.shell = which.sync('powershell.exe')
108112
$.prefix = ''
109113
$.postfix = '; exit $LastExitCode'
110114
$.quote = quotePowerShell
111115
}
112116

113-
export function setupBash() {
117+
export function useBash() {
114118
$.shell = which.sync('bash')
115119
$.prefix = 'set -euo pipefail;'
116120
$.quote = quote
@@ -170,30 +174,21 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
170174
} as Shell & Options,
171175
{
172176
set(_, key, value) {
173-
if (key === 'cwdHook') {
174-
cwdHook.enabled = !!value
175-
if (value) cwdHook.enable()
176-
else cwdHook.disable()
177-
return true
178-
}
179-
180177
const target = key in Function.prototype ? _ : getStore()
181178
Reflect.set(target, key === 'sync' ? syncExec : key, value)
182179

183180
return true
184181
},
185182
get(_, key) {
186183
if (key === 'sync') return $({ sync: true })
187-
if (key === 'cwdHook') return cwdHook.enabled
188184

189185
const target = key in Function.prototype ? _ : getStore()
190186
return Reflect.get(target, key)
191187
},
192188
}
193189
)
194-
$.cwdHook = false
195190
try {
196-
setupBash()
191+
useBash()
197192
} catch (err) {}
198193

199194
type Resolve = (out: ProcessOutput) => void

src/globals.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ declare global {
4141
var quote: typeof _.quote
4242
var quotePowerShell: typeof _.quotePowerShell
4343
var retry: typeof _.retry
44-
var setupPowerShell: typeof _.setupPowerShell
44+
var usePowerShell: typeof _.usePowerShell
45+
var useBash: typeof _.useBash
4546
var sleep: typeof _.sleep
4647
var spinner: typeof _.spinner
4748
var stdin: typeof _.stdin

test/core.test.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -232,14 +232,6 @@ describe('core', () => {
232232
assert.ok((await p) == 'foo')
233233
})
234234

235-
test('$.cwdHook is configurable', () => {
236-
$.cwdHook = true
237-
assert.equal($.cwdHook, true)
238-
239-
$.cwdHook = false
240-
assert.equal($.cwdHook, false)
241-
})
242-
243235
test('cd() works with relative paths', async () => {
244236
let cwd = process.cwd()
245237
try {
@@ -271,8 +263,8 @@ describe('core', () => {
271263
}
272264
})
273265

274-
test('cd() does not affect parallel contexts ($.cwdHook enabled)', async () => {
275-
$.cwdHook = true
266+
test('cd() does not affect parallel contexts ($.cwdSyncHook enabled)', async () => {
267+
syncProcessCwd()
276268
const cwd = process.cwd()
277269
try {
278270
fs.mkdirpSync('/tmp/zx-cd-parallel/one/two')
@@ -306,7 +298,7 @@ describe('core', () => {
306298
} finally {
307299
fs.rmSync('/tmp/zx-cd-parallel', { recursive: true })
308300
cd(cwd)
309-
$.cwdHook = false
301+
syncProcessCwd(false)
310302
}
311303
})
312304

test/index.test.js

+6
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ import {
2020
$,
2121
log,
2222
cd,
23+
syncProcessCwd,
24+
usePowerShell,
25+
useBash,
2326
kill,
2427
ProcessOutput,
2528
ProcessPromise,
@@ -60,10 +63,13 @@ describe('index', () => {
6063
assert(ProcessOutput)
6164
assert(ProcessPromise)
6265
assert(cd)
66+
assert(syncProcessCwd)
6367
assert(log)
6468
assert(kill)
6569
assert(defaults)
6670
assert(within)
71+
assert(usePowerShell)
72+
assert(useBash)
6773

6874
// goods
6975
assert(argv)

test/package.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import { test, describe, beforeEach, before, after } from 'node:test'
1717
import '../build/globals.js'
1818

1919
describe('package', () => {
20-
before(() => $.cwdHook = true)
21-
after(() => $.cwdHook = false)
20+
before(() => syncProcessCwd())
21+
after(() => syncProcessCwd(false))
2222
beforeEach(async () => {
2323
const pack = await $`npm pack`
2424
await $`tar xf ${pack}`

test/smoke/win32.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ _describe('win32', () => {
2424
assert.match(p.stdout, /bash/)
2525

2626
await within(async () => {
27-
setupPowerShell()
27+
usePowerShell()
2828
assert.match($.shell, /powershell/i)
2929
const p = await $`get-host`
3030
assert.match(p.stdout, /PowerShell/)
@@ -33,7 +33,7 @@ _describe('win32', () => {
3333

3434
test('quotePowerShell works', async () => {
3535
await within(async () => {
36-
setupPowerShell()
36+
usePowerShell()
3737
const p = await $`echo ${`Windows 'rulez!'`}`
3838
assert.match(p.stdout, /Windows 'rulez!'/)
3939
})

0 commit comments

Comments
 (0)