Skip to content

Commit

Permalink
fix: more accurate warning message for missing global peer dependenci…
Browse files Browse the repository at this point in the history
…es (#5871)
  • Loading branch information
sodatea committed Sep 28, 2020
1 parent 286d068 commit 8578567
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
29 changes: 3 additions & 26 deletions packages/@vue/cli/lib/util/clearConsole.js
@@ -1,35 +1,12 @@
const getVersions = require('./getVersions')
const {
chalk,
execa,
semver,

clearConsole,

hasYarn,
hasPnpm3OrLater
clearConsole
} = require('@vue/cli-shared-utils')

async function getInstallationCommand () {
if (hasYarn()) {
const { stdout: yarnGlobalDir } = await execa('yarn', ['global', 'dir'])
if (__dirname.includes(yarnGlobalDir)) {
return 'yarn global add'
}
}

if (hasPnpm3OrLater()) {
const { stdout: pnpmGlobalPrefix } = await execa('pnpm', ['config', 'get', 'prefix'])
if (__dirname.includes(pnpmGlobalPrefix) && __dirname.includes('pnpm-global')) {
return `pnpm i -g`
}
}

const { stdout: npmGlobalPrefix } = await execa('npm', ['config', 'get', 'prefix'])
if (__dirname.includes(npmGlobalPrefix)) {
return `npm i -g`
}
}
const getGlobalInstallCommand = require('./getGlobalInstallCommand')

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

try {
const command = await getInstallationCommand()
const command = getGlobalInstallCommand()
let name = require('../../package.json').name
if (semver.prerelease(latest)) {
name += '@next'
Expand Down
22 changes: 22 additions & 0 deletions packages/@vue/cli/lib/util/getGlobalInstallCommand.js
@@ -0,0 +1,22 @@
const { execa, hasYarn, hasPnpm3OrLater } = require('@vue/cli-shared-utils')

module.exports = function getGlobalInstallCommand () {
if (hasYarn()) {
const { stdout: yarnGlobalDir } = execa.sync('yarn', ['global', 'dir'])
if (__dirname.includes(yarnGlobalDir)) {
return 'yarn global add'
}
}

if (hasPnpm3OrLater()) {
const { stdout: pnpmGlobalPrefix } = execa.sync('pnpm', ['config', 'get', 'prefix'])
if (__dirname.includes(pnpmGlobalPrefix) && __dirname.includes('pnpm-global')) {
return `pnpm i -g`
}
}

const { stdout: npmGlobalPrefix } = execa.sync('npm', ['config', 'get', 'prefix'])
if (__dirname.includes(npmGlobalPrefix)) {
return `npm i -g`
}
}
11 changes: 4 additions & 7 deletions packages/@vue/cli/lib/util/loadCommand.js
@@ -1,3 +1,6 @@
const { chalk } = require('@vue/cli-shared-utils')
const getGlobalInstallCommand = require('./getGlobalInstallCommand')

module.exports = function loadCommand (commandName, moduleName) {
const isNotFoundError = err => {
return err.message.match(/Cannot find module/)
Expand All @@ -10,13 +13,7 @@ module.exports = function loadCommand (commandName, moduleName) {
return require('import-global')(moduleName)
} catch (err2) {
if (isNotFoundError(err2)) {
const { chalk, hasYarn, hasPnpm3OrLater } = require('@vue/cli-shared-utils')
let installCommand = `npm install -g`
if (hasYarn()) {
installCommand = `yarn global add`
} else if (hasPnpm3OrLater()) {
installCommand = `pnpm install -g`
}
const installCommand = getGlobalInstallCommand()
console.log()
console.log(
` Command ${chalk.cyan(`vue ${commandName}`)} requires a global addon to be installed.\n` +
Expand Down

0 comments on commit 8578567

Please sign in to comment.