Improve interaction of the `preferLocal`, `node` and `nodePath` options (#841)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 316f562 · Browse files · 5 files·+30 added−21 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+30 added−21 removed
- tests:+22 added−13 removed
- other:+8 added−8 removed
Provenance
- Commit
b965682- Tree
8b589fd- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
316f562
5 files changed+30 added−21 removed
modified:lib/arguments/node.js+1 added-1 removed
26 unmodified lines2728293031313233343511 unmodified lines26 unmodified lines }
const normalizedNodePath = safeNormalizeFileUrl(nodePath, 'The "nodePath" option'); const resolvedNodePath = resolve(cwd, normalizedNodePath); const newOptions = {...options, nodePath: resolvedNodePath, cwd}; const newOptions = {...options, nodePath: resolvedNodePath, node: shouldHandleNode, cwd};
if (!shouldHandleNode) { return [file, args, newOptions]; }11 unmodified linesmodified:lib/arguments/options.js+3 added-3 removed
91 unmodified lines9293949596969798991009910010110210310491 unmodified lines});
const DEFAULT_MAX_BUFFER = 1000 * 1000 * 100;
const getEnv = ({env: envOption, extendEnv, preferLocal, localDir, nodePath}) => {const getEnv = ({env: envOption, extendEnv, preferLocal, node, localDir, nodePath}) => { const env = extendEnv ? {...process.env, ...envOption} : envOption;
if (preferLocal) { return npmRunPathEnv({env, cwd: localDir, execPath: nodePath}); if (preferLocal || node) { return npmRunPathEnv({env, cwd: localDir, execPath: nodePath, preferLocal, addExecPath: node}); }
return env;};modified:test/arguments/node.js+22 added-13 removed
95 unmodified lines96 test('The "execPath" option cannot be used - execaNode()', testFormerNodePath, execaNode);97 test('The "execPath" option cannot be used - "node" option', testFormerNodePath, runWithNodeOption);98 test('The "execPath" option cannot be used - "node" option sync', testFormerNodePath, runWithNodeOptionSync);99 100-const nodePathArguments = ['node', ['-p', 'process.env.Path || process.env.PATH']];100+const nodePathArguments = ['-p', ['process.env.Path || process.env.PATH']];101 102-const testChildNodePath = async (t, mapPath) => {102+const testChildNodePath = async (t, execaMethod, mapPath) => {103 const nodePath = mapPath(await getNodePath());104- const {stdout} = await execa(...nodePathArguments, {preferLocal: true, nodePath});104+ const {stdout} = await execaMethod(...nodePathArguments, {nodePath});105 t.true(stdout.includes(TEST_NODE_VERSION));106 };107 108-test.serial('The "nodePath" option impacts the child process', testChildNodePath, identity);109-test.serial('The "nodePath" option can be a file URL', testChildNodePath, pathToFileURL);108+test.serial('The "nodePath" option impacts the child process - execaNode()', testChildNodePath, execaNode, identity);109+test.serial('The "nodePath" option impacts the child process - "node" option', testChildNodePath, runWithNodeOption, identity);110+test.serial('The "nodePath" option impacts the child process - "node" option sync', testChildNodePath, runWithNodeOptionSync, identity);111 111-test('The "nodePath" option defaults to the current Node.js binary in the child process', async t => {112- const {stdout} = await execa(...nodePathArguments, {preferLocal: true});112+const testChildNodePathDefault = async (t, execaMethod) => {113+ const {stdout} = await execaMethod(...nodePathArguments);114 t.true(stdout.includes(dirname(process.execPath)));114-});115+};116 117+test('The "nodePath" option defaults to the current Node.js binary in the child process - execaNode()', testChildNodePathDefault, execaNode);118+test('The "nodePath" option defaults to the current Node.js binary in the child process - "node" option', testChildNodePathDefault, runWithNodeOption);119+test('The "nodePath" option defaults to the current Node.js binary in the child process - "node" option sync', testChildNodePathDefault, runWithNodeOptionSync);120+116-test.serial('The "nodePath" option requires "preferLocal: true" to impact the child process', async t => {121+test.serial('The "nodePath" option requires "node: true" to impact the child process', async t => {122 const nodePath = await getNodePath();118- const {stdout} = await execa(...nodePathArguments, {nodePath});123+ const {stdout} = await execa('node', nodePathArguments.flat(), {nodePath});124 t.false(stdout.includes(TEST_NODE_VERSION));125 });126 122-test.serial('The "nodePath" option is relative to "cwd" when used in the child process', async t => {127+const testChildNodePathCwd = async (t, execaMethod) => {128 const nodePath = await getNodePath();129 const cwd = dirname(dirname(nodePath));130 const relativeExecPath = relative(cwd, nodePath);126- const {stdout} = await execa(...nodePathArguments, {preferLocal: true, nodePath: relativeExecPath, cwd});131+ const {stdout} = await execaMethod(...nodePathArguments, {nodePath: relativeExecPath, cwd});132 t.true(stdout.includes(TEST_NODE_VERSION));128-});133+};134 135+test.serial('The "nodePath" option is relative to "cwd" when used in the child process - execaNode()', testChildNodePathCwd, execaNode);136+test.serial('The "nodePath" option is relative to "cwd" when used in the child process - "node" option', testChildNodePathCwd, runWithNodeOption);137+test.serial('The "nodePath" option is relative to "cwd" when used in the child process - "node" option sync', testChildNodePathCwd, runWithNodeOptionSync);138+139 const testCwdNodePath = async (t, execaMethod) => {140 const nodePath = await getNodePath();141 const cwd = dirname(dirname(nodePath));142 const relativeExecPath = relative(cwd, nodePath);116 unmodified linesmodified:index.d.ts+2 added-2 removed
293 unmodified lines294 295 /**296 Path to the Node.js executable.297 298- When the `node` option is `true`, this is used to to create the child process. When the `preferLocal` option is `true`, this is used in the child process itself.299-298 For example, this can be used together with [`get-node`](https://github.com/ehmicky/get-node) to run a specific Node.js version.299 300+ Requires the `node` option to be `true`.301+302 @default [`process.execPath`](https://nodejs.org/api/process.html#process_process_execpath) (current Node.js executable)303 */304 readonly nodePath?: string | URL;305 1073 unmodified linesmodified:readme.md+2 added-2 removed
625 unmodified lines626 Default: [`process.execPath`](https://nodejs.org/api/process.html#process_process_execpath) (current Node.js executable)627 628 Path to the Node.js executable.629 630-When the [`node`](#node) option is `true`, this is used to to create the child process. When the [`preferLocal`](#preferlocal) option is `true`, this is used in the child process itself.631-630 For example, this can be used together with [`get-node`](https://github.com/ehmicky/get-node) to run a specific Node.js version.631 632+Requires the [`node`](#node) option to be `true`.633+634 #### verbose635 636 Type: `boolean`\637 Default: `false`374 unmodified lines