Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions apps/spfx-cli/src/cli/SPFxCommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,39 @@ import type { Terminal } from '@rushstack/terminal';

import { CreateAction } from './actions/CreateAction';
import { ListTemplatesAction } from './actions/ListTemplatesAction';
import packageJson from '../../package.json';

const CLI_VERSION: string = packageJson.version;

export class SPFxCommandLineParser extends CommandLineParser {
private readonly _terminal: Terminal;

public constructor(terminal: Terminal) {
super({
toolFilename: 'spfx',
toolDescription: 'CLI for managing SharePoint Framework (SPFx) projects'
});

this._terminal = terminal;

this.defineFlagParameter({
parameterLongName: '--version',
parameterShortName: '-v',
description: 'Show the CLI version and exit.'
});

this.addAction(new CreateAction(terminal));
this.addAction(new ListTemplatesAction(terminal));
}

public override async executeWithoutErrorHandlingAsync(args?: string[]): Promise<void> {
const effectiveArgs: string[] = args ?? process.argv.slice(2);

if (effectiveArgs.length === 1 && (effectiveArgs[0] === '--version' || effectiveArgs[0] === '-v')) {
this._terminal.writeLine(CLI_VERSION);
return;
}

await super.executeWithoutErrorHandlingAsync(args);
}
}
12 changes: 12 additions & 0 deletions apps/spfx-cli/src/cli/test/CommandLineHelp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { AnsiEscape, Terminal, StringBufferTerminalProvider } from '@rushstack/terminal';

import { SPFxCommandLineParser } from '../SPFxCommandLineParser';
import packageJson from '../../../package.json';

describe('CommandLineHelp', () => {
beforeEach(() => {
Expand Down Expand Up @@ -37,4 +38,15 @@ describe('CommandLineHelp', () => {
expect(actionHelpText).toMatchSnapshot(action.actionName);
}
});

it.each(['--version', '-v'])('prints the CLI version for %s', async (versionFlag) => {
const terminalProvider: StringBufferTerminalProvider = new StringBufferTerminalProvider();
const parser: SPFxCommandLineParser = new SPFxCommandLineParser(new Terminal(terminalProvider));

await parser.executeWithoutErrorHandlingAsync([versionFlag]);

expect(terminalProvider.getAllOutputAsChunks({ asLines: true })).toEqual([
`[ log] ${packageJson.version}[n]`
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Optional arguments:
`;

exports[`CommandLineHelp prints the help: global help 1`] = `
"usage: spfx [-h] <command> ...
"usage: spfx [-h] [-v] <command> ...

CLI for managing SharePoint Framework (SPFx) projects

Expand All @@ -72,6 +72,7 @@ Positional arguments:

Optional arguments:
-h, --help Show this help message and exit.
-v, --version Show the CLI version and exit.

[bold]For detailed help about a specific command, use: spfx <command> -h[normal]
"
Expand Down