@suwis/funtask
Version:
Funtask js sdk
140 lines (114 loc) • 3.99 kB
JavaScript
// 颜色模块
const chalk = require('chalk')
const program = require('commander')
const minimist = require('minimist')
const devhelper = require('./../lib/devhelper')
program
.version(require('../package').version)
.usage('<command> [options]')
// 创建插件命令
program
.command('create <app-name>')
.description('create a new project powered by funtask')
.action((name, cmd) => {
const options = cleanArgs(cmd)
if (minimist(process.argv.slice(3))._.length > 1) {
console.log(chalk.yellow('\n Info: You provided more than one argument. The first one will be used as the app\'s name, the rest are ignored.'))
}
// 引入构建器
require('../lib/generate')(name, options)
})
// 引入开发插件
program
.command('link')
.description('link app to funtask')
.action((name, cmd) => {
// 引入构建器
devhelper.link(name)
})
// 移除开发插件
program
.command('unlink')
.description('unlink app from funtask')
.action((cmd, args) => {
let name = args instanceof Array ? args[0] : ''
// 引入构建器
devhelper.unlink(name)
})
// ui 界面
program
.command('ui')
.description('start and open the vue-cli ui')
.option('-H, --host <host>', 'Host used for the UI server (default: localhost)')
.option('-p, --port <port>', 'Port used for the UI server (by default search for available port)')
.option('-D, --dev', 'Run in dev mode')
.option('--quiet', `Don't output starting messages`)
.option('--headless', `Don't open browser on start and output port`)
.action((cmd) => {
checkNodeVersion('>=8.6', 'funtask ui')
require('../lib/ui')(cleanArgs(cmd))
})
// output help information on unknown commands
program
.arguments('<command>')
.action((cmd) => {
program.outputHelp()
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`))
suggestCommands(cmd)
})
// 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 = require('../lib/util/enhanceErrorMessages')
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()
}
// 检测命令行是否支持
function suggestCommands(cmd) {
const availableCommands = program.commands.map(cmd => {
return cmd._name
})
const suggestion = didYouMean(cmd, availableCommands)
if (suggestion) {
console.log(` ` + chalk.red(`Did you mean ${chalk.yellow(suggestion)}?`))
}
}
function didYouMean(cmd, availableCommands) {
// console.log(cmd, availableCommands)
}
function camelize(str) {
return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '')
}
// commander passes the Command object itself as options,
// extract only actual options into a fresh object.
function cleanArgs(cmd) {
const args = {}
let options = cmd instanceof Object ? cmd.options : []
options.forEach(o => {
const key = camelize(o.long.replace(/^--/, ''))
// if an option is not present and Command has a method with the same name
// it should not be copied
if (typeof cmd[key] !== 'function' && typeof cmd[key] !== 'undefined') {
args[key] = cmd[key]
}
})
return args
}