Skip to content

Commit badf63d

Browse files
B4rtwarehaoqunjiang
authored andcommitted
fix: add pnpm v4 support (#4677)
* fix: add pnpm v4 support in pnpm v4 the option '--loglevel' is no longer available instead '--reporter' is used. * refactor: remove 'v' from PNPM constants for linting * refactor: rename variable * fix: typo in _hasPnpm4orLater Co-Authored-By: Pavan Kumar Sunkara <pavan.sss1991@gmail.com> * refactor: reduce the amount of duplicate code for pnpm version check * refactor: remove return-assignment * refactor: add explicit return value instead of using array access Co-Authored-By: Pavan Kumar Sunkara <pavan.sss1991@gmail.com> * fix: remove return value from checkPnpmVersion * fix: pnpmVersion variable * refactor: cache pnpm version number * refactor: fix function name and revert api break * fix: function call correction * refactor: export hasPnpmVersionOrLater and use this in favor of hasPnpm4OrLater * refactor: move cache getter into getPnpmVersion * refactor: add comment * refactor: remove comment
1 parent b65b24e commit badf63d

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,37 @@ exports.hasProjectGit = (cwd) => {
7979
}
8080

8181
let _hasPnpm
82-
let _hasPnpm3orLater
82+
let _pnpmVersion
8383
const _pnpmProjects = new LRU({
8484
max: 10,
8585
maxAge: 1000
8686
})
8787

88-
exports.hasPnpm3OrLater = () => {
89-
if (process.env.VUE_CLI_TEST) {
90-
return true
91-
}
92-
if (_hasPnpm3orLater != null) {
93-
return _hasPnpm3orLater
88+
function getPnpmVersion () {
89+
if (_pnpmVersion != null) {
90+
return _pnpmVersion
9491
}
9592
try {
96-
const pnpmVersion = execSync('pnpm --version', {
93+
_pnpmVersion = execSync('pnpm --version', {
9794
stdio: ['pipe', 'pipe', 'ignore']
9895
}).toString()
9996
// there's a critical bug in pnpm 2
10097
// https://github.com/pnpm/pnpm/issues/1678#issuecomment-469981972
10198
// so we only support pnpm >= 3.0.0
10299
_hasPnpm = true
103-
_hasPnpm3orLater = semver.gte(pnpmVersion, '3.0.0')
104-
return _hasPnpm3orLater
105-
} catch (e) {
106-
return (_hasPnpm3orLater = false)
100+
} catch (e) {}
101+
return _pnpmVersion || '0.0.0'
102+
}
103+
104+
exports.hasPnpmVersionOrLater = (version) => {
105+
if (process.env.VUE_CLI_TEST) {
106+
return true
107107
}
108+
return semver.gte(getPnpmVersion(), version)
109+
}
110+
111+
exports.hasPnpm3OrLater = () => {
112+
return this.hasPnpmVersionOrLater('3.0.0')
108113
}
109114

110115
exports.hasProjectPnpm = (cwd) => {

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
hasYarn,
1212
hasProjectYarn,
1313
hasPnpm3OrLater,
14+
hasPnpmVersionOrLater,
1415
hasProjectPnpm
1516
} = require('@vue/cli-shared-utils/lib/env')
1617
const { isOfficialPlugin, resolvePluginId } = require('@vue/cli-shared-utils/lib/pluginResolution')
@@ -32,19 +33,26 @@ const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG
3233

3334
const TAOBAO_DIST_URL = 'https://npm.taobao.org/dist'
3435
const SUPPORTED_PACKAGE_MANAGERS = ['yarn', 'pnpm', 'npm']
36+
const PACKAGE_MANAGER_PNPM4_CONFIG = {
37+
install: ['install', '--reporter', 'silent', '--shamefully-hoist'],
38+
add: ['install', '--reporter', 'silent', '--shamefully-hoist'],
39+
upgrade: ['update', '--reporter', 'silent'],
40+
remove: ['uninstall', '--reporter', 'silent']
41+
}
42+
const PACKAGE_MANAGER_PNPM3_CONFIG = {
43+
install: ['install', '--loglevel', 'error', '--shamefully-flatten'],
44+
add: ['install', '--loglevel', 'error', '--shamefully-flatten'],
45+
upgrade: ['update', '--loglevel', 'error'],
46+
remove: ['uninstall', '--loglevel', 'error']
47+
}
3548
const PACKAGE_MANAGER_CONFIG = {
3649
npm: {
3750
install: ['install', '--loglevel', 'error'],
3851
add: ['install', '--loglevel', 'error'],
3952
upgrade: ['update', '--loglevel', 'error'],
4053
remove: ['uninstall', '--loglevel', 'error']
4154
},
42-
pnpm: {
43-
install: ['install', '--loglevel', 'error', '--shamefully-flatten'],
44-
add: ['install', '--loglevel', 'error', '--shamefully-flatten'],
45-
upgrade: ['update', '--loglevel', 'error'],
46-
remove: ['uninstall', '--loglevel', 'error']
47-
},
55+
pnpm: hasPnpmVersionOrLater('4.0.0') ? PACKAGE_MANAGER_PNPM4_CONFIG : PACKAGE_MANAGER_PNPM3_CONFIG,
4856
yarn: {
4957
install: [],
5058
add: ['add'],

0 commit comments

Comments
 (0)