|
1 | | -// copy from https://github.com/egoist/detect-package-manager/blob/main/src/index.ts |
2 | | -// and merge https://github.com/egoist/detect-package-manager/pull/9 to support Monorepo |
3 | | -import { resolve, dirname } from "path" |
4 | | -import { exec, exists } from "builder-util" |
5 | | - |
6 | | -export type PM = "npm" | "yarn" | "pnpm" | "bun" |
7 | | - |
8 | | -const cache = new Map() |
9 | | -const globalInstallationCache = new Map<string, boolean>() |
10 | | -const lockfileCache = new Map<string, PM>() |
11 | | - |
12 | | -/** |
13 | | - * Check if a global pm is available |
14 | | - */ |
15 | | -function hasGlobalInstallation(pm: PM): Promise<boolean> { |
16 | | - const key = `has_global_${pm}` |
17 | | - if (globalInstallationCache.has(key)) { |
18 | | - return Promise.resolve(globalInstallationCache.get(key)!) |
19 | | - } |
| 1 | +import * as path from "path" |
| 2 | +import * as fs from "fs" |
20 | 3 |
|
21 | | - return exec(pm, ["--version"], { shell: true }) |
22 | | - .then(res => { |
23 | | - return /^\d+.\d+.\d+$/.test(res) |
24 | | - }) |
25 | | - .then(value => { |
26 | | - globalInstallationCache.set(key, value) |
27 | | - return value |
28 | | - }) |
29 | | - .catch(() => false) |
| 4 | +export enum PM { |
| 5 | + NPM = "npm", |
| 6 | + YARN = "yarn", |
| 7 | + PNPM = "pnpm", |
| 8 | + YARN_BERRY = "yarn-berry", |
30 | 9 | } |
31 | 10 |
|
32 | | -function getTypeofLockFile(cwd = process.cwd()): Promise<PM> { |
33 | | - const key = `lockfile_${cwd}` |
34 | | - if (lockfileCache.has(key)) { |
35 | | - return Promise.resolve(lockfileCache.get(key)!) |
36 | | - } |
| 11 | +function detectPackageManagerByEnv(): PM { |
| 12 | + if (process.env.npm_config_user_agent) { |
| 13 | + const userAgent = process.env.npm_config_user_agent |
37 | 14 |
|
38 | | - return Promise.all([ |
39 | | - exists(resolve(cwd, "yarn.lock")), |
40 | | - exists(resolve(cwd, "package-lock.json")), |
41 | | - exists(resolve(cwd, "pnpm-lock.yaml")), |
42 | | - exists(resolve(cwd, "bun.lockb")), |
43 | | - ]).then(([isYarn, _, isPnpm, isBun]) => { |
44 | | - let value: PM |
45 | | - |
46 | | - if (isYarn) { |
47 | | - value = "yarn" |
48 | | - } else if (isPnpm) { |
49 | | - value = "pnpm" |
50 | | - } else if (isBun) { |
51 | | - value = "bun" |
52 | | - } else { |
53 | | - value = "npm" |
| 15 | + if (userAgent.includes("pnpm")) { |
| 16 | + return PM.PNPM |
54 | 17 | } |
55 | 18 |
|
56 | | - cache.set(key, value) |
57 | | - return value |
58 | | - }) |
59 | | -} |
| 19 | + if (userAgent.includes("yarn")) { |
| 20 | + if (userAgent.includes("yarn/")) { |
| 21 | + const version = userAgent.match(/yarn\/(\d+)\./) |
| 22 | + if (version && parseInt(version[1]) >= 2) { |
| 23 | + return PM.YARN_BERRY |
| 24 | + } |
| 25 | + } |
| 26 | + return PM.YARN |
| 27 | + } |
60 | 28 |
|
61 | | -export const detect = async ({ cwd, includeGlobalBun }: { cwd?: string; includeGlobalBun?: boolean } = {}) => { |
62 | | - let type = await getTypeofLockFile(cwd) |
63 | | - if (type) { |
64 | | - return type |
| 29 | + if (userAgent.includes("npm")) { |
| 30 | + return PM.NPM |
| 31 | + } |
65 | 32 | } |
66 | 33 |
|
67 | | - let tmpCwd = cwd || "." |
68 | | - for (let i = 1; i <= 5; i++) { |
69 | | - tmpCwd = dirname(tmpCwd) |
70 | | - type = await getTypeofLockFile(tmpCwd) |
71 | | - if (type) { |
72 | | - return type |
| 34 | + if (process.env.npm_execpath) { |
| 35 | + const execPath = process.env.npm_execpath.toLowerCase() |
| 36 | + |
| 37 | + if (execPath.includes("pnpm")) { |
| 38 | + return PM.PNPM |
| 39 | + } |
| 40 | + |
| 41 | + if (execPath.includes("yarn")) { |
| 42 | + if (execPath.includes("berry") || process.env.YARN_VERSION?.startsWith("2.") || process.env.YARN_VERSION?.startsWith("3.")) { |
| 43 | + return PM.YARN_BERRY |
| 44 | + } |
| 45 | + return PM.YARN |
| 46 | + } |
| 47 | + |
| 48 | + if (execPath.includes("npm")) { |
| 49 | + return PM.NPM |
73 | 50 | } |
74 | 51 | } |
75 | 52 |
|
76 | | - if (await hasGlobalInstallation("yarn")) { |
77 | | - return "yarn" |
| 53 | + if (process.env.PNPM_HOME) { |
| 54 | + return PM.PNPM |
78 | 55 | } |
79 | | - if (await hasGlobalInstallation("pnpm")) { |
80 | | - return "yarn" |
| 56 | + |
| 57 | + if (process.env.YARN_REGISTRY) { |
| 58 | + if (process.env.YARN_VERSION?.startsWith("2.") || process.env.YARN_VERSION?.startsWith("3.")) { |
| 59 | + return PM.YARN_BERRY |
| 60 | + } |
| 61 | + return PM.YARN |
81 | 62 | } |
82 | 63 |
|
83 | | - if (includeGlobalBun && (await hasGlobalInstallation("bun"))) { |
84 | | - return "bun" |
| 64 | + if (process.env.npm_package_json) { |
| 65 | + return PM.NPM |
85 | 66 | } |
86 | | - return "npm" |
| 67 | + |
| 68 | + // return default |
| 69 | + return PM.NPM |
87 | 70 | } |
88 | 71 |
|
89 | | -export function getPackageManagerVersion(pm: PM) { |
90 | | - return exec(pm, ["--version"], { shell: true }).then(res => res.trim()) |
| 72 | +export function getPackageManagerCommand(pm: PM) { |
| 73 | + let cmd = pm |
| 74 | + if (pm === PM.YARN_BERRY || process.env.FORCE_YARN === "true") { |
| 75 | + cmd = PM.YARN |
| 76 | + } |
| 77 | + return `${cmd}${process.platform === "win32" ? ".cmd" : ""}` |
91 | 78 | } |
92 | 79 |
|
93 | | -export function clearCache() { |
94 | | - return cache.clear() |
| 80 | +export function detectPackageManager(cwd: string) { |
| 81 | + const isYarnLockFileExists = fs.existsSync(path.join(cwd, "yarn.lock")) |
| 82 | + const isPnpmLockFileExists = fs.existsSync(path.join(cwd, "pnpm-lock.yaml")) |
| 83 | + const isNpmLockFileExists = fs.existsSync(path.join(cwd, "package-lock.json")) |
| 84 | + |
| 85 | + if (isYarnLockFileExists && !isPnpmLockFileExists && !isNpmLockFileExists) { |
| 86 | + // check if yarn is berry |
| 87 | + const pm = detectPackageManagerByEnv() |
| 88 | + if (pm === PM.YARN_BERRY) { |
| 89 | + return PM.YARN_BERRY |
| 90 | + } |
| 91 | + return PM.YARN |
| 92 | + } |
| 93 | + |
| 94 | + if (isPnpmLockFileExists && !isYarnLockFileExists && !isNpmLockFileExists) { |
| 95 | + return PM.PNPM |
| 96 | + } |
| 97 | + |
| 98 | + if (isNpmLockFileExists && !isYarnLockFileExists && !isPnpmLockFileExists) { |
| 99 | + return PM.NPM |
| 100 | + } |
| 101 | + // if there are no lock files or multiple lock files, return the package manager from env |
| 102 | + return detectPackageManagerByEnv() |
95 | 103 | } |
0 commit comments