Skip to content

Commit 87e4faf

Browse files
committed
Require Node.js 12.20 and move to ESM
1 parent 638d958 commit 87e4faf

File tree

8 files changed

+129
-145
lines changed

8 files changed

+129
-145
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13-
- 14
14-
- 12
15-
- 10
16-
- 8
13+
- 16
1714
steps:
1815
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
16+
- uses: actions/setup-node@v2
2017
with:
2118
node-version: ${{ matrix.node-version }}
2219
- run: npm install

index.d.ts

Lines changed: 62 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,84 @@
1-
declare namespace npmRunPath {
2-
interface RunPathOptions {
3-
/**
4-
Working directory.
1+
export interface RunPathOptions {
2+
/**
3+
Working directory.
54
6-
@default process.cwd()
7-
*/
8-
readonly cwd?: string;
5+
@default process.cwd()
6+
*/
7+
readonly cwd?: string;
98

10-
/**
11-
PATH to be appended. Default: [`PATH`](https://github.com/sindresorhus/path-key).
9+
/**
10+
PATH to be appended. Default: [`PATH`](https://github.com/sindresorhus/path-key).
1211
13-
Set it to an empty string to exclude the default PATH.
14-
*/
15-
readonly path?: string;
12+
Set it to an empty string to exclude the default PATH.
13+
*/
14+
readonly path?: string;
1615

17-
/**
18-
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.
16+
/**
17+
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.
1918
20-
This can be either an absolute path or a path relative to the `cwd` option.
19+
This can be either an absolute path or a path relative to the `cwd` option.
2120
22-
@default process.execPath
23-
*/
24-
readonly execPath?: string;
25-
}
21+
@default process.execPath
22+
*/
23+
readonly execPath?: string;
24+
}
2625

27-
interface ProcessEnv {
28-
[key: string]: string | undefined;
29-
}
26+
export type ProcessEnv = Record<string, string | undefined>;
3027

31-
interface EnvOptions {
32-
/**
33-
Working directory.
28+
export interface EnvOptions {
29+
/**
30+
The working directory.
3431
35-
@default process.cwd()
36-
*/
37-
readonly cwd?: string;
32+
@default process.cwd()
33+
*/
34+
readonly cwd?: string;
3835

39-
/**
40-
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.
41-
*/
42-
readonly env?: ProcessEnv;
36+
/**
37+
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.
38+
*/
39+
readonly env?: ProcessEnv;
4340

44-
/**
45-
Path to the current Node.js executable. Its directory is pushed to the front of PATH.
41+
/**
42+
The path to the current Node.js executable. Its directory is pushed to the front of PATH.
4643
47-
This can be either an absolute path or a path relative to the `cwd` option.
44+
This can be either an absolute path or a path relative to the `cwd` option.
4845
49-
@default process.execPath
50-
*/
51-
readonly execPath?: string;
52-
}
46+
@default process.execPath
47+
*/
48+
readonly execPath?: string;
5349
}
5450

