14
14
15
15
import assert from 'node:assert'
16
16
import { spawn , spawnSync , StdioNull , StdioPipe } from 'node:child_process'
17
- import { AsyncLocalStorage , createHook } from 'node:async_hooks'
17
+ import { AsyncHook , AsyncLocalStorage , createHook } from 'node:async_hooks'
18
18
import { Readable , Writable } from 'node:stream'
19
19
import { inspect } from 'node:util'
20
20
import {
@@ -38,6 +38,7 @@ import {
38
38
parseDuration ,
39
39
quote ,
40
40
quotePowerShell ,
41
+ noquote ,
41
42
} from './util.js'
42
43
43
44
export interface Shell {
@@ -74,14 +75,18 @@ export interface Options {
74
75
}
75
76
76
77
const storage = new AsyncLocalStorage < Options > ( )
77
- const hook = createHook ( {
78
+ const cwdSyncHook : AsyncHook & { enabled ?: boolean } = createHook ( {
78
79
init : syncCwd ,
79
80
before : syncCwd ,
80
81
promiseResolve : syncCwd ,
81
82
after : syncCwd ,
82
83
destroy : syncCwd ,
83
84
} )
84
- hook . enable ( )
85
+
86
+ export function syncProcessCwd ( flag : boolean = true ) {
87
+ if ( flag ) cwdSyncHook . enable ( )
88
+ else cwdSyncHook . disable ( )
89
+ }
85
90
86
91
export const defaults : Options = {
87
92
[ processCwd ] : process . cwd ( ) ,
@@ -94,24 +99,22 @@ export const defaults: Options = {
94
99
quiet : false ,
95
100
prefix : '' ,
96
101
postfix : '' ,
97
- quote : ( ) => {
98
- throw new Error ( 'No quote function is defined: https://ï.at/no-quote-func' )
99
- } ,
102
+ quote : noquote ,
100
103
spawn,
101
104
spawnSync,
102
105
log,
103
106
kill,
104
107
}
105
108
const isWin = process . platform == 'win32'
106
109
107
- export function setupPowerShell ( ) {
110
+ export function usePowerShell ( ) {
108
111
$ . shell = which . sync ( 'powershell.exe' )
109
112
$ . prefix = ''
110
113
$ . postfix = '; exit $LastExitCode'
111
114
$ . quote = quotePowerShell
112
115
}
113
116
114
- export function setupBash ( ) {
117
+ export function useBash ( ) {
115
118
$ . shell = which . sync ( 'bash' )
116
119
$ . prefix = 'set -euo pipefail;'
117
120
$ . quote = quote
@@ -184,9 +187,8 @@ export const $: Shell & Options = new Proxy<Shell & Options>(
184
187
} ,
185
188
}
186
189
)
187
-
188
190
try {
189
- setupBash ( )
191
+ useBash ( )
190
192
} catch ( err ) { }
191
193
192
194
type Resolve = ( out : ProcessOutput ) => void
0 commit comments