From 1021b89df74186698d2dbd68ed34b212d42c5dc5 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Tue, 27 Nov 2018 23:31:47 +0800 Subject: [PATCH 1/5] chore: init --- packages/@vuepress/cli/index.js | 70 ++++++----------------------- packages/@vuepress/cli/package.json | 4 +- yarn.lock | 8 ++-- 3 files changed, 20 insertions(+), 62 deletions(-) diff --git a/packages/@vuepress/cli/index.js b/packages/@vuepress/cli/index.js index 6f6d732936..a234ae6795 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,13 +33,11 @@ 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]') - 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') @@ -68,9 +66,8 @@ 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') @@ -98,57 +95,18 @@ 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', () => { + // Listen to unknown commands + cli.on('command:*', () => { + console.error('Unknown command: %s', cli.args.join(' ')) console.log() - console.log(` Run ${chalk.cyan(`vuepress --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)}` : `` - ) }) function wrapCommand (fn) { @@ -160,8 +118,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..a526a8a057 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.2" }, "peerDependencies": { "@vuepress/core": "^1.0.0-alpha.1" diff --git a/yarn.lock b/yarn.lock index 5889989c9d..4111ee02f1 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.2: + version "6.3.2" + resolved "https://registry.yarnpkg.com/cac/-/cac-6.3.2.tgz#7578e161670abac154812860b2951782c32192a7" + 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" From 35eb4f2b4162853bb52848a1fff91be59a14f860 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Wed, 28 Nov 2018 00:39:03 +0800 Subject: [PATCH 2/5] chore: some fixes --- packages/@vuepress/cli/index.js | 50 ++++++++++++++++------------- packages/@vuepress/cli/package.json | 2 +- yarn.lock | 6 ++-- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/packages/@vuepress/cli/index.js b/packages/@vuepress/cli/index.js index a234ae6795..13dba0fa55 100644 --- a/packages/@vuepress/cli/index.js +++ b/packages/@vuepress/cli/index.js @@ -35,25 +35,28 @@ exports.bootstrap = function ({ cli .version(pkg.version) + .help() 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('-p, --port [port]', 'use specified port (default: 8080)') + .option('-h, --host [host]', 'use specified host (default: 0.0.0.0)') + .option('-t, --temp [temp]', 'set the directory of the temporary file') + .option('-c, --cache [cache]', 'set the directory of cache') .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('options', options) env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' }) wrapCommand(dev)(path.resolve(sourceDir), { @@ -68,20 +71,22 @@ exports.bootstrap = function ({ 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('-d, --dest [dest]', 'specify build output dir (default: .vuepress/dist)') + .option('-t, --temp [temp]', 'set the directory of the temporary file') + .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('options', options) env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' }) wrapCommand(build)(path.resolve(sourceDir), { @@ -103,7 +108,6 @@ exports.bootstrap = function ({ }) // output help information on unknown commands - // Listen to unknown commands cli.on('command:*', () => { console.error('Unknown command: %s', cli.args.join(' ')) console.log() diff --git a/packages/@vuepress/cli/package.json b/packages/@vuepress/cli/package.json index a526a8a057..72a5b3b27f 100644 --- a/packages/@vuepress/cli/package.json +++ b/packages/@vuepress/cli/package.json @@ -30,7 +30,7 @@ "dependencies": { "chalk": "^2.3.2", "semver": "^5.5.0", - "cac": "^6.3.2" + "cac": "^6.3.3" }, "peerDependencies": { "@vuepress/core": "^1.0.0-alpha.1" diff --git a/yarn.lock b/yarn.lock index 4111ee02f1..d0a30abddd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1681,9 +1681,9 @@ byline@^5.0.0: version "5.0.0" resolved "https://registry.yarnpkg.com/byline/-/byline-5.0.0.tgz#741c5216468eadc457b03410118ad77de8c1ddb1" -cac@^6.3.2: - version "6.3.2" - resolved "https://registry.yarnpkg.com/cac/-/cac-6.3.2.tgz#7578e161670abac154812860b2951782c32192a7" +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" From 9aafaf514e0f7f928df62232b3021bd5be2599ca Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Wed, 28 Nov 2018 00:45:11 +0800 Subject: [PATCH 3/5] chore: remove -h from --host --- packages/@vuepress/cli/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@vuepress/cli/index.js b/packages/@vuepress/cli/index.js index 13dba0fa55..2dd4e2d7ea 100644 --- a/packages/@vuepress/cli/index.js +++ b/packages/@vuepress/cli/index.js @@ -40,9 +40,9 @@ exports.bootstrap = function ({ cli .command('dev [targetDir]', 'start development server') .option('-p, --port [port]', 'use specified port (default: 8080)') - .option('-h, --host [host]', 'use specified host (default: 0.0.0.0)') .option('-t, --temp [temp]', 'set the directory of the temporary file') .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') From dd98d1478413487c352d2182975fecc6132fa275 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Wed, 28 Nov 2018 00:52:40 +0800 Subject: [PATCH 4/5] chore: tweaks --- packages/@vuepress/cli/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/@vuepress/cli/index.js b/packages/@vuepress/cli/index.js index 2dd4e2d7ea..d0627b3900 100644 --- a/packages/@vuepress/cli/index.js +++ b/packages/@vuepress/cli/index.js @@ -56,7 +56,7 @@ exports.bootstrap = function ({ silent } = options logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 }) - logger.debug('options', options) + logger.debug('cli_options', options) env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' }) wrapCommand(dev)(path.resolve(sourceDir), { @@ -86,7 +86,7 @@ exports.bootstrap = function ({ silent } = options logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 }) - logger.debug('options', options) + logger.debug('cli_options', options) env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' }) wrapCommand(build)(path.resolve(sourceDir), { From 9b99ca7de0c2760b8a1fa82984722a59713dde31 Mon Sep 17 00:00:00 2001 From: ULIVZ <472590061@qq.com> Date: Wed, 28 Nov 2018 00:57:43 +0800 Subject: [PATCH 5/5] chore: some updates --- packages/@vuepress/cli/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/@vuepress/cli/index.js b/packages/@vuepress/cli/index.js index d0627b3900..c132a7b310 100644 --- a/packages/@vuepress/cli/index.js +++ b/packages/@vuepress/cli/index.js @@ -39,10 +39,10 @@ exports.bootstrap = function ({ cli .command('dev [targetDir]', 'start development server') - .option('-p, --port [port]', 'use specified port (default: 8080)') - .option('-t, --temp [temp]', 'set the directory of the temporary file') + .option('-p, --port ', 'use specified port (default: 8080)') + .option('-t, --temp ', 'set the directory of the temporary file') .option('-c, --cache [cache]', 'set the directory of cache') - .option('--host', 'use specified host (default: 0.0.0.0)') + .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') @@ -71,8 +71,8 @@ exports.bootstrap = function ({ cli .command('build [targetDir]', 'build dir as static site') - .option('-d, --dest [dest]', 'specify build output dir (default: .vuepress/dist)') - .option('-t, --temp [temp]', 'set the directory of the temporary file') + .option('-d, --dest ', 'specify build output dir (default: .vuepress/dist)') + .option('-t, --temp ', 'set the directory of the temporary file') .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')