npm-run-path
Advanced tools
Comparing version 5.2.0 to 5.3.0
@@ -1,2 +0,2 @@ | ||
export interface RunPathOptions { | ||
type CommonOptions = { | ||
/** | ||
@@ -10,43 +10,47 @@ Working directory. | ||
/** | ||
PATH to be appended. Default: [`PATH`](https://github.com/sindresorhus/path-key). | ||
The path to the current Node.js executable. | ||
Set it to an empty string to exclude the default PATH. | ||
*/ | ||
readonly path?: string; | ||
/** | ||
Path to the Node.js executable to use in child processes if that is different from the current one. Its directory is pushed to the front of PATH. | ||
This can be either an absolute path or a path relative to the `cwd` option. | ||
@default process.execPath | ||
@default [process.execPath](https://nodejs.org/api/process.html#processexecpath) | ||
*/ | ||
readonly execPath?: string | URL; | ||
} | ||
export type ProcessEnv = Record<string, string | undefined>; | ||
export interface EnvOptions { | ||
/** | ||
The working directory. | ||
Whether to push the current Node.js executable's directory (`execPath` option) to the front of PATH. | ||
@default process.cwd() | ||
@default true | ||
*/ | ||
readonly cwd?: string | URL; | ||
readonly addExecPath?: boolean; | ||
/** | ||
Accepts an object of environment variables, like `process.env`, and modifies the PATH using the correct [PATH key](https://github.com/sindresorhus/path-key). Use this if you're modifying the PATH for use in the `child_process` options. | ||
Whether to push the locally installed binaries' directory to the front of PATH. | ||
@default true | ||
*/ | ||
readonly env?: ProcessEnv; | ||
readonly preferLocal?: boolean; | ||
}; | ||
export type RunPathOptions = CommonOptions & { | ||
/** | ||
The path to the current Node.js executable. Its directory is pushed to the front of PATH. | ||
PATH to be appended. | ||
This can be either an absolute path or a path relative to the `cwd` option. | ||
Set it to an empty string to exclude the default PATH. | ||
@default process.execPath | ||
@default [`PATH`](https://github.com/sindresorhus/path-key) | ||
*/ | ||
readonly execPath?: string | URL; | ||
} | ||
readonly path?: string; | ||
}; | ||
export type ProcessEnv = Record<string, string | undefined>; | ||
export type EnvOptions = CommonOptions & { | ||
/** | ||
Accepts an object of environment variables, like `process.env`, and modifies the PATH using the correct [PATH key](https://github.com/sindresorhus/path-key). Use this if you're modifying the PATH for use in the `child_process` options. | ||
@default [process.env](https://nodejs.org/api/process.html#processenv) | ||
*/ | ||
readonly env?: ProcessEnv; | ||
}; | ||
/** | ||
@@ -72,2 +76,4 @@ Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries. | ||
/** | ||
Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries. | ||
@returns The augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object. | ||
@@ -74,0 +80,0 @@ |
55
index.js
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 = []; | ||
@@ -24,17 +36,18 @@ while (previous !== cwdPath) { | ||
} | ||
}; | ||
// Ensure the running `node` binary is used. | ||
result.push(path.resolve(cwdString, execPathString, '..')); | ||
// 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, '..')); | ||
}; | ||
return [...result, path_].join(path.delimiter); | ||
} | ||
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; | ||
} | ||
}; |
{ | ||
"name": "npm-run-path", | ||
"version": "5.2.0", | ||
"version": "5.3.0", | ||
"description": "Get your PATH prepended with locally installed binaries", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -35,9 +35,19 @@ # npm-run-path | ||
`options`: [`Options`](#options)\ | ||
_Returns_: `string` | ||
Returns the augmented PATH string. | ||
#### options | ||
### npmRunPathEnv(options?) | ||
`options`: [`Options`](#options)\ | ||
_Returns_: `object` | ||
Returns the augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object. | ||
### options | ||
Type: `object` | ||
##### cwd | ||
#### cwd | ||
@@ -49,50 +59,45 @@ Type: `string | URL`\ | ||
##### path | ||
#### execPath | ||
Type: `string`\ | ||
Default: [`PATH`](https://github.com/sindresorhus/path-key) | ||
Type: `string | URL`\ | ||
Default: [`process.execPath`](https://nodejs.org/api/process.html#processexecpath) | ||
The PATH to be appended. | ||
The path to the current Node.js executable. | ||
Set it to an empty string to exclude the default PATH. | ||
This can be either an absolute path or a path relative to the [`cwd` option](#cwd). | ||
##### execPath | ||
#### addExecPath | ||
Type: `string | URL`\ | ||
Default: `process.execPath` | ||
Type: `boolean`\ | ||
Default: `true` | ||
The path to the current Node.js executable. Its directory is pushed to the front of PATH. | ||
Whether to push the current Node.js executable's directory ([`execPath`](#execpath) option) to the front of PATH. | ||
This can be either an absolute path or a path relative to the [`cwd` option](#cwd). | ||
#### preferLocal | ||
### npmRunPathEnv(options?) | ||
Type: `boolean`\ | ||
Default: `true` | ||
Returns the augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object. | ||
Whether to push the locally installed binaries' directory to the front of PATH. | ||
#### options | ||
#### path | ||
Type: `object` | ||
Type: `string`\ | ||
Default: [`PATH`](https://github.com/sindresorhus/path-key) | ||
##### cwd | ||
The PATH to be appended. | ||
Type: `string | URL`\ | ||
Default: `process.cwd()` | ||
Set it to an empty string to exclude the default PATH. | ||
The working directory. | ||
Only available with [`npmRunPath()`](#npmrunpathoptions), not [`npmRunPathEnv()`](#npmrunpathenvoptions). | ||
##### env | ||
#### env | ||
Type: `object` | ||
Type: `object`\ | ||
Default: [`process.env`](https://nodejs.org/api/process.html#processenv) | ||
Accepts an object of environment variables, like `process.env`, and modifies the PATH using the correct [PATH key](https://github.com/sindresorhus/path-key). Use this if you're modifying the PATH for use in the `child_process` options. | ||
##### execPath | ||
Only available with [`npmRunPathEnv()`](#npmrunpathenvoptions), not [`npmRunPath()`](#npmrunpathoptions). | ||
Type: `string`\ | ||
Default: `process.execPath` | ||
The path to the Node.js executable to use in child processes if that is different from the current one. Its directory is pushed to the front of PATH. | ||
This can be either an absolute path or a path relative to the [`cwd` option](#cwd). | ||
## Related | ||
@@ -99,0 +104,0 @@ |
8471
109
105