Skip to content
Myr

Allow the first argument of `execa()` to be a file URL (#631)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent be739e7 · Browse files · 6 files+79 added−23 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+79 added−23 removed

  • tests:+35 added−3 removed
  • other:+44 added−20 removed

6 files+79 added−23 removed

modified:test/helpers/fixtures-dir.js+2 added-1 removed
modified:test/test.js+33 added-2 removed
2 unmodified lines
3import {fileURLToPath, pathToFileURL} from 'node:url';4import test from 'ava';5import isRunning from 'is-running';6import getNode from 'get-node';7import {execa, execaSync, $} from '../index.js';8import {setFixtureDir, PATH_KEY} from './helpers/fixtures-dir.js';7import {execa, execaSync, execaNode, $} from '../index.js';8import {setFixtureDir, PATH_KEY, FIXTURES_DIR_URL} from './helpers/fixtures-dir.js';910setFixtureDir();11process.env.FOO = 'foo';12
247 unmodified lines
260 t.true(isRunning(pid));261262 process.kill(pid, 'SIGKILL');263});264265const testFileUrl = async (t, execaMethod) => {266 const command = new URL('noop.js', FIXTURES_DIR_URL);267 const {stdout} = await execaMethod(command, ['foobar']);268 t.is(stdout, 'foobar');269};270271test('execa()\'s command argument can be a file URL', testFileUrl, execa);272test('execaSync()\'s command argument can be a file URL', testFileUrl, execaSync);273test('execaNode()\'s command argument can be a file URL', testFileUrl, execaNode);274275const testInvalidFileUrl = async (t, execaMethod) => {276 const invalidUrl = new URL('https://invalid.com');277 t.throws(() => {278 execaMethod(invalidUrl);279 }, {code: 'ERR_INVALID_URL_SCHEME'});280};281282test('execa()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execa);283test('execaSync()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execaSync);284test('execaNode()\'s command argument cannot be a non-file URL', testInvalidFileUrl, execaNode);285286const testInvalidCommand = async (t, execaMethod) => {287 t.throws(() => {288 execaMethod(['command', 'arg']);289 }, {message: /must be a string or a file URL/});290};291292test('execa()\'s command argument must be a string or file URL', testInvalidCommand, execa);293test('execaSync()\'s command argument must be a string or file URL', testInvalidCommand, execaSync);294test('execaNode()\'s command argument must be a string or file URL', testInvalidCommand, execaNode);
modified:index.d.ts+14 added-11 removed
550 unmodified lines
551ExecaChildPromise<StdoutStderrType> &552Promise<ExecaReturnValue<StdoutStderrType>>;553554/**555Executes a command using `file ...arguments`. `arguments` are specified as an array of strings. Returns a `childProcess`.555Executes a command using `file ...arguments`. `file` is a string or a file URL. `arguments` are an array of strings. Returns a `childProcess`.556557Arguments are automatically escaped. They can contain any character, including spaces.558559This is the preferred method when executing single commands.560561@param file - The program/script to execute.561@param file - The program/script to execute, as a string or file URL562@param arguments - Arguments to pass to `file` on execution.563@returns An `ExecaChildProcess` that is both:564 - a `Promise` resolving or rejecting with a `childProcessResult`.565 - a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with some additional methods and properties.
96 unmodified lines
662}, 1000);663```664*/665export function execa<EncodingType extends EncodingOption = DefaultEncodingOption>(666 file: string,666 file: string | URL,667 arguments?: readonly string[],668 options?: Options<EncodingType>669): ExecaChildProcess<GetStdoutStderrType<EncodingType>>;670export function execa<EncodingType extends EncodingOption = DefaultEncodingOption>(671 file: string, options?: Options<EncodingType>671 file: string | URL,672 options?: Options<EncodingType>673): ExecaChildProcess<GetStdoutStderrType<EncodingType>>;674675/**676Same as `execa()` but synchronous.677677@param file - The program/script to execute.678@param file - The program/script to execute, as a string or file URL679@param arguments - Arguments to pass to `file` on execution.680@returns A `childProcessResult` object681@throws A `childProcessResult` error682
49 unmodified lines
732}733```734*/735export function execaSync<EncodingType extends EncodingOption = DefaultEncodingOption>(735 file: string,736 file: string | URL,737 arguments?: readonly string[],738 options?: SyncOptions<EncodingType>739): ExecaSyncReturnValue<GetStdoutStderrType<EncodingType>>;740export function execaSync<EncodingType extends EncodingOption = DefaultEncodingOption>(740 file: string, options?: SyncOptions<EncodingType>741 file: string | URL,742 options?: SyncOptions<EncodingType>743): ExecaSyncReturnValue<GetStdoutStderrType<EncodingType>>;744745/**746Executes a command. The `command` string includes both the `file` and its `arguments`. Returns a `childProcess`.
236 unmodified lines
983*/984export const $: Execa$;985986/**985Execute a Node.js script as a child process.987Executes a Node.js file using `node scriptPath ...arguments`. `file` is a string or a file URL. `arguments` are an array of strings. Returns a `childProcess`.988989Arguments are automatically escaped. They can contain any character, including spaces.990991This is the preferred method when executing Node.js files.
2 unmodified lines
994 - the current Node version and options are used. This can be overridden using the `nodePath` and `nodeOptions` options.995 - the `shell` option cannot be used996 - an extra channel [`ipc`](https://nodejs.org/api/child_process.html#child_process_options_stdio) is passed to `stdio`997996@param scriptPath - Node.js script to execute.998@param scriptPath - Node.js script to execute, as a string or file URL999@param arguments - Arguments to pass to `scriptPath` on execution.1000@returns An `ExecaChildProcess` that is both:1001 - a `Promise` resolving or rejecting with a `childProcessResult`.1002 - a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with some additional methods and properties.
6 unmodified lines
1009await execaNode('scriptPath', ['argument']);1010```1011*/1012export function execaNode<EncodingType extends EncodingOption = DefaultEncodingOption>(1011 scriptPath: string,1013 scriptPath: string | URL,1014 arguments?: readonly string[],1015 options?: NodeOptions<EncodingType>1016): ExecaChildProcess<GetStdoutStderrType<EncodingType>>;1017export function execaNode<EncodingType extends EncodingOption = DefaultEncodingOption>(1016 scriptPath: string, options?: NodeOptions<EncodingType>1018 scriptPath: string | URL,1019 options?: NodeOptions<EncodingType>1020): ExecaChildProcess<GetStdoutStderrType<EncodingType>>;
modified:index.js+22 added-7 removed
1import {Buffer} from 'node:buffer';2import path from 'node:path';3import childProcess from 'node:child_process';4import process from 'node:process';5import {fileURLToPath} from 'node:url';6import crossSpawn from 'cross-spawn';7import stripFinalNewline from 'strip-final-newline';8import {npmRunPathEnv} from 'npm-run-path';9import onetime from 'onetime';
19 unmodified lines
2930 return env;31};3233const getFilePath = file => {34 if (file instanceof URL) {35 return fileURLToPath(file);36 }3738 if (typeof file !== 'string') {39 throw new TypeError('First argument must be a string or a file URL.');40 }4142 return file;43};4445const handleArguments = (file, args, options = {}) => {46 const parsed = crossSpawn._parse(file, args, options);47 file = parsed.command;48 args = parsed.args;
47 unmodified lines
96 return value;97};9899export function execa(file, args, options) {100 const filePath = getFilePath(file);87 const parsed = handleArguments(file, args, options);101 const parsed = handleArguments(filePath, args, options);102 const stdioStreams = handleInputAsync(parsed.options);103 validateTimeout(parsed.options);10491 const command = joinCommand(file, args);92 const escapedCommand = getEscapedCommand(file, args);105 const command = joinCommand(filePath, args);106 const escapedCommand = getEscapedCommand(filePath, args);107 logCommand(escapedCommand, parsed.options);108109 let spawned;110 try {
78 unmodified lines
189 return spawned;190}191192export function execaSync(file, args, options) {193 const filePath = getFilePath(file);179 const parsed = handleArguments(file, args, options);194 const parsed = handleArguments(filePath, args, options);195 const stdioStreams = handleInputSync(parsed.options);196182 const command = joinCommand(file, args);183 const escapedCommand = getEscapedCommand(file, args);197 const command = joinCommand(filePath, args);198 const escapedCommand = getEscapedCommand(filePath, args);199 logCommand(escapedCommand, parsed.options);200201 let result;202 try {
113 unmodified lines
316 return execa(317 nodePath,318 [319 ...nodeOptions,305 scriptPath,320 getFilePath(scriptPath),321 ...(Array.isArray(args) ? args : []),322 ],323 {324 ...options,
8 unmodified lines
modified:index.test-d.ts+6 added
228 unmodified lines
229execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: false});230execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: 42});231execa('unicorns').kill('SIGKILL', {forceKillAfterTimeout: undefined});232233expectError(execa(['unicorns', 'arg']));234expectType<ExecaChildProcess>(execa('unicorns'));235expectType<ExecaChildProcess>(execa(new URL('file:///test')));236expectType<ExecaReturnValue>(await execa('unicorns'));237expectType<ExecaReturnValue>(238 await execa('unicorns', {encoding: 'utf8'}),239);
4 unmodified lines
244expectType<ExecaReturnValue<Uint8Array>>(245 await execa('unicorns', ['foo'], {encoding: 'buffer'}),246);247248expectError(execaSync(['unicorns', 'arg']));249expectType<ExecaSyncReturnValue>(execaSync('unicorns'));250expectType<ExecaSyncReturnValue>(execaSync(new URL('file:///test')));251expectType<ExecaSyncReturnValue>(252 execaSync('unicorns', {encoding: 'utf8'}),253);254expectType<ExecaSyncReturnValue<Uint8Array>>(
18 unmodified lines
273expectType<ExecaSyncReturnValue<Uint8Array>>(execaCommandSync('unicorns', {encoding: 'buffer'}));274expectType<ExecaSyncReturnValue>(execaCommandSync('unicorns foo', {encoding: 'utf8'}));275expectType<ExecaSyncReturnValue<Uint8Array>>(execaCommandSync('unicorns foo', {encoding: 'buffer'}));276277expectError(execaNode(['unicorns', 'arg']));278expectType<ExecaChildProcess>(execaNode('unicorns'));279expectType<ExecaReturnValue>(await execaNode('unicorns'));280expectType<ExecaReturnValue>(await execaNode(new URL('file:///test')));281expectType<ExecaReturnValue>(282 await execaNode('unicorns', {encoding: 'utf8'}),283);284expectType<ExecaReturnValue<Uint8Array>>(await execaNode('unicorns', {encoding: 'buffer'}));
71 unmodified lines
modified:readme.md+2 added-2 removed
236 unmodified lines
237### Methods238239#### execa(file, arguments?, options?)240241Executes a command using `file ...arguments`. `arguments` are specified as an array of strings. Returns a [`childProcess`](#childprocess).241Executes a command using `file ...arguments`. `file` is a string or a file URL. `arguments` are an array of strings. Returns a [`childProcess`](#childprocess).242243Arguments are [automatically escaped](#shell-syntax). They can contain any character, including spaces.244245This is the preferred method when executing single commands.246247#### execaNode(scriptPath, arguments?, options?)248249Executes a Node.js file using `node scriptPath ...arguments`. `arguments` are specified as an array of strings. Returns a [`childProcess`](#childprocess).249Executes a Node.js file using `node scriptPath ...arguments`. `file` is a string or a file URL. `arguments` are an array of strings. Returns a [`childProcess`](#childprocess).250251Arguments are [automatically escaped](#shell-syntax). They can contain any character, including spaces.252253This is the preferred method when executing Node.js files.
568 unmodified lines