Skip to content
Myr

Allow `cwd` and `localDir` options to be URLs (#492)

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

authored by ehmicky · committed by GitHub · parent e4b9295 · Browse files · 5 files+39 added−5 removed
This commit

State

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

Diff stats

5 files+39 added−5 removed

  • tests:+24 added−1 removed
  • other:+15 added−4 removed

5 files+39 added−5 removed

modified:test/test.js+24 added-1 removed
1import path from 'node:path';2import process from 'node:process';3import {fileURLToPath} from 'node:url';3import {fileURLToPath, pathToFileURL} from 'node:url';4import test from 'ava';5import isRunning from 'is-running';6import getNode from 'get-node';7import semver from 'semver';8import {execa, execaSync} from '../index.js';910process.env.PATH = fileURLToPath(new URL('./fixtures', import.meta.url)) + path.delimiter + process.env.PATH;11process.env.FOO = 'foo';
186 unmodified lines
198 const {stdout} = await execa('environment.js', [], {env: {BAR: 'bar', PATH: process.env.PATH}, extendEnv: false});199 t.deepEqual(stdout.split('\n'), ['undefined', 'bar']);200});201202test('can use `options.cwd` as a string', async t => {203 const cwd = '/';204 const {stdout} = await execa('node', ['-p', 'process.cwd()'], {cwd});205 t.is(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));206});207208if (semver.satisfies(process.version, '^14.18.0 || >=16.4.0')) {209 test('localDir option can be a URL', async t => {210 const command = process.platform === 'win32' ? 'echo %PATH%' : 'echo $PATH';211 const {stdout} = await execa(command, {shell: true, preferLocal: true, localDir: pathToFileURL('/test')});212 const envPaths = stdout.split(path.delimiter);213 t.true(envPaths.some(envPath => envPath.endsWith('.bin')));214 });215216 test('can use `options.cwd` as a URL', async t => {217 const cwd = '/';218 const cwdUrl = pathToFileURL(cwd);219 const {stdout} = await execa('node', ['-p', 'process.cwd()'], {cwd: cwdUrl});220 t.is(path.toNamespacedPath(stdout), path.toNamespacedPath(cwd));221 });222}223224test('can use `options.shell: true`', async t => {225 const {stdout} = await execa('node test/fixtures/noop.js foo', {shell: true});226 t.is(stdout, 'foo');227});
23 unmodified lines
modified:index.d.ts+6 added-2 removed
31 unmodified lines
3233 /**34 Preferred path to find locally installed binaries in (use with `preferLocal`).3536 Using a `URL` is only supported in Node.js `14.18.0`, `16.14.0` or above.3738 @default process.cwd()39 */38 readonly localDir?: string;40 readonly localDir?: string | URL;4142 /**43 Path to the Node.js executable to use in child processes.44
67 unmodified lines
112113 /**114 Current working directory of the child process.115116 Using a `URL` is only supported in Node.js `14.18.0`, `16.14.0` or above.117118 @default process.cwd()119 */116 readonly cwd?: string;120 readonly cwd?: string | URL;121122 /**123 Environment key-value pairs. Extends automatically from `process.env`. Set `extendEnv` to `false` if you don't want this.124
432 unmodified lines
modified:index.test-d.ts+2 added
88 unmodified lines
89/* eslint-disable @typescript-eslint/no-floating-promises */90execa('unicorns', {cleanup: false});91execa('unicorns', {preferLocal: false});92execa('unicorns', {localDir: '.'});93execa('unicorns', {localDir: new URL('file:///test')});94execa('unicorns', {execPath: '/path'});95execa('unicorns', {buffer: false});96execa('unicorns', {input: ''});97execa('unicorns', {input: Buffer.from('')});
23 unmodified lines
121execa('unicorns', {reject: false});122execa('unicorns', {stripFinalNewline: false});123execa('unicorns', {extendEnv: false});124execa('unicorns', {cwd: '.'});125execa('unicorns', {cwd: new URL('file:///test')});126// eslint-disable-next-line @typescript-eslint/naming-convention127execa('unicorns', {env: {PATH: ''}});128execa('unicorns', {argv0: ''});129execa('unicorns', {stdio: 'pipe'});
78 unmodified lines
modified:package.json+1 added
modified:readme.md+6 added-2 removed
355 unmodified lines
356If you `$ npm install foo`, you can then `execa('foo')`.357358#### localDir359360Type: `string`\360Type: `string | URL`\361Default: `process.cwd()`362363Preferred path to find locally installed binaries in (use with `preferLocal`).364365Using a `URL` is only supported in Node.js `14.18.0`, `16.14.0` or above.366367#### execPath368369Type: `string`\370Default: `process.execPath` (Current Node.js executable)
76 unmodified lines
447Execa also accepts the below options which are the same as the options for [`child_process#spawn()`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)/[`child_process#exec()`](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback)448449#### cwd450449Type: `string`\451Type: `string | URL`\452Default: `process.cwd()`453454Current working directory of the child process.455456Using a `URL` is only supported in Node.js `14.18.0`, `16.14.0` or above.457458#### env459460Type: `object`\461Default: `process.env`
197 unmodified lines