Skip to content

Commit 9c3300c

Browse files
committed
Tweaks
1 parent 3ef8d0a commit 9c3300c

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

index.js

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import process from 'node:process';
22
import path from 'node:path';
3-
import {fileURLToPath} from 'node:url';
43
import pathKey from 'path-key';
4+
import {toPath, traversePathUp} from 'unicorn-magic';
55

66
export const npmRunPath = ({
77
cwd = process.cwd(),
@@ -10,8 +10,7 @@ export const npmRunPath = ({
1010
execPath = process.execPath,
1111
addExecPath = true,
1212
} = {}) => {
13-
const cwdString = cwd instanceof URL ? fileURLToPath(cwd) : cwd;
14-
const cwdPath = path.resolve(cwdString);
13+
const cwdPath = path.resolve(toPath(cwd));
1514
const result = [];
1615

1716
if (preferLocal) {
@@ -26,19 +25,14 @@ export const npmRunPath = ({
2625
};
2726

2827
const applyPreferLocal = (result, cwdPath) => {
29-
let previous;
30-
31-
while (previous !== cwdPath) {
32-
result.push(path.join(cwdPath, 'node_modules/.bin'));
33-
previous = cwdPath;
34-
cwdPath = path.resolve(cwdPath, '..');
28+
for (const directory of traversePathUp(cwdPath)) {
29+
result.push(path.join(directory, 'node_modules/.bin'));
3530
}
3631
};
3732

3833
// Ensure the running `node` binary is used
3934
const applyExecPath = (result, execPath, cwdPath) => {
40-
const execPathString = execPath instanceof URL ? fileURLToPath(execPath) : execPath;
41-
result.push(path.resolve(cwdPath, execPathString, '..'));
35+
result.push(path.resolve(cwdPath, toPath(execPath), '..'));
4236
};
4337

4438
export const npmRunPathEnv = ({env = process.env, ...options} = {}) => {

package.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
"url": "https://sindresorhus.com"
1212
},
1313
"type": "module",
14-
"exports": "./index.js",
15-
"types": "./index.d.ts",
14+
"exports": {
15+
"types": "./index.d.ts",
16+
"default": "./index.js"
17+
},
1618
"sideEffects": false,
1719
"engines": {
1820
"node": ">=18"
@@ -39,10 +41,11 @@
3941
"executable"
4042
],
4143
"dependencies": {
42-
"path-key": "^4.0.0"
44+
"path-key": "^4.0.0",
45+
"unicorn-magic": "^0.2.0"
4346
},
4447
"devDependencies": {
45-
"ava": "^6.1.2",
48+
"ava": "^6.1.3",
4649
"tsd": "^0.31.0",
4750
"xo": "^0.58.0"
4851
}

0 commit comments

Comments
 (0)