@@ -32,6 +32,7 @@ const {
3232 ArrayPrototypeSort,
3333 ArrayPrototypeSplice,
3434 ArrayPrototypeUnshift,
35+ ArrayPrototypePushApply,
3536 NumberIsInteger,
3637 ObjectAssign,
3738 ObjectDefineProperty,
@@ -249,6 +250,45 @@ ObjectDefineProperty(exec, promisify.custom, {
249250 value: customPromiseExecFunction(exec)
250251});
251252
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+
252292/**
253293 * Spawns the specified file as a shell.
254294 * @param {string} file
@@ -274,27 +314,8 @@ ObjectDefineProperty(exec, promisify.custom, {
274314 * ) => any} [callback]
275315 * @returns {ChildProcess}
276316 */
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));
298319
299320 options = {
300321 encoding: 'utf8',
@@ -824,7 +845,7 @@ function checkExecSyncError(ret, args, cmd) {
824845
825846/**
826847 * Spawns a file as a shell synchronously.
827- * @param {string} command
848+ * @param {string} file
828849 * @param {string[]} [args]
829850 * @param {{
830851 * cwd?: string;
@@ -842,17 +863,18 @@ function checkExecSyncError(ret, args, cmd) {
842863 * }} [options]
843864 * @returns {Buffer | string}
844865 */
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) );
847868
848869 const inheritStderr = !options.stdio;
849- const ret = spawnSync(options.file,
850- ArrayPrototypeSlice(options.args, 1), options);
870+ const ret = spawnSync(file, args, options);
851871
852872 if (inheritStderr && ret.stderr)
853873 process.stderr.write(ret.stderr);
854874
855- const err = checkExecSyncError(ret, options.args, undefined);
875+ const errArgs = [options.argv0 || file];
876+ ArrayPrototypePushApply(errArgs, args);
877+ const err = checkExecSyncError(ret, errArgs);
856878
857879 if (err)
858880 throw err;
0 commit comments