Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit

Permalink
feat(cli): add user-friendly exit for unknown options (#854)
Browse files Browse the repository at this point in the history
  • Loading branch information
nozomuikuta authored Nov 18, 2021
1 parent c981a95 commit b734105
Showing 1 changed file with 51 additions and 32 deletions.
83 changes: 51 additions & 32 deletions packages/create-nuxt-app/lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,41 +24,60 @@ const showEnvInfo = async () => {
process.exit(1)
}

cli
.command('[out-dir]', 'Generate in a custom directory or current directory')
.option('-e, --edge', 'To install `nuxt-edge` instead of `nuxt`')
.option('-i, --info', 'Print out debugging information relating to the local environment')
.option('--answers <json>', 'Skip all the prompts and use the provided answers')
.option('--verbose', 'Show debug logs')
.option('--overwrite-dir', 'Overwrite the target directory')
.action((outDir = '.', cliOptions) => {
if (cliOptions.info) {
return showEnvInfo()
}
console.log()
console.log(chalk`{cyan create-nuxt-app v${version}}`)
const run = () => {
cli
.command('[out-dir]', 'Generate in a custom directory or current directory')
.option('-e, --edge', 'To install `nuxt-edge` instead of `nuxt`')
.option('-i, --info', 'Print out debugging information relating to the local environment')
.option('--answers <json>', 'Skip all the prompts and use the provided answers')
.option('--verbose', 'Show debug logs')
.option('--overwrite-dir', 'Overwrite the target directory')
.action((outDir = '.', cliOptions) => {
if (cliOptions.info) {
return showEnvInfo()
}
console.log()
console.log(chalk`{cyan create-nuxt-app v${version}}`)

const { answers, overwriteDir, verbose } = cliOptions
if (fs.existsSync(outDir) && fs.readdirSync(outDir).length && !overwriteDir) {
const baseDir = outDir === '.' ? path.basename(process.cwd()) : outDir
return console.error(chalk.red(
`Could not create project in ${chalk.bold(baseDir)} because the directory is not empty.`))
}
const { answers, overwriteDir, verbose } = cliOptions
if (fs.existsSync(outDir) && fs.readdirSync(outDir).length && !overwriteDir) {
const baseDir = outDir === '.' ? path.basename(process.cwd()) : outDir
return console.error(chalk.red(
`Could not create project in ${chalk.bold(baseDir)} because the directory is not empty.`))
}

console.log(chalk`✨ Generating Nuxt.js project in {cyan ${outDir}}`)
console.log(chalk`✨ Generating Nuxt.js project in {cyan ${outDir}}`)

const logLevel = verbose ? 4 : 2
// See https://sao.vercel.app/api.html#standalone-cli
sao({ generator, outDir, logLevel, answers, cliOptions })
.run()
.catch((err) => {
console.trace(err)
process.exit(1)
})
})
const logLevel = verbose ? 4 : 2
// See https://sao.vercel.app/api.html#standalone-cli
sao({ generator, outDir, logLevel, answers, cliOptions })
.run()
.catch((err) => {
console.trace(err)
process.exit(1)
})
})

cli.help()

cli.help()
cli.version(version)

cli.version(version)
cli.parse()
}

cli.parse()
try {
run()
} catch (err) {
// https://github.com/cacjs/cac/blob/f51fc2254d7ea30b4faea76f69f52fe291811e4f/src/utils.ts#L152
// https://github.com/cacjs/cac/blob/f51fc2254d7ea30b4faea76f69f52fe291811e4f/src/Command.ts#L258
if (err.name === 'CACError' && err.message.startsWith('Unknown option')) {
console.error()
console.error(chalk.red(err.message))
console.error()
cli.outputHelp()
} else {
console.error()
console.error(err)
}
process.exit(1)
}

0 comments on commit b734105

Please sign in to comment.