migrate.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const {
  2. chalk,
  3. log,
  4. error,
  5. logWithSpinner,
  6. stopSpinner,
  7. loadModule,
  8. resolvePluginId
  9. } = require('@vue/cli-shared-utils')
  10. const Migrator = require('./Migrator')
  11. const PackageManager = require('./util/ProjectPackageManager')
  12. const readFiles = require('./util/readFiles')
  13. const getPkg = require('./util/getPkg')
  14. const getChangedFiles = require('./util/getChangedFiles')
  15. const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG
  16. async function runMigrator (context, plugin, pkg = getPkg(context)) {
  17. const afterInvokeCbs = []
  18. const migrator = new Migrator(context, {
  19. plugin,
  20. pkg,
  21. files: await readFiles(context),
  22. afterInvokeCbs
  23. })
  24. log(`🚀 Running migrator of ${plugin.id}`)
  25. await migrator.generate({
  26. extractConfigFiles: true,
  27. checkExisting: true
  28. })
  29. const newDeps = migrator.pkg.dependencies
  30. const newDevDeps = migrator.pkg.devDependencies
  31. const depsChanged =
  32. JSON.stringify(newDeps) !== JSON.stringify(pkg.dependencies) ||
  33. JSON.stringify(newDevDeps) !== JSON.stringify(pkg.devDependencies)
  34. if (!isTestOrDebug && depsChanged) {
  35. log(`📦 Installing additional dependencies...`)
  36. log()
  37. const pm = new PackageManager({ context })
  38. await pm.install()
  39. }
  40. if (afterInvokeCbs.length) {
  41. logWithSpinner('⚓', `Running completion hooks...`)
  42. for (const cb of afterInvokeCbs) {
  43. await cb()
  44. }
  45. stopSpinner()
  46. log()
  47. }
  48. log(
  49. `${chalk.green(
  50. '✔'
  51. )} Successfully invoked migrator for plugin: ${chalk.cyan(plugin.id)}`
  52. )
  53. const changedFiles = getChangedFiles(context)
  54. if (changedFiles.length) {
  55. log(` The following files have been updated / added:\n`)
  56. log(chalk.red(changedFiles.map(line => ` ${line}`).join('\n')))
  57. log()
  58. log(
  59. ` You should review these changes with ${chalk.cyan(
  60. 'git diff'
  61. )} and commit them.`
  62. )
  63. log()
  64. }
  65. migrator.printExitLogs()
  66. }
  67. async function migrate (pluginId, { from }, context = process.cwd()) {
  68. // TODO: remove this after upgrading to commander 4.x
  69. if (!from) {
  70. throw new Error(`Required option 'from' not specified`)
  71. }
  72. const pluginName = resolvePluginId(pluginId)
  73. const pluginMigrator = loadModule(`${pluginName}/migrator`, context)
  74. if (!pluginMigrator) {
  75. log(`There's no migrator in ${pluginName}`)
  76. return
  77. }
  78. await runMigrator(context, {
  79. id: pluginName,
  80. apply: pluginMigrator,
  81. baseVersion: from
  82. })
  83. }
  84. module.exports = (...args) => {
  85. return migrate(...args).catch(err => {
  86. error(err)
  87. if (!process.env.VUE_CLI_TEST) {
  88. process.exit(1)
  89. }
  90. })
  91. }
  92. module.exports.runMigrator = runMigrator