Skip to content

Commit 1a897cb

Browse files
authored
feat($cli): migrate to CAC (#1049)
1 parent 51463b9 commit 1a897cb

File tree

3 files changed

+42
-80
lines changed

3 files changed

+42
-80
lines changed

packages/@vuepress/cli/index.js

+36-74
Original file line numberDiff line numberDiff line change
@@ -23,39 +23,40 @@ if (!semver.satisfies(process.version, requiredVersion)) {
2323
process.exit(1)
2424
}
2525

26-
const program = require('commander')
26+
const cli = require('cac')()
2727

28-
exports.program = program
28+
exports.cli = cli
2929
exports.bootstrap = function ({
3030
plugins,
3131
theme
3232
} = {}) {
3333
const { path, logger, env } = require('@vuepress/shared-utils')
3434
const { dev, build, eject } = require('@vuepress/core')
3535

36-
program
36+
cli
3737
.version(pkg.version)
38-
.usage('<command> [options]')
38+
.help()
3939

40-
program
41-
.command('dev [targetDir]')
42-
.description('start development server')
40+
cli
41+
.command('dev [targetDir]', 'start development server')
4342
.option('-p, --port <port>', 'use specified port (default: 8080)')
44-
.option('-h, --host <host>', 'use specified host (default: 0.0.0.0)')
4543
.option('-t, --temp <temp>', 'set the directory of the temporary file')
46-
.option('-c, --cache <cache>', 'set the directory of cache')
44+
.option('-c, --cache [cache]', 'set the directory of cache')
45+
.option('--host <host>', 'use specified host (default: 0.0.0.0)')
4746
.option('--no-cache', 'clean the cache before build')
4847
.option('--debug', 'start development server in debug mode')
4948
.option('--silent', 'start development server in silent mode')
50-
.action((sourceDir = '.', {
51-
host,
52-
port,
53-
debug,
54-
temp,
55-
cache,
56-
silent
57-
}) => {
49+
.action((sourceDir = '.', options) => {
50+
const {
51+
host,
52+
port,
53+
debug,
54+
temp,
55+
cache,
56+
silent
57+
} = options
5858
logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 })
59+
logger.debug('cli_options', options)
5960
env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' })
6061

6162
wrapCommand(dev)(path.resolve(sourceDir), {
@@ -68,23 +69,24 @@ exports.bootstrap = function ({
6869
})
6970
})
7071

71-
program
72-
.command('build [targetDir]')
73-
.description('build dir as static site')
72+
cli
73+
.command('build [targetDir]', 'build dir as static site')
7474
.option('-d, --dest <dest>', 'specify build output dir (default: .vuepress/dist)')
7575
.option('-t, --temp <temp>', 'set the directory of the temporary file')
76-
.option('-c, --cache <cache>', 'set the directory of cache')
76+
.option('-c, --cache [cache]', 'set the directory of cache')
7777
.option('--no-cache', 'clean the cache before build')
7878
.option('--debug', 'build in development mode for debugging')
7979
.option('--silent', 'build static site in silent mode')
80-
.action((sourceDir = '.', {
81-
debug,
82-
dest,
83-
temp,
84-
cache,
85-
silent
86-
}) => {
80+
.action((sourceDir = '.', options) => {
81+
const {
82+
debug,
83+
dest,
84+
temp,
85+
cache,
86+
silent
87+
} = options
8788
logger.setOptions({ logLevel: silent ? 1 : debug ? 4 : 3 })
89+
logger.debug('cli_options', options)
8890
env.setOptions({ isDebug: debug, isTest: process.env.NODE_ENV === 'test' })
8991

9092
wrapCommand(build)(path.resolve(sourceDir), {
@@ -98,59 +100,19 @@ exports.bootstrap = function ({
98100
})
99101
})
100102

101-
program
102-
.command('eject [targetDir]')
103-
.description('copy the default theme into .vuepress/theme for customization.')
103+
cli
104+
.command('eject [targetDir]', 'copy the default theme into .vuepress/theme for customization.')
104105
.option('--debug', 'eject in debug mode')
105106
.action((dir = '.') => {
106107
wrapCommand(eject)(path.resolve(dir))
107108
})
108109

109110
// output help information on unknown commands
110-
program
111-
.arguments('<command>')
112-
.action((cmd) => {
113-
program.outputHelp()
114-
console.log(` ` + chalk.red(`Unknown command ${chalk.yellow(cmd)}.`))
115-
console.log()
116-
})
117-
118-
// add some useful info on help
119-
program.on('--help', () => {
120-
console.log()
121-
console.log(` Run ${chalk.cyan(`vuepress <command> --help`)} for detailed usage of given command.`)
111+
cli.on('command:*', () => {
112+
console.error('Unknown command: %s', cli.args.join(' '))
122113
console.log()
123114
})
124115

125-
program.commands.forEach(c => c.on('--help', () => console.log()))
126-
127-
// enhance common error messages
128-
const enhanceErrorMessages = (methodName, log) => {
129-
program.Command.prototype[methodName] = function (...args) {
130-
if (methodName === 'unknownOption' && this._allowUnknownOption) {
131-
return
132-
}
133-
this.outputHelp()
134-
console.log(` ` + chalk.red(log(...args)))
135-
console.log()
136-
process.exit(1)
137-
}
138-
}
139-
140-
enhanceErrorMessages('missingArgument', argName => {
141-
return `Missing required argument ${chalk.yellow(`<${argName}>`)}.`
142-
})
143-
144-
enhanceErrorMessages('unknownOption', optionName => {
145-
return `Unknown option ${chalk.yellow(optionName)}.`
146-
})
147-
148-
enhanceErrorMessages('optionMissingArgument', (option, flag) => {
149-
return `Missing required argument for option ${chalk.yellow(option.flags)}` + (
150-
flag ? `, got ${chalk.yellow(flag)}` : ``
151-
)
152-
})
153-
154116
function wrapCommand (fn) {
155117
return (...args) => {
156118
return fn(...args).catch(err => {
@@ -160,8 +122,8 @@ exports.bootstrap = function ({
160122
}
161123
}
162124

163-
program.parse(process.argv)
125+
cli.parse(process.argv)
164126
if (!process.argv.slice(2).length) {
165-
program.outputHelp()
127+
cli.outputHelp()
166128
}
167129
}

packages/@vuepress/cli/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
},
3030
"dependencies": {
3131
"chalk": "^2.3.2",
32-
"commander": "^2.15.1",
33-
"semver": "^5.5.0"
32+
"semver": "^5.5.0",
33+
"cac": "^6.3.3"
3434
},
3535
"peerDependencies": {
3636
"@vuepress/core": "^1.0.0-alpha.1"

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,10 @@ byline@^5.0.0:
16811681
version "5.0.0"
16821682
resolved "https://registry.yarnpkg.com/byline/-/byline-5.0.0.tgz#741c5216468eadc457b03410118ad77de8c1ddb1"
16831683

1684+
cac@^6.3.3:
1685+
version "6.3.3"
1686+
resolved "https://registry.yarnpkg.com/cac/-/cac-6.3.3.tgz#01e56f50068bd1be326b1612950d77d31112400d"
1687+
16841688
cacache@^10.0.4:
16851689
version "10.0.4"
16861690
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:
20742078
version "2.15.1"
20752079
resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f"
20762080

2077-
commander@^2.15.1:
2078-
version "2.16.0"
2079-
resolved "https://registry.yarnpkg.com/commander/-/commander-2.16.0.tgz#f16390593996ceb4f3eeb020b31d78528f7f8a50"
2080-
20812081
commander@~2.13.0:
20822082
version "2.13.0"
20832083
resolved "https://registry.yarnpkg.com/commander/-/commander-2.13.0.tgz#6964bca67685df7c1f1430c584f07d7597885b9c"

0 commit comments

Comments
 (0)