Skip to content

Commit

Permalink
Enhances cli warning when non-existing command is called. Closes pnp#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MathijsVerbeeck authored and Jwaegebaert committed Apr 22, 2024
1 parent 28ff16f commit 3b7cd6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/cli/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,18 @@ describe('cli', () => {
assert(cliLogStub.calledWith(`CLI for Microsoft 365 v${packageJSON.version}`));
});

it('shows message that command cannot be found when an unknown command is entered', async () => {
const commandName = 'unknown';
await cli.execute([commandName]);
assert(cliErrorStub.calledWith(chalk.red(`Command '${cli.currentCommandName}' was not found. Below you can find the commands and command groups you can use. For detailed information on a command group, use 'm365 [command group] --help'.`)));
});

it('does not show message that command cannot be found when a uncompleted command is entered', async () => {
const commandName = 'cli mock';
await cli.execute([commandName]);
assert(cliErrorStub.notCalled);
});

it('exits with 0 code when no command specified', async () => {
await cli.execute([]);
assert(processExitStub.calledWith(0));
Expand Down
11 changes: 8 additions & 3 deletions src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { CommandOptionInfo } from './CommandOptionInfo.js';
import { Logger } from './Logger.js';
import { SelectionConfig, ConfirmationConfig, prompt } from '../utils/prompt.js';
import { timings } from './timings.js';
import chalk from 'chalk';
const require = createRequire(import.meta.url);

const __dirname = fileURLToPath(new URL('.', import.meta.url));
Expand Down Expand Up @@ -120,7 +121,7 @@ async function execute(rawArgs: string[]): Promise<void> {
parsedArgs.h ||
parsedArgs.help) {
if (parsedArgs.output !== 'none') {
printHelp(await getHelpMode(parsedArgs));
await printHelp(await getHelpMode(parsedArgs));
}
return;
}
Expand Down Expand Up @@ -652,14 +653,18 @@ function getFirstNonUndefinedArrayItem(arr: any[]): any {
return undefined;
}

function printHelp(helpMode: string, exitCode: number = 0): void {
async function printHelp(helpMode: string, exitCode: number = 0): Promise<void> {
const properties: any = {};

if (cli.commandToExecute) {
properties.command = cli.commandToExecute.name;
printCommandHelp(helpMode);
}
else {
if (cli.currentCommandName && !cli.commands.some(command => command.name.startsWith(cli.currentCommandName!))) {
await cli.error(chalk.red(`Command '${cli.currentCommandName}' was not found. Below you can find the commands and command groups you can use. For detailed information on a command group, use 'm365 [command group] --help'.`));
}

cli.log();
cli.log(`CLI for Microsoft 365 v${app.packageJson().version}`);
cli.log(`${app.packageJson().description} `);
Expand Down Expand Up @@ -878,7 +883,7 @@ async function closeWithError(error: any, args: CommandArgs, showHelpIfEnabled:

if (showHelpIfEnabled &&
await cli.getSettingWithDefaultValue<boolean>(settingsNames.showHelpOnFailure, showHelpIfEnabled)) {
printHelp(await getHelpMode(args.options), exitCode);
await printHelp(await getHelpMode(args.options), exitCode);
}
else {
process.exit(exitCode);
Expand Down

0 comments on commit 3b7cd6f

Please sign in to comment.