Skip to content

Commit 5cf68b7

Browse files
committed
feat: let cwdHook be configurable
1 parent b75c71a commit 5cf68b7

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/core.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import {
3838
parseDuration,
3939
quote,
4040
quotePowerShell,
41+
noquote,
4142
} from './util.js'
4243

4344
export interface Shell {
@@ -56,6 +57,7 @@ export interface Options {
5657
[processCwd]: string
5758
[syncExec]: boolean
5859
cwd?: string
60+
cwdHook?: boolean
5961
ac?: AbortController
6062
input?: string | Buffer | Readable | ProcessOutput | ProcessPromise
6163
verbose: boolean
@@ -74,14 +76,14 @@ export interface Options {
7476
}
7577

7678
const storage = new AsyncLocalStorage<Options>()
77-
const hook = createHook({
79+
const cwdHook = createHook({
7880
init: syncCwd,
7981
before: syncCwd,
8082
promiseResolve: syncCwd,
8183
after: syncCwd,
8284
destroy: syncCwd,
8385
})
84-
hook.enable()
86+
cwdHook.enable()
8587

8688
export const defaults: Options = {
8789
[processCwd]: process.cwd(),
@@ -94,9 +96,7 @@ export const defaults: Options = {
9496
quiet: false,
9597
prefix: '',
9698
postfix: '',
97-
quote: () => {
98-
throw new Error('No quote function is defined: https://ï.at/no-quote-func')
99-
},
99+
quote: noquote,
100100
spawn,
101101
spawnSync,
102102
log,
@@ -171,6 +171,12 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
171171
} as Shell & Options,
172172
{
173173
set(_, key, value) {
174+
if (key === 'cwdHook') {
175+
if (value) cwdHook.enable()
176+
else cwdHook.disable()
177+
return true
178+
}
179+
174180
const target = key in Function.prototype ? _ : getStore()
175181
Reflect.set(target, key === 'sync' ? syncExec : key, value)
176182

src/util.ts

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export function normalizeMultilinePieces(
4242
)
4343
}
4444

45+
export function noquote(): string {
46+
throw new Error('No quote function is defined: https://ï.at/no-quote-func')
47+
}
48+
4549
export function quote(arg: string) {
4650
if (/^[a-z0-9/_.\-@:=]+$/i.test(arg) || arg === '') {
4751
return arg

test/core.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -263,25 +263,25 @@ describe('core', () => {
263263
}
264264
})
265265

266-
test('cd() does affect parallel contexts', async () => {
266+
test('cd() does not affect parallel contexts', async () => {
267267
const cwd = process.cwd()
268268
try {
269269
fs.mkdirpSync('/tmp/zx-cd-parallel/one/two')
270270
await Promise.all([
271271
within(async () => {
272272
assert.equal(process.cwd(), cwd)
273-
await sleep(1)
274273
cd('/tmp/zx-cd-parallel/one')
274+
await sleep(Math.random() * 15)
275275
assert.ok(process.cwd().endsWith('/tmp/zx-cd-parallel/one'))
276276
}),
277277
within(async () => {
278278
assert.equal(process.cwd(), cwd)
279-
await sleep(2)
279+
await sleep(Math.random() * 15)
280280
assert.equal(process.cwd(), cwd)
281281
}),
282282
within(async () => {
283283
assert.equal(process.cwd(), cwd)
284-
await sleep(3)
284+
await sleep(Math.random() * 15)
285285
$.cwd = '/tmp/zx-cd-parallel/one/two'
286286
assert.equal(process.cwd(), cwd)
287287
assert.ok(

0 commit comments

Comments
 (0)