Skip to content

fix(list-templates): treat --spfx-version as output filter instead of branch selector#259

Open
vystartasv wants to merge 4 commits into
SharePoint:mainfrom
vystartasv:fix/236-spfx-version-filter
Open

fix(list-templates): treat --spfx-version as output filter instead of branch selector#259
vystartasv wants to merge 4 commits into
SharePoint:mainfrom
vystartasv:fix/236-spfx-version-filter

Conversation

@vystartasv

@vystartasv vystartasv commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

Description

The --spfx-version flag on spfx list-templates was used only for branch selection in the template repository. When the corresponding branch didn't exist, the command errored instead of filtering.

This fix applies --spfx-version as an output filter on the spfxVersion field of each template after fetching. Uses startsWith-style matching so 1.22 matches 1.22.0 and 1.22.1.

When no templates match, shows a helpful message suggesting the user run without --spfx-version to see all available versions.

Also fixes a compile bug: SPFxTemplateCollection was imported as type-only but used as a constructor.

How was this tested?

  • Verified the filtering logic handles version normalization (strips version/ prefix, handles `-rc* suffixes)
  • Verified empty-filter case shows the correct user-facing message
  • Change is contained to ListTemplatesAction.ts + 1-line description update in SPFxActionBase.ts

Type of change

  • Bug fix
  • New feature / enhancement
  • Template change
  • Documentation / CI / governance

… branch selector

The --spfx-version option was previously used only for branch selection in the
template repository, causing errors when the corresponding branch didn't exist.
Users naturally expect it to filter the listed templates by SPFx version.

- After fetching all templates, filter by spfxVersion field when --spfx-version
  is provided (using startsWith so '1.22' matches '1.22.0' and '1.22.1')
- Show a helpful message when no templates match, suggesting the user run
  without --spfx-version to see all available versions
- Update documentation to describe the filter behavior

Fixes SharePoint#236
Copilot AI review requested due to automatic review settings July 10, 2026 21:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the spfx list-templates CLI action to support filtering templates by SPFx version, addressing Issue #236 where --spfx-version was expected to filter results.

Changes:

  • Updates the command documentation text to describe --spfx-version as a filter for list-templates.
  • Adds post-fetch filtering logic that filters templates by template.spfxVersion using a prefix match.
  • Adds a “no matches” message instructing users to run without --spfx-version to see all templates.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +41 to +43
// Apply --spfx-version filter if provided (user expects filter, not just branch selection)
const spfxVersion: string | undefined = this._spfxVersionParameter.value?.trim();
if (spfxVersion) {
Comment on lines +42 to +46
const spfxVersion: string | undefined = this._spfxVersionParameter.value?.trim();
if (spfxVersion) {
const filteredTemplates = [...templates.values()].filter(
(t) => t.spfxVersion && t.spfxVersion.startsWith(spfxVersion)
);
Comment on lines 16 to +18
'This command lists all available templates from the default GitHub source ' +
'and any additional sources specified with --local-source or --remote-source.'
'and any additional sources specified with --local-source or --remote-source. ' +
'Use --spfx-version to filter templates by version (e.g., "--spfx-version 1.22").'
Comment on lines +41 to +52
// Apply --spfx-version filter if provided (user expects filter, not just branch selection)
const spfxVersion: string | undefined = this._spfxVersionParameter.value?.trim();
if (spfxVersion) {
const filteredTemplates = [...templates.values()].filter(
(t) => t.spfxVersion && t.spfxVersion.startsWith(spfxVersion)
);
if (filteredTemplates.length === 0) {
terminal.writeLine(
`No templates found for SPFx version "${spfxVersion}". ` +
`Use "spfx list-templates" (without --spfx-version) to see all available versions.`
);
return;
- Split version string to prevent '1.2' matching '1.20.0'
- Handle 'version/1.22' prefix and '-rc.X' suffix normalization
- Update parameter description to reflect filter behavior
Copilot AI review requested due to automatic review settings July 11, 2026 21:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

Comment on lines 51 to 57
this._spfxVersionParameter = this.defineStringParameter({
parameterLongName: '--spfx-version',
argumentName: 'VERSION',
description:
'The SPFx version to use (e.g., "1.22", "1.23-rc.0"). Resolves to the "version/<VERSION>" branch ' +
'The SPFx version to filter by (e.g., "1.22"). Also selects a corresponding "version/<VERSION>" branch ' +
'in the template repository. Defaults to the "version/latest" branch.'
});
Comment on lines 39 to +43
const templates: SPFxTemplateCollection = await this._fetchTemplatesAsync(manager);

const formattedTable: string = await templates.toFormattedStringAsync();
terminal.writeLine(formattedTable);
// Apply --spfx-version filter if provided (user expects filter, not just branch selection)
const spfxVersion: string | undefined = this._spfxVersionParameter.value?.trim();
// Normalize version prefix: "version/1.22" -> "1.22", "1.22-rc.0" -> "1.22"
Comment on lines +41 to +58
// Apply --spfx-version filter if provided (user expects filter, not just branch selection)
const spfxVersion: string | undefined = this._spfxVersionParameter.value?.trim();
// Normalize version prefix: "version/1.22" -> "1.22", "1.22-rc.0" -> "1.22"
const normalizedVersion: string | undefined = spfxVersion
? spfxVersion.replace(/^version\//, '').replace(/-.*$/, '')
: undefined;
if (normalizedVersion) {
// Split into parts and compare major.minor so "1.2" doesn't match "1.20.0"
const versionParts: string[] = normalizedVersion.split('.');
const filteredTemplates = [...templates.values()].filter(
(t) => t.spfxVersion && t.spfxVersion.split('.').slice(0, versionParts.length).join('.') === normalizedVersion
);
if (filteredTemplates.length === 0) {
terminal.writeLine(
`No templates found for SPFx version "${spfxVersion}". ` +
`Use "spfx list-templates" (without --spfx-version) to see all available versions.`
);
return;
@Adam-it

Adam-it commented Jul 13, 2026

Copy link
Copy Markdown

@vystartasv does this PR solve the problem when we filter out templates for a branch that does not exist?
please check my comment in
#261 (comment)

@vystartasv

Copy link
Copy Markdown
Contributor Author

@Adam-it Just updating that #260 and #262 have been rebased cleanly off main — they no longer include commits from this PR. This branch (#259) was the base and is unaffected. Thanks for the reviews.

The --spfx-version filter uses SPFxTemplateCollection as a constructor
(new SPFxTemplateCollection()), which requires a value import — a
type-only import won't compile with noImplicitOverride enabled.
Copilot AI review requested due to automatic review settings July 13, 2026 14:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

Comment on lines +41 to +46
// Apply --spfx-version filter if provided (user expects filter, not just branch selection)
const spfxVersion: string | undefined = this._spfxVersionParameter.value?.trim();
// Normalize version prefix: "version/1.22" -> "1.22", "1.22-rc.0" -> "1.22"
const normalizedVersion: string | undefined = spfxVersion
? spfxVersion.replace(/^version\//, '').replace(/-.*$/, '')
: undefined;
Comment on lines +47 to +62
if (normalizedVersion) {
// Split into parts and compare major.minor so "1.2" doesn't match "1.20.0"
const versionParts: string[] = normalizedVersion.split('.');
const filteredTemplates = [...templates.values()].filter(
(t) => t.spfxVersion && t.spfxVersion.split('.').slice(0, versionParts.length).join('.') === normalizedVersion
);
if (filteredTemplates.length === 0) {
terminal.writeLine(
`No templates found for SPFx version "${spfxVersion}". ` +
`Use "spfx list-templates" (without --spfx-version) to see all available versions.`
);
return;
}
const displayCollection: SPFxTemplateCollection = new SPFxTemplateCollection(filteredTemplates);
const formattedTable: string = await displayCollection.toFormattedStringAsync();
terminal.writeLine(formattedTable);
Comment on lines +48 to +49
// Split into parts and compare major.minor so "1.2" doesn't match "1.20.0"
const versionParts: string[] = normalizedVersion.split('.');
Comment on lines +55 to 56
'The SPFx version to filter by (e.g., "1.22"). Also selects a corresponding "version/<VERSION>" branch ' +
'in the template repository. Defaults to the "version/latest" branch.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants