Skip to content

Commit 132b0db

Browse files
committed
feat: wip invoke command
1 parent aa43bec commit 132b0db

File tree

8 files changed

+113
-3
lines changed

8 files changed

+113
-3
lines changed

packages/@vue/cli-plugin-eslint/generator.js

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
module.exports = (api, { config, lintOn = [] }) => {
2+
if (typeof lintOn === 'string') {
3+
lintOn = lintOn.split(',')
4+
}
5+
26
const pkg = {
37
scripts: {
48
lint: 'vue-cli-service lint'

packages/@vue/cli-plugin-typescript/generator/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ module.exports = (api, {
44
lintOn = [],
55
experimentalCompileTsWithBabel
66
}) => {
7+
if (typeof lintOn === 'string') {
8+
lintOn = lintOn.split(',')
9+
}
10+
711
if (classComponent) {
812
api.extendPackage({
913
devDependencies: {

packages/@vue/cli-plugin-unit-jest/generator/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = (api, options) => {
1+
module.exports = api => {
22
api.render('./template')
33
api.extendPackage({
44
scripts: {

packages/@vue/cli-plugin-unit-mocha/generator/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = (api, options) => {
1+
module.exports = api => {
22
api.render('./template')
33

44
const devDependencies = {

packages/@vue/cli/bin/vue

+15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const path = require('path')
55
const slash = require('slash')
66
const chalk = require('chalk')
77
const semver = require('semver')
8+
const minimist = require('minimist')
89
const { error } = require('@vue/cli-shared-utils')
910
const requiredVersion = require('../package.json').engines.node
1011

@@ -42,6 +43,14 @@ program
4243
require('../lib/create')(name, cleanArgs(cmd))
4344
})
4445

46+
program
47+
.command('invoke <plugin>')
48+
.allowUnknownOption()
49+
.description('invoke the generator of an installed plugin in an already created project')
50+
.action((plugin) => {
51+
require('../lib/invoke')(plugin, minimist(process.argv.slice(3)))
52+
})
53+
4554
program
4655
.command('serve [entry]')
4756
.description('serve a .js or vue file in development mode with zero config')
@@ -52,6 +61,9 @@ program
5261

5362
program
5463
.command('build [entry]')
64+
.option('-t, --target', 'Build target (app, lib, web-component). Default: app')
65+
.option('-f, --format', 'How the lib is exposed (iife, amd, cjs, umd). Default: umd')
66+
.option('-n, --name', 'Library name for umd export')
5567
.description('build a .js or .vue file in production mode with zero config')
5668
.action((entry, cmd) => {
5769
loadCommand('build', '@vue/cli-service-global').build(entry, cleanArgs(cmd))
@@ -76,6 +88,9 @@ program.commands.forEach(c => c.on('--help', () => console.log()))
7688
// enhance common error messages
7789
const enhanceErrorMessages = (methodName, log) => {
7890
program.Command.prototype[methodName] = function (...args) {
91+
if (methodName === 'unknownOption' && this._allowUnknownOption) {
92+
return
93+
}
7994
this.outputHelp()
8095
console.log(` ` + chalk.red(log(...args)))
8196
console.log()

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

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const chalk = require('chalk')
4+
const resolve = require('resolve')
5+
const Generator = require('./Generator')
6+
const {
7+
log,
8+
error,
9+
logWithSpinner,
10+
stopSpinner
11+
} = require('@vue/cli-shared-utils')
12+
13+
async function invoke (pluginName, options) {
14+
delete options._
15+
const context = process.cwd()
16+
const pkgPath = path.resolve(context, 'package.json')
17+
18+
if (!fs.existsSync(pkgPath)) {
19+
error(`package.json not found in ${chalk.yellow(context)}`)
20+
process.exit(1)
21+
}
22+
23+
const pkg = require(pkgPath)
24+
25+
// attempt to locate the plugin in package.json
26+
const findPlugin = deps => {
27+
if (!deps) return
28+
let name
29+
if (deps[name = pluginName] ||
30+
deps[name = `@vue/cli-plugin-${pluginName}`] ||
31+
deps[name = `vue-cli-plugin-${pluginName}`]) {
32+
return name
33+
}
34+
}
35+
const resolvedPluginName = (
36+
findPlugin(pkg.devDependencies) ||
37+
findPlugin(pkg.dependencies)
38+
)
39+
40+
if (!resolvedPluginName) {
41+
error(`Cannot resolve plugin ${chalk.yellow(pluginName)} from package.json.`)
42+
process.exit(1)
43+
}
44+
45+
const generatorURI = `${resolvedPluginName}/generator`
46+
const generatorPath = resolve.sync(generatorURI, { basedir: context })
47+
const plugin = {
48+
id: resolvedPluginName,
49+
apply: require(generatorPath),
50+
options
51+
}
52+
53+
const createCompleteCbs = []
54+
const generator = new Generator(
55+
context,
56+
pkg,
57+
[plugin],
58+
createCompleteCbs
59+
)
60+
61+
await generator.generate()
62+
63+
// TODO check if package.json was changed,
64+
// if yes installDeps
65+
logWithSpinner('📦', `Installing additional dependencies...`)
66+
67+
if (createCompleteCbs.lenght) {
68+
logWithSpinner('⚓', `Running completion hooks...`)
69+
for (const cb of createCompleteCbs) {
70+
await cb()
71+
}
72+
}
73+
74+
stopSpinner()
75+
log()
76+
log(` Successfully invoked generator for plugin: ${chalk.cyan(resolvedPluginName)}`)
77+
log(` You should review and commit the changes.`)
78+
log()
79+
}
80+
81+
module.exports = (pluginName, options) => {
82+
invoke(pluginName, options).catch(err => {
83+
error(err)
84+
process.exit(1)
85+
})
86+
}

packages/@vue/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"inquirer": "^4.0.1",
3838
"isbinaryfile": "^3.0.2",
3939
"klaw-sync": "^3.0.2",
40+
"minimist": "^1.2.0",
4041
"mkdirp": "^0.5.1",
4142
"resolve": "^1.5.0",
4243
"rimraf": "^2.6.2",

scripts/testChanged.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const additionalArgs = process.argv.slice(2)
1212

1313
if (!files.length) {
1414
console.log(
15-
`No changed src files found.\n` +
15+
`No modified & unstaged src files found.\n` +
1616
`To run the full test suite, run ${chalk.cyan(`yarn test-all`)} instead.`
1717
)
1818
return

0 commit comments

Comments
 (0)