Skip to content

Commit 563636c

Browse files
committed
test: test for vue invoke
1 parent 06af371 commit 563636c

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const create = require('@vue/cli-test-utils/createTestProject')
2+
3+
test('invoke single generator', async () => {
4+
const project = await create('invoke', {
5+
plugins: {
6+
'@vue/cli-plugin-babel': {}
7+
}
8+
})
9+
// mock install
10+
const pkg = JSON.parse(await project.read('package.json'))
11+
pkg.devDependencies['@vue/cli-plugin-eslint'] = '*'
12+
await project.write('package.json', JSON.stringify(pkg, null, 2))
13+
14+
const cliBinPath = require.resolve('../bin/vue')
15+
await project.run(`${cliBinPath} invoke eslint --config airbnb --lintOn save,commit`)
16+
17+
const updatedPkg = JSON.parse(await project.read('package.json'))
18+
expect(updatedPkg.scripts.lint).toBe('vue-cli-service lint')
19+
expect(updatedPkg.devDependencies).toHaveProperty('eslint-plugin-vue')
20+
expect(updatedPkg.devDependencies).toHaveProperty('lint-staged')
21+
expect(updatedPkg.eslintConfig).toEqual({
22+
extends: ['plugin:vue/essential', '@vue/airbnb']
23+
})
24+
expect(updatedPkg.gitHooks).toEqual({
25+
'pre-commit': 'lint-staged'
26+
})
27+
28+
const lintedMain = await project.read('src/main.js')
29+
expect(lintedMain).toMatch(';') // should've been linted in post-generate hook
30+
})

packages/@vue/cli/bin/vue

+6-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ if (!semver.satisfies(process.version, requiredVersion)) {
1818
}
1919

2020
// enter debug mode when creating test repo
21-
if (slash(process.cwd()).indexOf('/packages/test') > 0 &&
22-
fs.existsSync(path.resolve(process.cwd(), '../@vue'))) {
21+
if (
22+
slash(process.cwd()).indexOf('/packages/test') > 0 && (
23+
fs.existsSync(path.resolve(process.cwd(), '../@vue')) ||
24+
fs.existsSync(path.resolve(process.cwd(), '../../@vue'))
25+
)
26+
) {
2327
process.env.VUE_CLI_DEBUG = true
2428
}
2529

packages/@vue/cli/lib/invoke.js

+22-6
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ const path = require('path')
33
const chalk = require('chalk')
44
const resolve = require('resolve')
55
const Generator = require('./Generator')
6+
const { loadOptions } = require('./options')
7+
const installDeps = require('./util/installDeps')
8+
const clearConsole = require('./util/clearConsole')
69
const {
710
log,
811
error,
12+
hasYarn,
913
logWithSpinner,
1014
stopSpinner
1115
} = require('@vue/cli-shared-utils')
@@ -58,13 +62,25 @@ async function invoke (pluginName, options) {
5862
createCompleteCbs
5963
)
6064

65+
clearConsole()
66+
logWithSpinner('🚀', `Invoking generator for ${resolvedPluginName}...`)
6167
await generator.generate()
6268

63-
// TODO check if package.json was changed,
64-
// if yes installDeps
65-
logWithSpinner('📦', `Installing additional dependencies...`)
69+
const isTestOrDebug = process.env.VUE_CLI_TEST || process.env.VUE_CLI_DEBUG
70+
const newDeps = generator.pkg.dependencies
71+
const newDevDeps = generator.pkg.devDependencies
72+
const depsChanged = (
73+
JSON.stringify(newDeps) !== JSON.stringify(pkg.dependencies) ||
74+
JSON.stringify(newDevDeps) !== JSON.stringify(pkg.devDependencies)
75+
)
76+
77+
if (!isTestOrDebug && depsChanged) {
78+
logWithSpinner('📦', `Installing additional dependencies...`)
79+
const packageManager = loadOptions().packageManager || (hasYarn ? 'yarn' : 'npm')
80+
await installDeps(context, packageManager)
81+
}
6682

67-
if (createCompleteCbs.lenght) {
83+
if (createCompleteCbs.length) {
6884
logWithSpinner('⚓', `Running completion hooks...`)
6985
for (const cb of createCompleteCbs) {
7086
await cb()
@@ -73,8 +89,8 @@ async function invoke (pluginName, options) {
7389

7490
stopSpinner()
7591
log()
76-
log(` Successfully invoked generator for plugin: ${chalk.cyan(resolvedPluginName)}`)
77-
log(` You should review and commit the changes.`)
92+
log(` Successfully invoked generator for plugin: ${chalk.cyan(resolvedPluginName)}`)
93+
log(` You should review and commit the changes.`)
7894
log()
7995
}
8096

0 commit comments

Comments
 (0)