Skip to content

Commit b610ebf

Browse files
committed
Fix logic for package manager detection
1 parent 78405ea commit b610ebf

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/PackageManagers.res

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
open Node
2+
13
@scope(("process", "env"))
2-
external execpath: option<string> = "npm_execpath"
4+
external npm_execpath: option<string> = "npm_execpath"
5+
6+
let compatiblePackageManagers = ["pnpm", "npm", "yarn", "bun"]
7+
8+
let isCompatiblePackageManager = execPath => {
9+
let filename = Path.parse(execPath).name
310

4-
// pnpm must be before npm in this array, as npm is a substring of it
5-
let packageManagers = ["pnpm", "npm", "yarn", "bun"]
6-
let defaultPackageManager = "npm"
11+
// Note: exec path may be something like
12+
// /usr/local/lib/node_modules/npm/bin/npm-cli.js
13+
// So we have to check for substrings here.
14+
compatiblePackageManagers->Array.some(pm => filename->String.includes(pm))
15+
}
716

817
let getActivePackageManager = () =>
9-
execpath
10-
->Option.flatMap(execpath => packageManagers->Array.find(pm => execpath->String.includes(pm)))
11-
->Option.getOr(defaultPackageManager)
18+
switch npm_execpath {
19+
| Some(execPath) if isCompatiblePackageManager(execPath) => execPath
20+
| _ => "npm"
21+
}

src/bindings/Node.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ module Path = {
3838
external join: array<string> => string = "join"
3939

4040
@module("node:path") external join2: (string, string) => string = "join"
41+
42+
type parseResult = {
43+
root: string,
44+
dir: string,
45+
base: string,
46+
ext: string,
47+
name: string,
48+
}
49+
50+
@module("node:path") external parse: string => parseResult = "parse"
4151
}
4252

4353
module Process = {

0 commit comments

Comments
 (0)