Skip to content

Commit 60d62d1

Browse files
authored
fix(cli): output help when no conventional config + no subcommand (#9648)
1 parent affca7a commit 60d62d1

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

packages/docusaurus/bin/docusaurus.mjs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ cli.arguments('<command>').action((cmd) => {
218218
logger.error` Unknown command name=${cmd}.`;
219219
});
220220

221+
// === The above is the commander configuration ===
222+
// They don't start any code execution yet until cli.parse() is called below
223+
221224
/**
222225
* @param {string | undefined} command
223226
*/
@@ -237,12 +240,29 @@ function isInternalCommand(command) {
237240
);
238241
}
239242

240-
if (!isInternalCommand(process.argv.slice(2)[0])) {
241-
await externalCommand(cli);
242-
}
243+
// process.argv always looks like this:
244+
// [
245+
// '/path/to/node',
246+
// '/path/to/docusaurus.mjs',
247+
// '<subcommand>',
248+
// ...subcommandArgs
249+
// ]
243250

244-
if (!process.argv.slice(2).length) {
251+
// There is no subcommand
252+
// TODO: can we use commander to handle this case?
253+
if (process.argv.length < 3 || process.argv[2]?.startsWith('--')) {
245254
cli.outputHelp();
255+
process.exit(1);
256+
}
257+
258+
// There is an unrecognized subcommand
259+
// Let plugins extend the CLI before parsing
260+
if (!isInternalCommand(process.argv[2])) {
261+
// TODO: in this step, we must assume default site structure because there's
262+
// no way to know the siteDir/config yet. Maybe the root cli should be
263+
// responsible for parsing these arguments?
264+
// https://github.com/facebook/docusaurus/issues/8903
265+
await externalCommand(cli);
246266
}
247267

248268
cli.parse(process.argv);

0 commit comments

Comments
 (0)