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