clearConsole.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const getVersions = require('./getVersions')
  2. const {
  3. chalk,
  4. execa,
  5. semver,
  6. clearConsole,
  7. hasYarn,
  8. hasPnpm3OrLater
  9. } = require('@vue/cli-shared-utils')
  10. async function getInstallationCommand () {
  11. if (hasYarn()) {
  12. const { stdout: yarnGlobalDir } = await execa('yarn', ['global', 'dir'])
  13. if (__dirname.includes(yarnGlobalDir)) {
  14. return 'yarn global add'
  15. }
  16. }
  17. if (hasPnpm3OrLater()) {
  18. const { stdout: pnpmGlobalPrefix } = await execa('pnpm', ['config', 'get', 'prefix'])
  19. if (__dirname.includes(pnpmGlobalPrefix) && __dirname.includes('pnpm-global')) {
  20. return `pnpm i -g`
  21. }
  22. }
  23. const { stdout: npmGlobalPrefix } = await execa('npm', ['config', 'get', 'prefix'])
  24. if (__dirname.includes(npmGlobalPrefix)) {
  25. return `npm i -g`
  26. }
  27. }
  28. exports.generateTitle = async function (checkUpdate) {
  29. const { current, latest, error } = await getVersions()
  30. let title = chalk.bold.blue(`Vue CLI v${current}`)
  31. if (process.env.VUE_CLI_TEST) {
  32. title += ' ' + chalk.blue.bold('TEST')
  33. }
  34. if (process.env.VUE_CLI_DEBUG) {
  35. title += ' ' + chalk.magenta.bold('DEBUG')
  36. }
  37. if (error) {
  38. title += '\n' + chalk.red('Failed to check for updates')
  39. }
  40. if (checkUpdate && !error && semver.gt(latest, current)) {
  41. if (process.env.VUE_CLI_API_MODE) {
  42. title += chalk.green(` 🌟️ New version available: ${latest}`)
  43. } else {
  44. let upgradeMessage = `New version available ${chalk.magenta(current)} → ${chalk.green(latest)}`
  45. try {
  46. const command = await getInstallationCommand()
  47. let name = require('../../package.json').name
  48. if (semver.prerelease(latest)) {
  49. name += '@next'
  50. }
  51. if (command) {
  52. upgradeMessage +=
  53. `\nRun ${chalk.yellow(`${command} ${name}`)} to update!`
  54. }
  55. } catch (e) {}
  56. const upgradeBox = require('boxen')(upgradeMessage, {
  57. align: 'center',
  58. borderColor: 'green',
  59. dimBorder: true,
  60. padding: 1
  61. })
  62. title += `\n${upgradeBox}\n`
  63. }
  64. }
  65. return title
  66. }
  67. exports.clearConsole = async function clearConsoleWithTitle (checkUpdate) {
  68. const title = await exports.generateTitle(checkUpdate)
  69. clearConsole(title)
  70. }