Files
vue-cli/packages/@vue/cli/bin/vue
2018-01-03 13:21:48 -05:00

91 lines
2.7 KiB
JavaScript
Executable File

#!/usr/bin/env node
const chalk = require('chalk')
const semver = require('semver')
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)
}
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 options')
.option('-d, --default', 'Skip prompts and use default options')
.option('-r, --registry <url>', 'Use specified NPM registry when installing dependencies')
.option('-p, --package-manager <command>', 'Use specified NPM client when installing dependencies')
.action(require('../lib/create'))
program
.command('init <template> <app-name>')
.description('generate a project from a remote template (requires @vue/cli-init)')
.action(() => {
loadCommand('init', '@vue/cli-init')
})
program
.command('serve [filename]')
.description('serve a .js or vue file in development mode with zero config')
.action(filename => {
loadCommand('serve', '@vue/cli-service-global').serve(filename)
})
program
.command('build [filename]')
.description('build a .js or .vue file in production mode with zero config')
.action(filename => {
loadCommand('build', '@vue/cli-service-global').build(filename)
})
// 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) {
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()
}