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

Commit b734105

Browse files
authored
feat(cli): add user-friendly exit for unknown options (#854)
1 parent c981a95 commit b734105

File tree

1 file changed

+51
-32
lines changed
  • packages/create-nuxt-app/lib

1 file changed

+51
-32
lines changed

packages/create-nuxt-app/lib/cli.js

+51-32
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,60 @@ const showEnvInfo = async () => {
2424
process.exit(1)
2525
}
2626

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

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

48-
console.log(chalk`✨ Generating Nuxt.js project in {cyan ${outDir}}`)
49+
console.log(chalk`✨ Generating Nuxt.js project in {cyan ${outDir}}`)
4950

50-
const logLevel = verbose ? 4 : 2
51-
// See https://sao.vercel.app/api.html#standalone-cli
52-
sao({ generator, outDir, logLevel, answers, cliOptions })
53-
.run()
54-
.catch((err) => {
55-
console.trace(err)
56-
process.exit(1)
57-
})
58-
})
51+
const logLevel = verbose ? 4 : 2
52+
// See https://sao.vercel.app/api.html#standalone-cli
53+
sao({ generator, outDir, logLevel, answers, cliOptions })
54+
.run()
55+
.catch((err) => {
56+
console.trace(err)
57+
process.exit(1)
58+
})
59+
})
60+
61+
cli.help()
5962

60-
cli.help()
63+
cli.version(version)
6164

62-
cli.version(version)
65+
cli.parse()
66+
}
6367

64-
cli.parse()
68+
try {
69+
run()
70+
} catch (err) {
71+
// https://github.com/cacjs/cac/blob/f51fc2254d7ea30b4faea76f69f52fe291811e4f/src/utils.ts#L152
72+
// https://github.com/cacjs/cac/blob/f51fc2254d7ea30b4faea76f69f52fe291811e4f/src/Command.ts#L258
73+
if (err.name === 'CACError' && err.message.startsWith('Unknown option')) {
74+
console.error()
75+
console.error(chalk.red(err.message))
76+
console.error()
77+
cli.outputHelp()
78+
} else {
79+
console.error()
80+
console.error(err)
81+
}
82+
process.exit(1)
83+
}

0 commit comments

Comments
 (0)