fix(cli): Fix bad docusaurus CLI behavior on for --version, -V, --help, -h (#10368)

Co-authored-by: sebastien <lorber.sebastien@gmail.com>
This commit is contained in:
Ashiq Firoz 2024-08-08 19:16:51 +05:30 committed by GitHub
parent 7be1feaa0a
commit 087a32971f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -222,7 +222,8 @@ cli
cli.arguments('<command>').action((cmd) => { cli.arguments('<command>').action((cmd) => {
cli.outputHelp(); cli.outputHelp();
logger.error` Unknown command name=${cmd}.`; logger.error`Unknown Docusaurus CLI command name=${cmd}.`;
process.exit(1);
}); });
// === The above is the commander configuration === // === The above is the commander configuration ===
@ -247,24 +248,24 @@ function isInternalCommand(command) {
); );
} }
// process.argv always looks like this: /**
// [ * @param {string | undefined} command
// '/path/to/node', */
// '/path/to/docusaurus.mjs', function isExternalCommand(command) {
// '<subcommand>', return !!(command && !isInternalCommand(command) && !command.startsWith('-'));
// ...subcommandArgs }
// ]
// There is no subcommand // No command? We print the help message because Commander doesn't
// TODO: can we use commander to handle this case? // Note argv looks like this: ['../node','../docusaurus.mjs','<command>',...rest]
if (process.argv.length < 3 || process.argv[2]?.startsWith('--')) { if (process.argv.length < 3) {
cli.outputHelp(); cli.outputHelp();
logger.error`Please provide a Docusaurus CLI command.`;
process.exit(1); process.exit(1);
} }
// There is an unrecognized subcommand // There is an unrecognized subcommand
// Let plugins extend the CLI before parsing // Let plugins extend the CLI before parsing
if (!isInternalCommand(process.argv[2])) { if (isExternalCommand(process.argv[2])) {
// TODO: in this step, we must assume default site structure because there's // 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 // no way to know the siteDir/config yet. Maybe the root cli should be
// responsible for parsing these arguments? // responsible for parsing these arguments?