UNPKG

@lvl/wxapp-cli

Version:

wxapp-cli

134 lines (122 loc) 3.4 kB
#!/usr/bin/env node const program = require('commander') const pkg = require('../package.json') const updateNotifier = require('update-notifier') const { cmds } = require('../lib/config/wxapp-cli.config') const wxappCmd = require('../lib/cmd/index') const wxappHelp = require('../lib/help/index') updateNotifier({ pkg }).notify() program .version(pkg.version, '-v, --version') .usage('<command> [options]') .on('--help', function () { console.log('') console.log('Examples:') cmds.forEach(cmd => { wxappHelp[cmd]() }) console.log('') }) program .command('create <app-name>') .description('create a new wxapp project') .option('-c, --gitc [message]', 'Git initialization with initial commit message') .option('-g, --git', 'Skip git initialization') .option('-n, --no-git', 'Skip git initialization') .on('--help', function () { console.log('') console.log('Examples:') wxappHelp.create() console.log('') }) .action(function (appName, cmd) { let ops = cleanArgs(cmd) if (process.argv.includes('-c') || process.argv.includes('--gitc')) { options.forceGit = true } wxappCmd.create(appName, ops) }) program .command('add <type> <file-name>') .description('add a page, component, or package in an already created project') .option('-p, --page-name [pageName]', 'The page-name is required when the type is \'page\'') .option('-k, --package-name [packageName]', 'The package-name is required when the type is \'package\'') .on('--help', function () { console.log('') console.log('Examples:') wxappHelp.add() console.log('') }) .action(function (type, fileName, cmd) { let ops = cleanArgs(cmd) wxappCmd.add(type, fileName, ops) }) program .command('dev') .description('build in development mode') .on('--help', function () { console.log('') console.log('Examples:') wxappHelp.dev() console.log('') }) .action(function (cmd) { let ops = cleanArgs(cmd) wxappCmd.dev(ops) }) program .command('prod') .description('build in production mode') .on('--help', function () { console.log('') console.log('Examples:') wxappHelp.prod() console.log('') }) .action(function (cmd) { let ops = cleanArgs(cmd) wxappCmd.prod(ops) }) program .command('update') .description('update wxapp-cli template in an already created project') .on('--help', function () { console.log('') console.log('Examples:') wxappHelp.update() console.log('') }) .action(function (cmd) { let ops = cleanArgs(cmd) wxappCmd.update(ops) }) program .command('ui') .description('have fun') .on('--help', function () { console.log('') console.log('Examples:') wxappHelp.ui() console.log('') }) .action(function (...args) { wxappCmd.ui(...args) }) program.parse(process.argv) // 转驼峰 function camelize(str) { return str.replace(/-(\w)/g, (_, c) => c ? c.toUpperCase() : '') } // 从commander提取options function cleanArgs(cmd) { const args = {} cmd.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 }