diff --git a/apps/spfx-cli/src/cli/SPFxCommandLineParser.ts b/apps/spfx-cli/src/cli/SPFxCommandLineParser.ts index 7e8f6c02..b3edfc9d 100644 --- a/apps/spfx-cli/src/cli/SPFxCommandLineParser.ts +++ b/apps/spfx-cli/src/cli/SPFxCommandLineParser.ts @@ -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 { + 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); + } } diff --git a/apps/spfx-cli/src/cli/test/CommandLineHelp.test.ts b/apps/spfx-cli/src/cli/test/CommandLineHelp.test.ts index 63963b69..b871e54b 100644 --- a/apps/spfx-cli/src/cli/test/CommandLineHelp.test.ts +++ b/apps/spfx-cli/src/cli/test/CommandLineHelp.test.ts @@ -4,6 +4,7 @@ import { AnsiEscape, Terminal, StringBufferTerminalProvider } from '@rushstack/terminal'; import { SPFxCommandLineParser } from '../SPFxCommandLineParser'; +import packageJson from '../../../package.json'; describe('CommandLineHelp', () => { beforeEach(() => { @@ -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]` + ]); + }); }); diff --git a/apps/spfx-cli/src/cli/test/__snapshots__/CommandLineHelp.test.ts.snap b/apps/spfx-cli/src/cli/test/__snapshots__/CommandLineHelp.test.ts.snap index b7be4dfb..cb37c80d 100644 --- a/apps/spfx-cli/src/cli/test/__snapshots__/CommandLineHelp.test.ts.snap +++ b/apps/spfx-cli/src/cli/test/__snapshots__/CommandLineHelp.test.ts.snap @@ -60,7 +60,7 @@ Optional arguments: `; exports[`CommandLineHelp prints the help: global help 1`] = ` -"usage: spfx [-h] ... +"usage: spfx [-h] [-v] ... CLI for managing SharePoint Framework (SPFx) projects @@ -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 -h[normal] "