55-
declare const npmRunPath: {
56-
/**
57-
Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries.
51+
/**
52+
Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries.
5853
59-
@returns The augmented path string.
54+
@returns The augmented path string.
6055
61-
@example
62-
```
63-
import * as childProcess from 'child_process';
64-
import npmRunPath = require('npm-run-path');
56+
@example
57+
```
58+
import childProcess from 'node:child_process';
59+
import {npmRunPath} from 'npm-run-path';
6560
66-
console.log(process.env.PATH);
67-
//=> '/usr/local/bin'
61+
console.log(process.env.PATH);
62+
//=> '/usr/local/bin'
6863
69-
console.log(npmRunPath());
70-
//=> '/Users/sindresorhus/dev/foo/node_modules/.bin:/Users/sindresorhus/dev/node_modules/.bin:/Users/sindresorhus/node_modules/.bin:/Users/node_modules/.bin:/node_modules/.bin:/usr/local/bin'
64+
console.log(npmRunPath());
65+
//=> '/Users/sindresorhus/dev/foo/node_modules/.bin:/Users/sindresorhus/dev/node_modules/.bin:/Users/sindresorhus/node_modules/.bin:/Users/node_modules/.bin:/node_modules/.bin:/usr/local/bin'
66+
```
67+
*/
68+
export function npmRunPath(options?: RunPathOptions): string;
7169

72-
// `foo` is a locally installed binary
73-
childProcess.execFileSync('foo', {
74-
env: npmRunPath.env()
75-
});
76-
```
77-
*/
78-
(options?: npmRunPath.RunPathOptions): string;
79-
80-
/**
81-
@returns The augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object.
82-
*/
83-
env(options?: npmRunPath.EnvOptions): npmRunPath.ProcessEnv;
70+
/**
71+
@returns The augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object.
8472
85-
// TODO: Remove this for the next major release
86-
default: typeof npmRunPath;
87-
};
73+
@example
74+
```
75+
import childProcess from 'node:child_process';
76+
import {npmRunPathEnv} from 'npm-run-path';
8877
89-
export = npmRunPath;
78+
// `foo` is a locally installed binary
79+
childProcess.execFileSync('foo', {
80+
env: npmRunPathEnv()
81+
});
82+
```
83+
*/
84+
export function npmRunPathEnv(options?: EnvOptions): ProcessEnv;

index.js

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
'use strict';
2-
const path = require('path');
3-
const pathKey = require('path-key');
4-
5-
const npmRunPath = options => {
6-
options = {
7-
cwd: process.cwd(),
8-
path: process.env[pathKey()],
9-
execPath: process.execPath,
10-
...options
11-
};
1+
import process from 'node:process';
2+
import path from 'node:path';
3+
import pathKey from 'path-key';
4+
5+
export function npmRunPath(options) {
6+
const {
7+
cwd = process.cwd(),
8+
path: path_ = process.env[pathKey()],
9+
execPath = process.execPath,
10+
} = options;
1211

1312
let previous;
14-
let cwdPath = path.resolve(options.cwd);
13+
let cwdPath = path.resolve(cwd);
1514
const result = [];
1615

1716
while (previous !== cwdPath) {
@@ -20,28 +19,18 @@ const npmRunPath = options => {
2019
cwdPath = path.resolve(cwdPath, '..');
2120
}
2221

23-
// Ensure the running `node` binary is used
24-
const execPathDir = path.resolve(options.cwd, options.execPath, '..');
25-
result.push(execPathDir);
26-
27-
return result.concat(options.path).join(path.delimiter);
28-
};
22+
// Ensure the running `node` binary is used.
23+
result.push(path.resolve(cwd, execPath, '..'));
2924

30-
module.exports = npmRunPath;
31-
// TODO: Remove this for the next major release
32-
module.exports.default = npmRunPath;
25+
return [...result, path_].join(path.delimiter);
26+
}
3327

34-
module.exports.env = options => {
35-
options = {
36-
env: process.env,
37-
...options
38-
};
28+
export function npmRunPathEnv({env = process.env, ...options} = {}) {
29+
env = {...env};
3930

40-
const env = {...options.env};
4131
const path = pathKey({env});
42-
4332
options.path = env[path];
44-
env[path] = module.exports(options);
33+
env[path] = npmRunPath(options);
4534

4635
return env;
47-
};
36+
}

index.test-d.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
import process from 'node:process';
12
import {expectType} from 'tsd';
2-
import npmRunPath = require('.');
3-
import {ProcessEnv} from '.';
3+
import {npmRunPath, npmRunPathEnv, ProcessEnv} from './index.js';
44

55
expectType<string>(npmRunPath());
66
expectType<string>(npmRunPath({cwd: '/foo'}));
77
expectType<string>(npmRunPath({path: '/usr/local/bin'}));
88
expectType<string>(npmRunPath({execPath: '/usr/local/bin'}));
99

10-
expectType<ProcessEnv>(npmRunPath.env());
11-
expectType<ProcessEnv>(npmRunPath.env({cwd: '/foo'}));
12-
expectType<ProcessEnv>(npmRunPath.env({env: process.env}));
13-
expectType<ProcessEnv>(npmRunPath.env({execPath: '/usr/local/bin'}));
10+
expectType<ProcessEnv>(npmRunPathEnv());
11+
expectType<ProcessEnv>(npmRunPathEnv({cwd: '/foo'}));
12+
expectType<ProcessEnv>(npmRunPathEnv({env: process.env})); // eslint-disable-line @typescript-eslint/no-unsafe-assignment
13+
expectType<ProcessEnv>(npmRunPathEnv({execPath: '/usr/local/bin'}));

license

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

package.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
"description": "Get your PATH prepended with locally installed binaries",
55
"license": "MIT",
66
"repository": "sindresorhus/npm-run-path",
7+
"funding": "https://github.com/sponsors/sindresorhus",
78
"author": {
89
"name": "Sindre Sorhus",
910
"email": "[email protected]",
10-
"url": "sindresorhus.com"
11+
"url": "https://sindresorhus.com"
1112
},
13+
"type": "module",
14+
"exports": "./index.js",
1215
"engines": {
13-
"node": ">=8"
16+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1417
},
1518
"scripts": {
1619
"test": "xo && ava && tsd"
@@ -34,11 +37,11 @@
3437
"executable"
3538
],
3639
"dependencies": {
37-
"path-key": "^3.0.0"
40+
"path-key": "^4.0.0"
3841
},
3942
"devDependencies": {
40-
"ava": "^1.4.1",
41-
"tsd": "^0.7.2",
42-
"xo": "^0.24.0"
43+
"ava": "^3.15.0",
44+
"tsd": "^0.17.0",
45+
"xo": "^0.45.0"
4346
}
4447
}

0 commit comments

Comments
 (0)