Skip to content

Commit 8578567

Browse files
authored
fix: more accurate warning message for missing global peer dependencies (#5871)
1 parent 286d068 commit 8578567

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

Diff for: packages/@vue/cli/lib/util/clearConsole.js

+3-26
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,12 @@
11
const getVersions = require('./getVersions')
22
const {
33
chalk,
4-
execa,
54
semver,
65

7-
clearConsole,
8-
9-
hasYarn,
10-
hasPnpm3OrLater
6+
clearConsole
117
} = require('@vue/cli-shared-utils')
128

13-
async function getInstallationCommand () {
14-
if (hasYarn()) {
15-
const { stdout: yarnGlobalDir } = await execa('yarn', ['global', 'dir'])
16-
if (__dirname.includes(yarnGlobalDir)) {
17-
return 'yarn global add'
18-
}
19-
}
20-
21-
if (hasPnpm3OrLater()) {
22-
const { stdout: pnpmGlobalPrefix } = await execa('pnpm', ['config', 'get', 'prefix'])
23-
if (__dirname.includes(pnpmGlobalPrefix) && __dirname.includes('pnpm-global')) {
24-
return `pnpm i -g`
25-
}
26-
}
27-
28-
const { stdout: npmGlobalPrefix } = await execa('npm', ['config', 'get', 'prefix'])
29-
if (__dirname.includes(npmGlobalPrefix)) {
30-
return `npm i -g`
31-
}
32-
}
9+
const getGlobalInstallCommand = require('./getGlobalInstallCommand')
3310

3411
exports.generateTitle = async function (checkUpdate) {
3512
const { current, latest, error } = await getVersions()
@@ -53,7 +30,7 @@ exports.generateTitle = async function (checkUpdate) {
5330
let upgradeMessage = `New version available ${chalk.magenta(current)}${chalk.green(latest)}`
5431

5532
try {
56-
const command = await getInstallationCommand()
33+
const command = getGlobalInstallCommand()
5734
let name = require('../../package.json').name
5835
if (semver.prerelease(latest)) {
5936
name += '@next'
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { execa, hasYarn, hasPnpm3OrLater } = require('@vue/cli-shared-utils')
2+
3+
module.exports = function getGlobalInstallCommand () {
4+
if (hasYarn()) {
5+
const { stdout: yarnGlobalDir } = execa.sync('yarn', ['global', 'dir'])
6+
if (__dirname.includes(yarnGlobalDir)) {
7+
return 'yarn global add'
8+
}
9+
}
10+
11+
if (hasPnpm3OrLater()) {
12+
const { stdout: pnpmGlobalPrefix } = execa.sync('pnpm', ['config', 'get', 'prefix'])
13+
if (__dirname.includes(pnpmGlobalPrefix) && __dirname.includes('pnpm-global')) {
14+
return `pnpm i -g`
15+
}
16+
}
17+
18+
const { stdout: npmGlobalPrefix } = execa.sync('npm', ['config', 'get', 'prefix'])
19+
if (__dirname.includes(npmGlobalPrefix)) {
20+
return `npm i -g`
21+
}
22+
}

Diff for: packages/@vue/cli/lib/util/loadCommand.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const { chalk } = require('@vue/cli-shared-utils')
2+
const getGlobalInstallCommand = require('./getGlobalInstallCommand')
3+
14
module.exports = function loadCommand (commandName, moduleName) {
25
const isNotFoundError = err => {
36
return err.message.match(/Cannot find module/)
@@ -10,13 +13,7 @@ module.exports = function loadCommand (commandName, moduleName) {
1013
return require('import-global')(moduleName)
1114
} catch (err2) {
1215
if (isNotFoundError(err2)) {
13-
const { chalk, hasYarn, hasPnpm3OrLater } = require('@vue/cli-shared-utils')
14-
let installCommand = `npm install -g`
15-
if (hasYarn()) {
16-
installCommand = `yarn global add`
17-
} else if (hasPnpm3OrLater()) {
18-
installCommand = `pnpm install -g`
19-
}
16+
const installCommand = getGlobalInstallCommand()
2017
console.log()
2118
console.log(
2219
` Command ${chalk.cyan(`vue ${commandName}`)} requires a global addon to be installed.\n` +

0 commit comments

Comments
 (0)