mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-01-26 00:59:02 -06:00
135 lines
4.2 KiB
JavaScript
Executable File
135 lines
4.2 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const fs = require('fs')
|
|
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
|
|
|
|
if (!semver.satisfies(process.version, requiredVersion)) {
|
|
error(
|
|
`You are using Node ${process.version}, but this version of vue-cli ` +
|
|
`requires Node ${requiredVersion}.\nPlease upgrade your Node version.`
|
|
)
|
|
process.exit(1)
|
|
}
|
|
|
|
// enter debug mode when creating test repo
|
|
if (
|
|
slash(process.cwd()).indexOf('/packages/test') > 0 && (
|
|
fs.existsSync(path.resolve(process.cwd(), '../@vue')) ||
|
|
fs.existsSync(path.resolve(process.cwd(), '../../@vue'))
|
|
)
|
|
) {
|
|
process.env.VUE_CLI_DEBUG = true
|
|
}
|
|
|
|
const program = require('commander')
|
|
const loadCommand = require('../lib/util/loadCommand')
|
|
|
|
program
|
|
.version(require('../package').version)
|
|
.usage('<command> [options]')
|
|
|
|
program
|
|
.command('create <app-name>')
|
|
.description('create a new project powered by vue-cli-service')
|
|
.option('-s, --saved', 'Skip prompts and use saved config')
|
|
.option('-d, --default', 'Skip prompts and use default config')
|
|
.option('-c, --config <json>', 'Skip prompts and use inline JSON string as config')
|
|
.option('-r, --registry <url>', 'Use specified NPM registry when installing dependencies')
|
|
.option('-m, --packageManager <command>', 'Use specified NPM client when installing dependencies')
|
|
.option('-f, --force', 'Overwrite target directory if it exists')
|
|
.action((name, cmd) => {
|
|
require('../lib/create')(name, cleanArgs(cmd))
|
|
})
|
|
|
|
program
|
|
.command('invoke <plugin>')
|
|
.allowUnknownOption()
|
|
.description('invoke the generator of a 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')
|
|
.option('-o, --open', 'Open browser')
|
|
.action((entry, cmd) => {
|
|
loadCommand('serve', '@vue/cli-service-global').serve(entry, cleanArgs(cmd))
|
|
})
|
|
|
|
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))
|
|
})
|
|
|
|
program
|
|
.command('init <template> <app-name>')
|
|
.description('generate a project from a remote template (legacy API, requires @vue/cli-init)')
|
|
.action(() => {
|
|
loadCommand('init', '@vue/cli-init')
|
|
})
|
|
|
|
// add some useful info on help
|
|
program.on('--help', () => {
|
|
console.log()
|
|
console.log(` Run ${chalk.cyan(`vue <command> --help`)} for detailed usage of given command.`)
|
|
console.log()
|
|
})
|
|
|
|
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()
|
|
process.exit(1)
|
|
}
|
|
}
|
|
|
|
enhanceErrorMessages('missingArgument', argName => {
|
|
return `Missing required argument ${chalk.yellow(`<${argName}>`)}.`
|
|
})
|
|
|
|
enhanceErrorMessages('unknownOption', optionName => {
|
|
return `Unknown option ${chalk.yellow(optionName)}.`
|
|
})
|
|
|
|
enhanceErrorMessages('optionMissingArgument', (option, flag) => {
|
|
return `Missing required argument for option ${chalk.yellow(option.flags)}` + (
|
|
flag ? `, got ${chalk.yellow(flag)}` : ``
|
|
)
|
|
})
|
|
|
|
program.parse(process.argv)
|
|
|
|
if (!process.argv.slice(2).length) {
|
|
program.outputHelp()
|
|
}
|
|
|
|
// commander passes the Command object itself as options,
|
|
// extract only actual options into a fresh object.
|
|
function cleanArgs (cmd) {
|
|
const args = {}
|
|
cmd.options.forEach(o => {
|
|
const key = o.long.replace(/^--/, '')
|
|
args[key] = cmd[key]
|
|
})
|
|
return args
|
|
}
|