@@ -32,6 +32,7 @@ const {
32
32
ArrayPrototypeSort,
33
33
ArrayPrototypeSplice,
34
34
ArrayPrototypeUnshift,
35
+ ArrayPrototypePushApply,
35
36
NumberIsInteger,
36
37
ObjectAssign,
37
38
ObjectDefineProperty,
@@ -249,6 +250,45 @@ ObjectDefineProperty(exec, promisify.custom, {
249
250
value : customPromiseExecFunction ( exec )
250
251
} ) ;
251
252
253
+ function normalizeExecFileArgs ( file , args , options , callback ) {
254
+ if ( ArrayIsArray ( args ) ) {
255
+ args = ArrayPrototypeSlice ( args ) ;
256
+ } else if ( args != null && typeof args === 'object' ) {
257
+ callback = options ;
258
+ options = args ;
259
+ args = null ;
260
+ } else if ( typeof args === 'function' ) {
261
+ callback = args ;
262
+ options = null ;
263
+ args = null ;
264
+ }
265
+
266
+ if ( args == null ) {
267
+ args = [ ] ;
268
+ }
269
+
270
+ if ( typeof options === 'function' ) {
271
+ callback = options ;
272
+ } else if ( options != null ) {
273
+ validateObject ( options , 'options' ) ;
274
+ }
275
+
276
+ if ( options == null ) {
277
+ options = kEmptyObject ;
278
+ }
279
+
280
+ if ( callback != null ) {
281
+ validateFunction ( callback , 'callback' ) ;
282
+ }
283
+
284
+ // Validate argv0, if present.
285
+ if ( options . argv0 != null ) {
286
+ validateString ( options . argv0 , 'options.argv0' ) ;
287
+ }
288
+
289
+ return { file, args, options, callback } ;
290
+ }
291
+
252
292
/**
253
293
* Spawns the specified file as a shell.
254
294
* @param {string } file
@@ -274,27 +314,8 @@ ObjectDefineProperty(exec, promisify.custom, {
274
314
* ) => any} [callback]
275
315
* @returns {ChildProcess }
276
316
*/
277
- function execFile ( file , args = [ ] , options , callback ) {
278
- if ( args != null && typeof args === 'object' && ! ArrayIsArray ( args ) ) {
279
- callback = options ;
280
- options = args ;
281
- args = null ;
282
- } else if ( typeof args === 'function' ) {
283
- callback = args ;
284
- options = null ;
285
- args = null ;
286
- }
287
-
288
- if ( typeof options === 'function' ) {
289
- callback = options ;
290
- options = null ;
291
- } else if ( options != null ) {
292
- validateObject ( options , 'options' ) ;
293
- }
294
-
295
- if ( callback != null ) {
296
- validateFunction ( callback , 'callback' ) ;
297
- }
317
+ function execFile ( file , args , options , callback ) {
318
+ ( { file, args, options, callback } = normalizeExecFileArgs ( file , args , options , callback ) ) ;
298
319
299
320
options = {
300
321
encoding : 'utf8' ,
@@ -824,7 +845,7 @@ function checkExecSyncError(ret, args, cmd) {
824
845
825
846
/**
826
847
* Spawns a file as a shell synchronously.
827
- * @param {string } command
848
+ * @param {string } file
828
849
* @param {string[] } [args]
829
850
* @param {{
830
851
* cwd?: string;
@@ -842,17 +863,18 @@ function checkExecSyncError(ret, args, cmd) {
842
863
* }} [options]
843
864
* @returns {Buffer | string }
844
865
*/
845
- function execFileSync ( command , args , options ) {
846
- options = normalizeSpawnArguments ( command , args , options ) ;
866
+ function execFileSync ( file , args , options ) {
867
+ ( { file , args , options } = normalizeExecFileArgs ( file , args , options ) ) ;
847
868
848
869
const inheritStderr = ! options . stdio ;
849
- const ret = spawnSync ( options . file ,
850
- ArrayPrototypeSlice ( options . args , 1 ) , options ) ;
870
+ const ret = spawnSync ( file , args , options ) ;
851
871
852
872
if ( inheritStderr && ret . stderr )
853
873
process . stderr . write ( ret . stderr ) ;
854
874
855
- const err = checkExecSyncError ( ret , options . args , undefined ) ;
875
+ const errArgs = [ options . argv0 || file ] ;
876
+ ArrayPrototypePushApply ( errArgs , args ) ;
877
+ const err = checkExecSyncError ( ret , errArgs ) ;
856
878
857
879
if ( err )
858
880
throw err ;
0 commit comments