1 | const invoke = require('./invoke')
|
2 | const inquirer = require('inquirer')
|
3 | const {
|
4 | chalk,
|
5 | semver,
|
6 | resolveModule,
|
7 | loadModule
|
8 | } = require('@vue/cli-shared-utils')
|
9 |
|
10 | const getVersions = require('./util/getVersions')
|
11 | const PackageManager = require('./util/ProjectPackageManager')
|
12 | const {
|
13 | log,
|
14 | error,
|
15 | resolvePluginId,
|
16 | isOfficialPlugin
|
17 | } = require('@vue/cli-shared-utils')
|
18 | const confirmIfGitDirty = require('./util/confirmIfGitDirty')
|
19 |
|
20 | async function add (pluginToAdd, options = {}, context = process.cwd()) {
|
21 | if (!(await confirmIfGitDirty(context))) {
|
22 | return
|
23 | }
|
24 |
|
25 |
|
26 | const servicePkg = loadModule('@vue/cli-service/package.json', context)
|
27 | if (servicePkg && semver.satisfies(servicePkg.version, '3.x')) {
|
28 |
|
29 | if (/^(@vue\/)?router$/.test(pluginToAdd)) {
|
30 | return addRouter(context)
|
31 | }
|
32 | if (/^(@vue\/)?vuex$/.test(pluginToAdd)) {
|
33 | return addVuex(context)
|
34 | }
|
35 | }
|
36 |
|
37 | const pluginRe = /^(@?[^@]+)(?:@(.+))?$/
|
38 | const [
|
39 |
|
40 | _skip,
|
41 | pluginName,
|
42 | pluginVersion
|
43 | ] = pluginToAdd.match(pluginRe)
|
44 | const packageName = resolvePluginId(pluginName)
|
45 |
|
46 | log()
|
47 | log(`📦 Installing ${chalk.cyan(packageName)}...`)
|
48 | log()
|
49 |
|
50 | const pm = new PackageManager({ context })
|
51 |
|
52 | if (pluginVersion) {
|
53 | await pm.add(`${packageName}@${pluginVersion}`)
|
54 | } else if (isOfficialPlugin(packageName)) {
|
55 | const { latestMinor } = await getVersions()
|
56 | await pm.add(`${packageName}@~${latestMinor}`)
|
57 | } else {
|
58 | await pm.add(packageName, { tilde: true })
|
59 | }
|
60 |
|
61 | log(`${chalk.green('✔')} Successfully installed plugin: ${chalk.cyan(packageName)}`)
|
62 | log()
|
63 |
|
64 | const generatorPath = resolveModule(`${packageName}/generator`, context)
|
65 | if (generatorPath) {
|
66 | invoke(pluginName, options, context)
|
67 | } else {
|
68 | log(`Plugin ${packageName} does not have a generator to invoke`)
|
69 | }
|
70 | }
|
71 |
|
72 | module.exports = (...args) => {
|
73 | return add(...args).catch(err => {
|
74 | error(err)
|
75 | if (!process.env.VUE_CLI_TEST) {
|
76 | process.exit(1)
|
77 | }
|
78 | })
|
79 | }
|
80 |
|
81 | async function addRouter (context) {
|
82 | const options = await inquirer.prompt([{
|
83 | name: 'routerHistoryMode',
|
84 | type: 'confirm',
|
85 | message: `Use history mode for router? ${chalk.yellow(`(Requires proper server setup for index fallback in production)`)}`
|
86 | }])
|
87 | invoke.runGenerator(context, {
|
88 | id: 'core:router',
|
89 | apply: loadModule('@vue/cli-service/generator/router', context),
|
90 | options
|
91 | })
|
92 | }
|
93 |
|
94 | async function addVuex (context) {
|
95 | invoke.runGenerator(context, {
|
96 | id: 'core:vuex',
|
97 | apply: loadModule('@vue/cli-service/generator/vuex', context)
|
98 | })
|
99 | }
|