Skip to content

Commit 087a329

Browse files
ashiq-firozslorber
andauthored
fix(cli): Fix bad docusaurus CLI behavior on for --version, -V, --help, -h (#10368)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
1 parent 7be1fea commit 087a329

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

packages/docusaurus/bin/docusaurus.mjs

+13-12
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ cli
222222

223223
cli.arguments('<command>').action((cmd) => {
224224
cli.outputHelp();
225-
logger.error` Unknown command name=${cmd}.`;
225+
logger.error`Unknown Docusaurus CLI command name=${cmd}.`;
226+
process.exit(1);
226227
});
227228

228229
// === The above is the commander configuration ===
@@ -247,24 +248,24 @@ function isInternalCommand(command) {
247248
);
248249
}
249250

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

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

265266
// There is an unrecognized subcommand
266267
// Let plugins extend the CLI before parsing
267-
if (!isInternalCommand(process.argv[2])) {
268+
if (isExternalCommand(process.argv[2])) {
268269
// TODO: in this step, we must assume default site structure because there's
269270
// no way to know the siteDir/config yet. Maybe the root cli should be
270271
// responsible for parsing these arguments?

0 commit comments

Comments
 (0)