UNPKG

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