Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to CAC #1049

Merged
merged 5 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 41 additions & 79 deletions packages/@vuepress/cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,40 @@ 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
} = {}) {
const { path, logger, env } = require('@vuepress/shared-utils')
const { dev, build, eject } = require('@vuepress/core')

program
cli
.version(pkg.version)
.usage('<command> [options]')

program
.command('dev [targetDir]')
.description('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')
.help()

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)')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use <> if the flag requires a string-type value. i.e. it would fail on --host but work on --host localhost

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated at 9b99ca7

.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), {
Expand All @@ -68,23 +69,24 @@ exports.bootstrap = function ({
})
})

program
.command('build [targetDir]')
.description('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('-c, --cache <cache>', 'set the directory of cache')
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('-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), {
Expand All @@ -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('<command>')
.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 <command> --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 => {
Expand All @@ -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()
}
}
4 changes: 2 additions & 2 deletions packages/@vuepress/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -2074,10 +2078,6 @@ [email protected], 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"
Expand Down