loadCommand.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. module.exports = function loadCommand (commandName, moduleName) {
  2. const isNotFoundError = err => {
  3. return err.message.match(/Cannot find module/)
  4. }
  5. try {
  6. return require(moduleName)
  7. } catch (err) {
  8. if (isNotFoundError(err)) {
  9. try {
  10. return require('import-global')(moduleName)
  11. } catch (err2) {
  12. 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. }
  20. console.log()
  21. console.log(
  22. ` Command ${chalk.cyan(`vue ${commandName}`)} requires a global addon to be installed.\n` +
  23. ` Please run ${chalk.cyan(`${installCommand} ${moduleName}`)} and try again.`
  24. )
  25. console.log()
  26. process.exit(1)
  27. } else {
  28. throw err2
  29. }
  30. }
  31. } else {
  32. throw err
  33. }
  34. }
  35. }