-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathPackageManagers.res
More file actions
54 lines (45 loc) · 1.73 KB
/
PackageManagers.res
File metadata and controls
54 lines (45 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
open Node
type packageManager =
| Npm
| Yarn1
| YarnBerry
| Pnpm
| Bun
type packageManagerInfo = {
packageManager: packageManager,
command: string,
}
let defaultPackagerInfo = {packageManager: Npm, command: "npm"}
@scope(("process", "env"))
external npm_execpath: option<string> = "npm_execpath"
let getPackageManagerInfo = async () =>
switch npm_execpath {
| None => defaultPackagerInfo
| Some(execPath) =>
// #58: Windows: packageManager may be something like
// "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js".
//
// Therefore, packageManager needs to be in quotes, and we need to prepend "node "
// if packageManager points to a JS file, otherwise the invocation will hang.
let maybeNode = execPath->String.endsWith("js") ? "node " : ""
let command = `${maybeNode}"${execPath}"`
// Note: exec path may be something like
// /usr/local/lib/node_modules/npm/bin/npm-cli.js
// So we have to check for substrings here.
let filename = Path.parse(execPath).name->String.toLowerCase
let packageManager = switch () {
| _ if filename->String.includes("yarn") =>
let versionResult = await Promisified.ChildProcess.exec(`${command} --version`)
let version = versionResult.stdout->String.trim
let isYarn1 = CompareVersions.compareVersions(version, "2.0.0")->Ordering.isLess
Some(isYarn1 ? Yarn1 : YarnBerry)
| _ if filename->String.includes("pnpm") => Some(Pnpm)
| _ if filename->String.includes("npm") => Some(Npm) // make sure this goes after pnpm ...
| _ if filename->String.includes("bun") => Some(Bun)
| _ => None
}
switch packageManager {
| Some(packageManager) => {packageManager, command}
| None => defaultPackagerInfo
}
}