Skip to content

Commit

Permalink
fix(cli): Fix bad docusaurus CLI behavior on for --version, -V, --hel…
Browse files Browse the repository at this point in the history
…p, -h (#10368)

Co-authored-by: sebastien <[email protected]>
  • Loading branch information
ashiq-firoz and slorber authored Aug 8, 2024
1 parent 7be1fea commit 087a329
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions packages/docusaurus/bin/docusaurus.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ cli

cli.arguments('<command>').action((cmd) => {
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 ===
Expand All @@ -247,24 +248,24 @@ function isInternalCommand(command) {
);
}

// process.argv always looks like this:
// [
// '/path/to/node',
// '/path/to/docusaurus.mjs',
// '<subcommand>',
// ...subcommandArgs
// ]
/**
* @param {string | undefined} command
*/
function isExternalCommand(command) {
return !!(command && !isInternalCommand(command) && !command.startsWith('-'));
}

// There is no subcommand
// TODO: can we use commander to handle this case?
if (process.argv.length < 3 || process.argv[2]?.startsWith('--')) {
// No command? We print the help message because Commander doesn't
// Note argv looks like this: ['../node','../docusaurus.mjs','<command>',...rest]
if (process.argv.length < 3) {
cli.outputHelp();
logger.error`Please provide a Docusaurus CLI command.`;
process.exit(1);
}

// There is an unrecognized subcommand
// 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
// no way to know the siteDir/config yet. Maybe the root cli should be
// responsible for parsing these arguments?
Expand Down

0 comments on commit 087a329

Please sign in to comment.