Skip to content

Commit 51a4da7

Browse files
authored
fix: should infer package manager from config if there's no lockfile in the project (#5150)
* fix: should infer package manager from config if there's no lockfile in the project * fixup! fix: should infer package manager from config if there's no lockfile in the project
1 parent f5f4de0 commit 51a4da7

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

packages/@vue/cli-shared-utils/lib/env.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ function checkPnpm (result) {
130130
return result
131131
}
132132

133+
const _npmProjects = new LRU({
134+
max: 10,
135+
maxAge: 1000
136+
})
137+
exports.hasProjectNpm = (cwd) => {
138+
if (_npmProjects.has(cwd)) {
139+
return true
140+
}
141+
142+
const lockFile = path.join(cwd, 'package-lock.json')
143+
const result = fs.existsSync(lockFile)
144+
_npmProjects.set(cwd, result)
145+
return result
146+
}
147+
133148
// OS
134149
exports.isWindows = process.platform === 'win32'
135150
exports.isMacintosh = process.platform === 'darwin'

packages/@vue/cli/lib/util/ProjectPackageManager.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
hasPnpm3OrLater,
1919
hasPnpmVersionOrLater,
2020
hasProjectPnpm,
21+
hasProjectNpm,
2122

2223
isOfficialPlugin,
2324
resolvePluginId,
@@ -88,8 +89,17 @@ class PackageManager {
8889
if (forcePackageManager) {
8990
this.bin = forcePackageManager
9091
} else if (context) {
91-
this.bin = hasProjectYarn(context) ? 'yarn' : hasProjectPnpm(context) ? 'pnpm' : 'npm'
92-
} else {
92+
if (hasProjectYarn(context)) {
93+
this.bin = 'yarn'
94+
} else if (hasProjectPnpm(context)) {
95+
this.bin = 'pnpm'
96+
} else if (hasProjectNpm(context)) {
97+
this.bin = 'npm'
98+
}
99+
}
100+
101+
// if no package managers specified, and no lockfile exists
102+
if (!this.bin) {
93103
this.bin = loadOptions().packageManager || (hasYarn() ? 'yarn' : hasPnpm3OrLater() ? 'pnpm' : 'npm')
94104
}
95105

0 commit comments

Comments
 (0)