diff --git a/packages/@vuepress/cli/index.js b/packages/@vuepress/cli/index.js index 6f6d732936..c132a7b310 100644 --- a/packages/@vuepress/cli/index.js +++ b/packages/@vuepress/cli/index.js @@ -23,9 +23,9 @@ if (!semver.satisfies(process.version, requiredVersion)) { process.exit(1) } -const program = require('commander') +const cli = require('cac')() -exports.program = program +exports.cli = cli exports.bootstrap = function ({ plugins, theme @@ -33,29 +33,30 @@ exports.bootstrap = function ({ const { path, logger, env } = require('@vuepress/shared-utils') const { dev, build, eject } = require('@vuepress/core') - program + cli .version(pkg.version) - .usage(' [options]') + .help() - program - .command('dev [targetDir]') - .description('start development server') + cli + .command('dev [targetDir]', 'start development server') .option('-p, --port ', 'use specified port (default: 8080)') - .option('-h, --host ', 'use specified host (default: 0.0.0.0)') .option('-t, --temp ', 'set the directory of the temporary file') - .option('-c, --cache ', 'set the directory of cache') + .option('-c, --cache [cache]', 'set the directory of cache') + .option('--host ', 'use specified host (default: 0.0.0.0)') .option('--no-cache', 'clean the cache before build') .option('--debug', 'start development server in debug mode') .option('--silent', 'start development server in silent mode') - .action((sourceDir = '.', { - host, - port, - debug, - temp, - cache, - silent - }) => { + .action((sourceDir = '.', options) => { + const { + host, + port, + debug, + temp, + cache, + silent + } = options logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 }) + logger.debug('cli_options', options) env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' }) wrapCommand(dev)(path.resolve(sourceDir), { @@ -68,23 +69,24 @@ exports.bootstrap = function ({ }) }) - program - .command('build [targetDir]') - .description('build dir as static site') + cli + .command('build [targetDir]', 'build dir as static site') .option('-d, --dest ', 'specify build output dir (default: .vuepress/dist)') .option('-t, --temp ', 'set the directory of the temporary file') - .option('-c, --cache ', 'set the directory of cache') + .option('-c, --cache [cache]', 'set the directory of cache') .option('--no-cache', 'clean the cache before build') .option('--debug', 'build in development mode for debugging') .option('--silent', 'build static site in silent mode') - .action((sourceDir = '.', { - debug, - dest, - temp, - cache, - silent - }) => { + .action((sourceDir = '.', options) => { + const { + debug, + dest, + temp, + cache, + silent + } = options logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 }) + logger.debug('cli_options', options) env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' }) wrapCommand(build)(path.resolve(sourceDir), { @@ -98,59 +100,19 @@ exports.bootstrap = function ({ }) }) - program - .command('eject [targetDir]') - .description('copy the default theme into .vuepress/theme for customization.') + cli + .command('eject [targetDir]', 'copy the default theme into .vuepress/theme for customization.') .option('--debug', 'eject in debug mode') .action((dir = '.') => { wrapCommand(eject)(path.resolve(dir)) }) // output help information on unknown commands - program - .arguments('') - .action((cmd) => { - program.outputHelp() - console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`)) - console.log() - }) - - // add some useful info on help - program.on('--help', () => { - console.log() - console.log(` Run ${chalk.cyan(`vuepress --help`)} for detailed usage of given command.`) + cli.on('command:*', () => { + console.error('Unknown command: %s', cli.args.join(' ')) 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)}` : `` - ) - }) - function wrapCommand (fn) { return (...args) => { return fn(...args).catch(err => { @@ -160,8 +122,8 @@ exports.bootstrap = function ({ } } - program.parse(process.argv) + cli.parse(process.argv) if (!process.argv.slice(2).length) { - program.outputHelp() + cli.outputHelp() } } diff --git a/packages/@vuepress/cli/package.json b/packages/@vuepress/cli/package.json index 559f496c5b..72a5b3b27f 100644 --- a/packages/@vuepress/cli/package.json +++ b/packages/@vuepress/cli/package.json @@ -29,8 +29,8 @@ }, "dependencies": { "chalk": "^2.3.2", - "commander": "^2.15.1", - "semver": "^5.5.0" + "semver": "^5.5.0", + "cac": "^6.3.3" }, "peerDependencies": { "@vuepress/core": "^1.0.0-alpha.1" diff --git a/yarn.lock b/yarn.lock index 5889989c9d..d0a30abddd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1681,6 +1681,10 @@ byline@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/byline/-/byline-5.0.0.tgz#741c5216468eadc457b03410118ad77de8c1ddb1" +cac@^6.3.3: + version "6.3.3" + resolved "https://registry.yarnpkg.com/cac/-/cac-6.3.3.tgz#01e56f50068bd1be326b1612950d77d31112400d" + cacache@^10.0.4: version "10.0.4" resolved "https://registry.yarnpkg.com/cacache/-/cacache-10.0.4.tgz#6452367999eff9d4188aefd9a14e9d7c6a263460" @@ -2074,10 +2078,6 @@ commander@2.15.x, commander@^2.14.1, commander@^2.9.0, commander@~2.15.0: version "2.15.1" resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f" -commander@^2.15.1: - version "2.16.0" - resolved "https://registry.yarnpkg.com/commander/-/commander-2.16.0.tgz#f16390593996ceb4f3eeb020b31d78528f7f8a50" - commander@~2.13.0: version "2.13.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.13.0.tgz#6964bca67685df7c1f1430c584f07d7597885b9c"