Skip to content

Commit

Permalink
feat: wip invoke command
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 15, 2018
1 parent aa43bec commit 132b0db
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/@vue/cli-plugin-eslint/generator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module.exports = (api, { config, lintOn = [] }) => {
if (typeof lintOn === 'string') {
lintOn = lintOn.split(',')
}

const pkg = {
scripts: {
lint: 'vue-cli-service lint'
Expand Down
4 changes: 4 additions & 0 deletions packages/@vue/cli-plugin-typescript/generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ module.exports = (api, {
lintOn = [],
experimentalCompileTsWithBabel
}) => {
if (typeof lintOn === 'string') {
lintOn = lintOn.split(',')
}

if (classComponent) {
api.extendPackage({
devDependencies: {
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-plugin-unit-jest/generator/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = (api, options) => {
module.exports = api => {
api.render('./template')
api.extendPackage({
scripts: {
Expand Down
2 changes: 1 addition & 1 deletion packages/@vue/cli-plugin-unit-mocha/generator/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = (api, options) => {
module.exports = api => {
api.render('./template')

const devDependencies = {
Expand Down
15 changes: 15 additions & 0 deletions packages/@vue/cli/bin/vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const path = require('path')
const slash = require('slash')
const chalk = require('chalk')
const semver = require('semver')
const minimist = require('minimist')
const { error } = require('@vue/cli-shared-utils')
const requiredVersion = require('../package.json').engines.node

Expand Down Expand Up @@ -42,6 +43,14 @@ program
require('../lib/create')(name, cleanArgs(cmd))
})

program
.command('invoke <plugin>')
.allowUnknownOption()
.description('invoke the generator of an installed plugin in an already created project')
.action((plugin) => {
require('../lib/invoke')(plugin, minimist(process.argv.slice(3)))
})

program
.command('serve [entry]')
.description('serve a .js or vue file in development mode with zero config')
Expand All @@ -52,6 +61,9 @@ program

program
.command('build [entry]')
.option('-t, --target', 'Build target (app, lib, web-component). Default: app')
.option('-f, --format', 'How the lib is exposed (iife, amd, cjs, umd). Default: umd')
.option('-n, --name', 'Library name for umd export')
.description('build a .js or .vue file in production mode with zero config')
.action((entry, cmd) => {
loadCommand('build', '@vue/cli-service-global').build(entry, cleanArgs(cmd))
Expand All @@ -76,6 +88,9 @@ program.commands.forEach(c => c.on('--help', () => console.log()))
// enhance common error messages
const enhanceErrorMessages = (methodName, log) => {
program.Command.prototype[methodName] = function (...args) {
if (methodName === 'unknownOption' && this._allowUnknownOption) {
return
}
this.outputHelp()
console.log(` ` + chalk.red(log(...args)))
console.log()
Expand Down
86 changes: 86 additions & 0 deletions packages/@vue/cli/lib/invoke.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const resolve = require('resolve')
const Generator = require('./Generator')
const {
log,
error,
logWithSpinner,
stopSpinner
} = require('@vue/cli-shared-utils')

async function invoke (pluginName, options) {
delete options._
const context = process.cwd()
const pkgPath = path.resolve(context, 'package.json')

if (!fs.existsSync(pkgPath)) {
error(`package.json not found in ${chalk.yellow(context)}`)
process.exit(1)
}

const pkg = require(pkgPath)

// attempt to locate the plugin in package.json
const findPlugin = deps => {
if (!deps) return
let name
if (deps[name = pluginName] ||
deps[name = `@vue/cli-plugin-${pluginName}`] ||
deps[name = `vue-cli-plugin-${pluginName}`]) {
return name
}
}
const resolvedPluginName = (
findPlugin(pkg.devDependencies) ||
findPlugin(pkg.dependencies)
)

if (!resolvedPluginName) {
error(`Cannot resolve plugin ${chalk.yellow(pluginName)} from package.json.`)
process.exit(1)
}

const generatorURI = `${resolvedPluginName}/generator`
const generatorPath = resolve.sync(generatorURI, { basedir: context })
const plugin = {
id: resolvedPluginName,
apply: require(generatorPath),
options
}

const createCompleteCbs = []
const generator = new Generator(
context,
pkg,
[plugin],
createCompleteCbs
)

await generator.generate()

// TODO check if package.json was changed,
// if yes installDeps
logWithSpinner('📦', `Installing additional dependencies...`)

if (createCompleteCbs.lenght) {
logWithSpinner('⚓', `Running completion hooks...`)
for (const cb of createCompleteCbs) {
await cb()
}
}

stopSpinner()
log()
log(` Successfully invoked generator for plugin: ${chalk.cyan(resolvedPluginName)}`)
log(` You should review and commit the changes.`)
log()
}

module.exports = (pluginName, options) => {
invoke(pluginName, options).catch(err => {
error(err)
process.exit(1)
})
}
1 change: 1 addition & 0 deletions packages/@vue/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"inquirer": "^4.0.1",
"isbinaryfile": "^3.0.2",
"klaw-sync": "^3.0.2",
"minimist": "^1.2.0",
"mkdirp": "^0.5.1",
"resolve": "^1.5.0",
"rimraf": "^2.6.2",
Expand Down
2 changes: 1 addition & 1 deletion scripts/testChanged.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const additionalArgs = process.argv.slice(2)

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

0 comments on commit 132b0db

Please sign in to comment.