Implement spfx list-templates command#175
Conversation
Adds a new `list-templates` command that fetches and displays available templates from configured sources. Unlike `create`, the source model is additive — the default GitHub source is always included alongside any `--local-source` or `--remote-source` flags. Also extracts shared GitHub URL utilities (DEFAULT_GITHUB_REPO, SPFX_TEMPLATE_REPO_URL_ENV_VAR_NAME, parseGitHubUrlAndRef) into src/utilities/github.ts so both actions can reuse them. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Adds a new list-templates command to @microsoft/spfx-cli for enumerating available SPFx templates from the default GitHub repo plus any user-specified local/remote sources, and refactors shared GitHub URL parsing into a utility.
Changes:
- Introduces
spfx list-templatesaction with flags for default repo URL, SPFx branch/ref, and additive local/remote template sources. - Extracts GitHub repo defaults and
/tree/<ref>parsing intosrc/utilities/github.tsand reuses it fromCreateAction. - Adds Jest coverage + snapshots for the new command and updates CLI help snapshots and docs.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| common/changes/@microsoft/spfx-cli/list-templates_2026-03-24.json | Rush change file for the feature work. |
| apps/spfx-cli/src/utilities/github.ts | New shared GitHub constants + URL/ref parsing helper. |
| apps/spfx-cli/src/cli/actions/ListTemplatesAction.ts | Implements the new list-templates command behavior and output. |
| apps/spfx-cli/src/cli/actions/tests/ListTemplatesAction.test.ts | Unit tests for additive source selection, URL/ref parsing, and error handling. |
| apps/spfx-cli/src/cli/actions/tests/snapshots/ListTemplatesAction.test.ts.snap | Snapshot outputs for list-templates tests. |
| apps/spfx-cli/src/cli/actions/CreateAction.ts | Refactors to use shared GitHub utilities (no intended behavior change). |
| apps/spfx-cli/src/cli/actions/tests/CreateAction.test.ts | Updates tests to import shared env var name constant. |
| apps/spfx-cli/src/cli/SPFxCommandLineParser.ts | Registers the new action with the CLI parser. |
| apps/spfx-cli/src/cli/test/snapshots/CommandLineHelp.test.ts.snap | Updates help snapshots to include list-templates. |
| apps/spfx-cli/README.md | Documents spfx list-templates usage and flags for the published package. |
| CLAUDE.md | Adds guidance for updating Jest snapshots with Heft. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Fix toolFilename: 'spfx-cli' → 'spfx' to match the published binary name - Document slash-branch limitation in parseGitHubUrlAndRef (cannot express feature/foo via /tree/ URL; use --spfx-version instead) - Use `terminal` alias consistently in ListTemplatesAction (not this._terminal) - Remove unused SPFX_TEMPLATE_REPO_URL_ENV_VAR_NAME re-export from CreateAction - Update help snapshots for toolFilename change Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Manual Test ResultsTested on commit e9788bf, Node.js 22.16.0, Linux (codespace). Test plan
Results✅ Test 1 — Global help ✅ Test 2 — Command help ✅ Test 3 — Single local source ✅ Test 4 — Additive sources (two ✅ Test 5 — ✅ Test 6 — ✅ Test 7 — Live GitHub fetch ✅ Test 8 — Error handling (nonexistent path) Notes
Issues found during testing
|
apps/spfx-cli/src/cli/actions/tests/__snapshots__/ListTemplatesAction.test.ts.snap
Outdated
Show resolved
Hide resolved
- Add addDefaultGitHubSource() helper to utilities/github.ts, eliminating the duplicated GitHub source init block between CreateAction and ListTemplatesAction - Add TEMPLATE_URL_DESCRIPTION and SPFX_VERSION_DESCRIPTION constants so both actions share identical flag descriptions from one source of truth - Add utilities/tests/github.test.ts with 12 direct unit tests for parseGitHubUrlAndRef (plain URLs, .git stripping, /tree/ extraction, GHE, etc.) - Remove now-unused PublicGitHubRepositorySource import from CreateAction Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Replace the placeholder '[Mocked SPFxTemplateCollection]' toString with output that matches the real SPFxTemplateCollection/SPFxTemplate format, so snapshot diffs are meaningful when template display changes. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Replace per-action inline defineStringParameter({...}) objects with
TEMPLATE_URL_PARAMETER_DEFINITION and SPFX_VERSION_PARAMETER_DEFINITION
constants in utilities/github.ts. Both CreateAction and ListTemplatesAction
now call defineStringParameter(TEMPLATE_URL_PARAMETER_DEFINITION) directly,
ensuring the flag names, argument names, descriptions, and env var binding
stay in sync automatically.
Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Main changed --spfx-version to resolve "1.22" to the "version/1.22" branch. Updated addDefaultGitHubSource() to apply the same mapping so both CreateAction and ListTemplatesAction get consistent behavior. Also updated SPFX_VERSION_PARAMETER_DEFINITION description and ListTemplatesAction tests to expect the new version/ prefixed branch names. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Extract a shared base class `SPFxAction` for `CreateAction` and `ListTemplatesAction` that owns the common `--template-url` and `--spfx-version` parameters and the GitHub source registration logic. - Move `addDefaultGitHubSource` from `utilities/github.ts` into `SPFxAction._addGitHubTemplateSource()` (protected method) - Remove exported `TEMPLATE_URL_PARAMETER_DEFINITION` and `SPFX_VERSION_PARAMETER_DEFINITION` constants (now defined in the base class) - Add `protected override` to `onExecuteAsync` in both actions - Use `parseGitHubUrlAndRef.name` instead of string literal in tests - Update help snapshots for new parameter order Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
The list-templates command (merged via PR SharePoint#175) had the same misleading --local-source suggestion. Since list-templates uses an additive model where GitHub is always included, the hint is removed entirely — the Details suffix already identifies which source failed. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Main merged PR SharePoint#175 which extracted parseGitHubUrlAndRef to utilities/github.ts and introduced SPFxActionBase. Re-added PublicGitHubRepositorySource import and parseGitHubUrlAndRef import needed by the --remote-source loop. Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
Description
Implements issue #89: adds a
spfx list-templatescommand that fetches and displays available templates from configured sources.Key design decision: unlike
create(which uses an exclusive local-vs-github model),list-templatesuses an additive model — the default GitHub source is always included, and--local-source/--remote-sourceadd on top of it.Changes
New files:
src/utilities/github.ts— sharedDEFAULT_GITHUB_REPO,SPFX_TEMPLATE_REPO_URL_ENV_VAR_NAME, andparseGitHubUrlAndRef(), extracted fromCreateActionso both actions can reuse themsrc/cli/actions/ListTemplatesAction.ts— new action with--template-url,--spfx-version,--local-source,--remote-sourcesrc/cli/actions/tests/ListTemplatesAction.test.ts— 18 tests (written TDD before the implementation)Modified files:
CreateAction.ts— now imports shared values fromutilities/github(no behavior change)SPFxCommandLineParser.ts— registersListTemplatesActionCommandLineHelp.test.ts.snap— updated to includelist-templatesin help outputCLAUDE.md— documents theheft test --clean -usnapshot update commandHow was this tested
rushx build— 54/54 tests pass (18 newListTemplatesActiontests, 35 existingCreateActiontests, 1CommandLineHelpsnapshot test)Type of change
Closes #89
🤖 Generated with Claude Code