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

feat(cli): add user-friendly exit for unknown options #854

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Changes from all 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
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)
}