-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
164 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,52 @@ | ||
import process from 'node:process'; | ||
import path from 'node:path'; | ||
import url from 'node:url'; | ||
import {fileURLToPath} from 'node:url'; | ||
import pathKey from 'path-key'; | ||
|
||
export function npmRunPath(options = {}) { | ||
const { | ||
cwd = process.cwd(), | ||
path: path_ = process.env[pathKey()], | ||
execPath = process.execPath, | ||
} = options; | ||
export const npmRunPath = ({ | ||
cwd = process.cwd(), | ||
path: pathOption = process.env[pathKey()], | ||
preferLocal = true, | ||
execPath = process.execPath, | ||
addExecPath = true, | ||
} = {}) => { | ||
const cwdString = cwd instanceof URL ? fileURLToPath(cwd) : cwd; | ||
const cwdPath = path.resolve(cwdString); | ||
const result = []; | ||
|
||
if (preferLocal) { | ||
applyPreferLocal(result, cwdPath); | ||
} | ||
|
||
if (addExecPath) { | ||
applyExecPath(result, execPath, cwdPath); | ||
} | ||
|
||
return [...result, pathOption].join(path.delimiter); | ||
}; | ||
|
||
const applyPreferLocal = (result, cwdPath) => { | ||
let previous; | ||
const execPathString = execPath instanceof URL ? url.fileURLToPath(execPath) : execPath; | ||
const cwdString = cwd instanceof URL ? url.fileURLToPath(cwd) : cwd; | ||
let cwdPath = path.resolve(cwdString); | ||
const result = []; | ||
|
||
while (previous !== cwdPath) { | ||
result.push(path.join(cwdPath, 'node_modules/.bin')); | ||
previous = cwdPath; | ||
cwdPath = path.resolve(cwdPath, '..'); | ||
} | ||
}; | ||
|
||
// Ensure the running `node` binary is used. | ||
result.push(path.resolve(cwdString, execPathString, '..')); | ||
|
||
return [...result, path_].join(path.delimiter); | ||
} | ||
// Ensure the running `node` binary is used | ||
const applyExecPath = (result, execPath, cwdPath) => { | ||
const execPathString = execPath instanceof URL ? fileURLToPath(execPath) : execPath; | ||
result.push(path.resolve(cwdPath, execPathString, '..')); | ||
}; | ||
|
||
export function npmRunPathEnv({env = process.env, ...options} = {}) { | ||
export const npmRunPathEnv = ({env = process.env, ...options} = {}) => { | ||
env = {...env}; | ||
|
||
const path = pathKey({env}); | ||
options.path = env[path]; | ||
env[path] = npmRunPath(options); | ||
const pathName = pathKey({env}); | ||
options.path = env[pathName]; | ||
env[pathName] = npmRunPath(options); | ||
|
||
return env; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,38 @@ | ||
import process from 'node:process'; | ||
import {expectType} from 'tsd'; | ||
import {expectType, expectError} from 'tsd'; | ||
import {npmRunPath, npmRunPathEnv, ProcessEnv} from './index.js'; | ||
|
||
const fileUrl = new URL('file:///foo'); | ||
|
||
expectType<string>(npmRunPath()); | ||
expectType<string>(npmRunPath({cwd: '/foo'})); | ||
expectType<string>(npmRunPath({cwd: new URL('file:///foo')})); | ||
expectType<string>(npmRunPath({cwd: fileUrl})); | ||
expectError(npmRunPath({cwd: false})); | ||
expectType<string>(npmRunPath({path: '/usr/local/bin'})); | ||
expectError(npmRunPath({path: fileUrl})); | ||
expectError(npmRunPath({path: false})); | ||
expectType<string>(npmRunPath({execPath: '/usr/local/bin'})); | ||
expectType<string>(npmRunPath({execPath: new URL('file:///usr/local/bin')})); | ||
expectType<string>(npmRunPath({execPath: fileUrl})); | ||
expectError(npmRunPath({execPath: false})); | ||
expectType<string>(npmRunPath({addExecPath: false})); | ||
expectError(npmRunPath({addExecPath: ''})); | ||
expectType<string>(npmRunPath({preferLocal: false})); | ||
expectError(npmRunPath({preferLocal: ''})); | ||
|
||
expectType<ProcessEnv>(npmRunPathEnv()); | ||
expectType<ProcessEnv>(npmRunPathEnv({cwd: '/foo'})); | ||
expectType<ProcessEnv>(npmRunPathEnv({cwd: new URL('file:///foo')})); | ||
expectType<ProcessEnv>(npmRunPathEnv({cwd: fileUrl})); | ||
expectError(npmRunPathEnv({cwd: false})); | ||
expectType<ProcessEnv>(npmRunPathEnv({env: process.env})); // eslint-disable-line @typescript-eslint/no-unsafe-assignment | ||
expectType<ProcessEnv>(npmRunPathEnv({env: {foo: 'bar'}})); | ||
expectType<ProcessEnv>(npmRunPathEnv({env: {foo: undefined}})); | ||
expectError(npmRunPath({env: false})); | ||
expectError(npmRunPath({env: {[Symbol('key')]: 'bar'}})); | ||
expectError(npmRunPath({env: {foo: false}})); | ||
expectType<ProcessEnv>(npmRunPathEnv({execPath: '/usr/local/bin'})); | ||
expectType<ProcessEnv>(npmRunPathEnv({execPath: new URL('file:///usr/local/bin')})); | ||
expectType<ProcessEnv>(npmRunPathEnv({execPath: fileUrl})); | ||
expectError(npmRunPath({execPath: false})); | ||
expectType<ProcessEnv>(npmRunPathEnv({addExecPath: false})); | ||
expectError(npmRunPathEnv({addExecPath: ''})); | ||
expectType<ProcessEnv>(npmRunPathEnv({preferLocal: false})); | ||
expectError(npmRunPathEnv({preferLocal: ''})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters