From 60d62d1d65dc9972e36afecf07311ab9b2a0fcd9 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 21 Dec 2023 06:28:37 -0500 Subject: [PATCH] fix(cli): output help when no conventional config + no subcommand (#9648) --- packages/docusaurus/bin/docusaurus.mjs | 28 ++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/docusaurus/bin/docusaurus.mjs b/packages/docusaurus/bin/docusaurus.mjs index a2738c3866b6..b65915bcd6bd 100755 --- a/packages/docusaurus/bin/docusaurus.mjs +++ b/packages/docusaurus/bin/docusaurus.mjs @@ -218,6 +218,9 @@ cli.arguments('').action((cmd) => { logger.error` Unknown command name=${cmd}.`; }); +// === The above is the commander configuration === +// They don't start any code execution yet until cli.parse() is called below + /** * @param {string | undefined} command */ @@ -237,12 +240,29 @@ function isInternalCommand(command) { ); } -if (!isInternalCommand(process.argv.slice(2)[0])) { - await externalCommand(cli); -} +// process.argv always looks like this: +// [ +// '/path/to/node', +// '/path/to/docusaurus.mjs', +// '', +// ...subcommandArgs +// ] -if (!process.argv.slice(2).length) { +// There is no subcommand +// TODO: can we use commander to handle this case? +if (process.argv.length < 3 || process.argv[2]?.startsWith('--')) { cli.outputHelp(); + process.exit(1); +} + +// There is an unrecognized subcommand +// Let plugins extend the CLI before parsing +if (!isInternalCommand(process.argv[2])) { + // TODO: in this step, we must assume default site structure because there's + // no way to know the siteDir/config yet. Maybe the root cli should be + // responsible for parsing these arguments? + // https://github.com/facebook/docusaurus/issues/8903 + await externalCommand(cli); } cli.parse(process.argv);