@@ -41,10 +41,10 @@ import {
41
41
quotePowerShell ,
42
42
} from './util.js'
43
43
44
- export type Shell = (
45
- pieces : TemplateStringsArray ,
46
- ... args : any [ ]
47
- ) => ProcessPromise
44
+ export interface Shell {
45
+ ( pieces : TemplateStringsArray , ... args : any [ ] ) : ProcessPromise
46
+ ( opts : Partial < Options > ) : Shell
47
+ }
48
48
49
49
const processCwd = Symbol ( 'processCwd' )
50
50
@@ -54,6 +54,7 @@ export interface Options {
54
54
verbose : boolean
55
55
env : NodeJS . ProcessEnv
56
56
shell : string | boolean
57
+ nothrow : boolean
57
58
prefix : string
58
59
quote : typeof quote
59
60
spawn : typeof spawn
@@ -75,6 +76,7 @@ export const defaults: Options = {
75
76
verbose : true ,
76
77
env : process . env ,
77
78
shell : true ,
79
+ nothrow : false ,
78
80
prefix : '' ,
79
81
quote : ( ) => {
80
82
throw new Error ( 'No quote function is defined: https://ï.at/no-quote-func' )
@@ -102,15 +104,27 @@ function getStore() {
102
104
return storage . getStore ( ) || defaults
103
105
}
104
106
105
- export const $ = new Proxy < Shell & Options > (
107
+ export const $ : Shell & Options = new Proxy < Shell & Options > (
106
108
function ( pieces , ...args ) {
109
+ if ( ! Array . isArray ( pieces ) ) {
110
+ return function ( this : any , ...args : any ) {
111
+ const self = this
112
+ return within ( ( ) => {
113
+ return Object . assign ( $ , pieces ) . apply ( self , args )
114
+ } )
115
+ }
116
+ }
107
117
const from = new Error ( ) . stack ! . split ( / ^ \s * a t \s / m) [ 2 ] . trim ( )
108
118
if ( pieces . some ( ( p ) => p == undefined ) ) {
109
119
throw new Error ( `Malformed command at ${ from } ` )
110
120
}
111
121
let resolve : Resolve , reject : Resolve
112
122
const promise = new ProcessPromise ( ( ...args ) => ( [ resolve , reject ] = args ) )
113
- const cmd = buildCmd ( $ . quote , pieces , args ) as string
123
+ const cmd = buildCmd (
124
+ $ . quote ,
125
+ pieces as TemplateStringsArray ,
126
+ args
127
+ ) as string
114
128
115
129
promise . _bind ( cmd , from , resolve ! , reject ! , getStore ( ) )
116
130
// Postpone run to allow promise configuration.
@@ -148,7 +162,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
148
162
private _reject : Resolve = noop
149
163
private _snapshot = getStore ( )
150
164
private _stdio : [ IO , IO , IO ] = [ 'inherit' , 'pipe' , 'pipe' ]
151
- private _nothrow = false
165
+ private _nothrow ?: boolean
152
166
private _quiet = false
153
167
private _timeout ?: number
154
168
private _timeoutSignal ?: string
@@ -187,18 +201,10 @@ export class ProcessPromise extends Promise<ProcessOutput> {
187
201
188
202
this . _zurk = zurk$ ( {
189
203
cmd : $ . prefix + this . _command ,
190
- get cwd ( ) {
191
- return $ . cwd ?? $ [ processCwd ]
192
- } ,
193
- get shell ( ) {
194
- return typeof $ . shell === 'string' ? $ . shell : true
195
- } ,
196
- get env ( ) {
197
- return $ . env
198
- } ,
199
- get spawn ( ) {
200
- return $ . spawn
201
- } ,
204
+ cwd : $ . cwd ?? $ [ processCwd ] ,
205
+ shell : typeof $ . shell === 'string' ? $ . shell : true ,
206
+ env : $ . env ,
207
+ spawn : $ . spawn ,
202
208
quote : < T > ( v : T ) : T => v , // let zx handle quoting
203
209
stdio : this . _stdio as any ,
204
210
sync : false ,
@@ -222,6 +228,7 @@ export class ProcessPromise extends Promise<ProcessOutput> {
222
228
this . _zurk . then ( ( { error, stdout, stderr, stdall, status, signal } ) => {
223
229
if ( error ) {
224
230
const message = ProcessOutput . getErrorMessage ( error , self . _from )
231
+ // (nothrow ? self._resolve : self._reject)(
225
232
self . _reject (
226
233
new ProcessOutput ( null , null , stdout , stderr , stdall , message )
227
234
)
@@ -240,7 +247,8 @@ export class ProcessPromise extends Promise<ProcessOutput> {
240
247
stdall ,
241
248
message
242
249
)
243
- if ( status === 0 || self . _nothrow ) {
250
+
251
+ if ( status === 0 || ( self . _nothrow ?? $ . nothrow ) ) {
244
252
self . _resolve ( output )
245
253
} else {
246
254
self . _reject ( output )
0 commit comments