Skip to content

Commit bcc2208

Browse files
committedOct 24, 2022
feat(cli): print help messages if no matching command
1 parent 01975a1 commit bcc2208

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed
 

‎packages/cli/src/cli.ts

+14-18
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,6 @@ import { createBuild, createDev, info } from './commands/index.js'
77

88
const require = createRequire(import.meta.url)
99

10-
/**
11-
* Wrap raw command to catch errors and exit process
12-
*/
13-
const wrapCommand = (cmd: (...args: any[]) => Promise<void>): typeof cmd => {
14-
const wrappedCommand: typeof cmd = (...args) =>
15-
cmd(...args).catch((err) => {
16-
console.error(chalk.red(err.stack))
17-
process.exit(1)
18-
})
19-
return wrappedCommand
20-
}
21-
2210
/**
2311
* Vuepress cli
2412
*/
@@ -47,7 +35,7 @@ export const cli = (defaultAppConfig: Partial<AppConfig> = {}): void => {
4735
.option('--open', 'Open browser when ready')
4836
.option('--debug', 'Enable debug mode')
4937
.option('--no-watch', 'Disable watching page and config files')
50-
.action(wrapCommand(createDev(defaultAppConfig)))
38+
.action(createDev(defaultAppConfig))
5139

5240
// register `build` command
5341
program
@@ -62,12 +50,20 @@ export const cli = (defaultAppConfig: Partial<AppConfig> = {}): void => {
6250
.option('--clean-temp', 'Clean the temporary files before build')
6351
.option('--clean-cache', 'Clean the cache files before build')
6452
.option('--debug', 'Enable debug mode')
65-
.action(wrapCommand(createBuild(defaultAppConfig)))
53+
.action(createBuild(defaultAppConfig))
6654

6755
// register `info` command
68-
program
69-
.command('info', 'Display environment information')
70-
.action(wrapCommand(info))
56+
program.command('info', 'Display environment information').action(info)
57+
58+
program.parse(process.argv, { run: false })
7159

72-
program.parse(process.argv)
60+
// run command or fallback to help messages
61+
if (program.matchedCommand) {
62+
program.runMatchedCommand().catch((err) => {
63+
console.error(chalk.red(err.stack))
64+
process.exit(1)
65+
})
66+
} else {
67+
program.outputHelp()
68+
}
7369
}

0 commit comments

Comments
 (0)
Please sign in to comment.