mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-03-06 06:18:38 -06:00
fix: better pnpm check
1. separate project pnpm check and global pnpm check 2. rename hasPnpm to hasPnpm3OrLater
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
const {
|
||||
info,
|
||||
hasProjectYarn,
|
||||
hasPnpm,
|
||||
hasPnpm3OrLater,
|
||||
openBrowser,
|
||||
IpcMessenger
|
||||
} = require('@vue/cli-shared-utils')
|
||||
@@ -235,7 +235,7 @@ module.exports = (api, options) => {
|
||||
isFirstCompile = false
|
||||
|
||||
if (!isProduction) {
|
||||
const buildCommand = hasProjectYarn(api.getCwd()) ? `yarn build` : hasPnpm() ? `pnpm run build` : `npm run build`
|
||||
const buildCommand = hasProjectYarn(api.getCwd()) ? `yarn build` : hasPnpm3OrLater() ? `pnpm run build` : `npm run build`
|
||||
console.log(` Note that the development build is not optimized.`)
|
||||
console.log(` To create a production build, run ${chalk.cyan(buildCommand)}.`)
|
||||
} else {
|
||||
|
||||
@@ -14,7 +14,6 @@ const _gitProjects = new LRU({
|
||||
max: 10,
|
||||
maxAge: 1000
|
||||
})
|
||||
let _hasPnpm
|
||||
|
||||
// env detection
|
||||
exports.hasYarn = () => {
|
||||
@@ -79,25 +78,51 @@ exports.hasProjectGit = (cwd) => {
|
||||
return result
|
||||
}
|
||||
|
||||
exports.hasPnpm = () => {
|
||||
let _hasPnpm
|
||||
let _hasPnpm3orLater
|
||||
const _pnpmProjects = new LRU({
|
||||
max: 10,
|
||||
maxAge: 1000
|
||||
})
|
||||
|
||||
exports.hasPnpm3OrLater = () => {
|
||||
if (process.env.VUE_CLI_TEST) {
|
||||
return true
|
||||
}
|
||||
if (_hasPnpm != null) {
|
||||
return _hasPnpm
|
||||
if (_hasPnpm3orLater != null) {
|
||||
return _hasPnpm3orLater
|
||||
}
|
||||
try {
|
||||
const pnpmVersion = execSync('pnpm --version').toString()
|
||||
// there's a critical bug in pnpm 2
|
||||
// https://github.com/pnpm/pnpm/issues/1678#issuecomment-469981972
|
||||
// so we only support pnpm >= 3.0.0
|
||||
_hasPnpm = semver.gte(pnpmVersion, '3.0.0')
|
||||
return _hasPnpm
|
||||
_hasPnpm = true
|
||||
_hasPnpm3orLater = semver.gte(pnpmVersion, '3.0.0')
|
||||
return _hasPnpm3orLater
|
||||
} catch (e) {
|
||||
return (_hasPnpm = false)
|
||||
return (_hasPnpm3orLater = false)
|
||||
}
|
||||
}
|
||||
|
||||
exports.hasProjectPnpm = (cwd) => {
|
||||
if (_pnpmProjects.has(cwd)) {
|
||||
return checkPnpm(_pnpmProjects.get(cwd))
|
||||
}
|
||||
|
||||
const lockFile = path.join(cwd, 'pnpm-lock.yaml')
|
||||
const result = fs.existsSync(lockFile)
|
||||
_pnpmProjects.set(cwd, result)
|
||||
return checkPnpm(result)
|
||||
}
|
||||
|
||||
function checkPnpm (result) {
|
||||
if (result && !exports.hasPnpm3OrLater()) {
|
||||
throw new Error(`The project seems to require pnpm${_hasPnpm ? ' >= 3' : ''} but it's not installed.`)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// OS
|
||||
exports.isWindows = process.platform === 'win32'
|
||||
exports.isMacintosh = process.platform === 'darwin'
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
const {
|
||||
hasYarn,
|
||||
hasProjectYarn,
|
||||
hasPnpm
|
||||
hasPnpm3OrLater,
|
||||
hasProjectPnpm
|
||||
} = require('@vue/cli-shared-utils')
|
||||
const { loadOptions } = require('@vue/cli/lib/options')
|
||||
|
||||
exports.getCommand = function (cwd = undefined) {
|
||||
if (!cwd) {
|
||||
return loadOptions().packageManager || (hasYarn() ? 'yarn' : hasPnpm() ? 'pnpm' : 'npm')
|
||||
return loadOptions().packageManager || (hasYarn() ? 'yarn' : hasPnpm3OrLater() ? 'pnpm' : 'npm')
|
||||
}
|
||||
return hasProjectYarn(cwd) ? 'yarn' : hasPnpm() ? 'pnpm' : 'npm'
|
||||
return hasProjectYarn(cwd) ? 'yarn' : hasProjectPnpm() ? 'pnpm' : 'npm'
|
||||
}
|
||||
|
||||
@@ -133,6 +133,7 @@ import GIT_COMMIT from '@/graphql/git/gitCommit.gql'
|
||||
|
||||
const defaultCollapsed = [
|
||||
'yarn.lock',
|
||||
'pnpm-lock.yaml',
|
||||
'package-lock.json'
|
||||
]
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ const {
|
||||
hasGit,
|
||||
hasProjectGit,
|
||||
hasYarn,
|
||||
hasPnpm,
|
||||
hasPnpm3OrLater,
|
||||
logWithSpinner,
|
||||
stopSpinner,
|
||||
exit,
|
||||
@@ -99,7 +99,7 @@ module.exports = class Creator extends EventEmitter {
|
||||
cliOptions.packageManager ||
|
||||
loadOptions().packageManager ||
|
||||
(hasYarn() ? 'yarn' : null) ||
|
||||
(hasPnpm() ? 'pnpm' : 'npm')
|
||||
(hasPnpm3OrLater() ? 'pnpm' : 'npm')
|
||||
)
|
||||
|
||||
await clearConsole()
|
||||
@@ -412,7 +412,7 @@ module.exports = class Creator extends EventEmitter {
|
||||
|
||||
// ask for packageManager once
|
||||
const savedOptions = loadOptions()
|
||||
if (!savedOptions.packageManager && (hasYarn() || hasPnpm())) {
|
||||
if (!savedOptions.packageManager && (hasYarn() || hasPnpm3OrLater())) {
|
||||
const packageManagerChoices = []
|
||||
|
||||
if (hasYarn()) {
|
||||
@@ -423,7 +423,7 @@ module.exports = class Creator extends EventEmitter {
|
||||
})
|
||||
}
|
||||
|
||||
if (hasPnpm()) {
|
||||
if (hasPnpm3OrLater()) {
|
||||
packageManagerChoices.push({
|
||||
name: 'Use PNPM',
|
||||
value: 'pnpm',
|
||||
|
||||
@@ -6,7 +6,7 @@ const {
|
||||
log,
|
||||
error,
|
||||
hasProjectYarn,
|
||||
hasPnpm,
|
||||
hasProjectPnpm,
|
||||
resolvePluginId,
|
||||
resolveModule,
|
||||
loadModule
|
||||
@@ -27,7 +27,7 @@ async function add (pluginName, options = {}, context = process.cwd()) {
|
||||
log(`📦 Installing ${chalk.cyan(packageName)}...`)
|
||||
log()
|
||||
|
||||
const packageManager = loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : hasPnpm() ? 'pnpm' : 'npm')
|
||||
const packageManager = loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : hasProjectPnpm() ? 'pnpm' : 'npm')
|
||||
await installPackage(context, packageManager, options.registry, packageName)
|
||||
|
||||
log(`${chalk.green('✔')} Successfully installed plugin: ${chalk.cyan(packageName)}`)
|
||||
|
||||
@@ -14,7 +14,7 @@ const {
|
||||
error,
|
||||
hasProjectYarn,
|
||||
hasProjectGit,
|
||||
hasPnpm,
|
||||
hasProjectPnpm,
|
||||
logWithSpinner,
|
||||
stopSpinner,
|
||||
resolvePluginId,
|
||||
@@ -145,7 +145,7 @@ async function runGenerator (context, plugin, pkg = getPkg(context)) {
|
||||
log(`📦 Installing additional dependencies...`)
|
||||
log()
|
||||
const packageManager =
|
||||
loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : hasPnpm() ? 'pnpm' : 'npm')
|
||||
loadOptions().packageManager || (hasProjectYarn(context) ? 'yarn' : hasProjectPnpm() ? 'pnpm' : 'npm')
|
||||
await installDeps(context, packageManager, plugin.options && plugin.options.registry)
|
||||
}
|
||||
|
||||
|
||||
@@ -11,11 +11,11 @@ module.exports = function loadCommand (commandName, moduleName) {
|
||||
} catch (err2) {
|
||||
if (isNotFoundError(err2)) {
|
||||
const chalk = require('chalk')
|
||||
const { hasYarn, hasPnpm } = require('@vue/cli-shared-utils')
|
||||
const { hasYarn, hasPnpm3OrLater } = require('@vue/cli-shared-utils')
|
||||
let installCommand = `npm install -g`
|
||||
if (hasYarn()) {
|
||||
installCommand = `yarn global add`
|
||||
} else if (hasPnpm()) {
|
||||
} else if (hasPnpm3OrLater()) {
|
||||
installCommand = `pnpm install -g`
|
||||
}
|
||||
console.log()
|
||||
|
||||
Reference in New Issue
Block a user