-
Notifications
You must be signed in to change notification settings - Fork 14
Adds JSON output mode for the spfx list-templates command. Closes #235
#253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -165,6 +165,22 @@ export interface ISPFxTemplateJson { | |
| version: string; | ||
| } | ||
|
|
||
| // @public | ||
| export interface ITemplateJsonOutputEntry { | ||
| // (undocumented) | ||
| category: string; | ||
| // (undocumented) | ||
| description: string | null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We generally use |
||
| // (undocumented) | ||
| fileCount: number; | ||
| // (undocumented) | ||
| name: string; | ||
| // (undocumented) | ||
| spfxVersion: string; | ||
| // (undocumented) | ||
| version: string; | ||
| } | ||
|
|
||
| // @public | ||
| export interface ITemplateOutputEntry { | ||
| readonly contents: string | Buffer; | ||
|
|
@@ -288,6 +304,7 @@ export type SPFxTemplateCategory = (typeof SPFX_TEMPLATE_CATEGORIES)[number]; | |
| export class SPFxTemplateCollection extends Map<string, SPFxTemplate> { | ||
| constructor(templates: SPFxTemplate[]); | ||
| toFormattedStringAsync(): Promise<string>; | ||
| toJsonString(): string; | ||
| } | ||
|
|
||
| // @public | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,23 @@ | |
|
|
||
| import type { SPFxTemplate } from '../templating'; | ||
|
|
||
| /** | ||
| * @public | ||
| * Represents a single template entry in the JSON output produced by | ||
| * {@link SPFxTemplateCollection.toJsonString}. | ||
| */ | ||
| export interface ITemplateJsonOutputEntry { | ||
| name: string; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doc comments for these things. It's not obvious what |
||
| category: string; | ||
| // `null` (not `undefined`) is intentional: JSON.stringify drops `undefined` fields | ||
| // but preserves `null`, ensuring the field is always present in the output. | ||
| // eslint-disable-next-line @rushstack/no-new-null | ||
|
Comment on lines
+14
to
+16
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also just make this required. |
||
| description: string | null; | ||
| version: string; | ||
| spfxVersion: string; | ||
| fileCount: number; | ||
| } | ||
|
|
||
| /** | ||
| * @public | ||
| * Represents a collection of SharePoint Framework (SPFx) templates. | ||
|
|
@@ -17,6 +34,31 @@ export class SPFxTemplateCollection extends Map<string, SPFxTemplate> { | |
| super(templates.map((template) => [template.name, template])); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a JSON string representation of the collection as an array of template objects. | ||
| * Each object includes `name`, `category`, `description`, `version`, `spfxVersion`, and `fileCount`. | ||
| * | ||
| * @remarks | ||
| * Unlike {@link SPFxTemplateCollection.toFormattedStringAsync}, this method is synchronous | ||
| * because it has no external dependencies. | ||
| * | ||
| * @returns A pretty-printed JSON string | ||
| */ | ||
| public toJsonString(): string { | ||
| const items: ITemplateJsonOutputEntry[] = []; | ||
| for (const template of this.values()) { | ||
| items.push({ | ||
| name: template.name, | ||
| category: template.category, | ||
| description: template.description ?? null, | ||
| version: template.version, | ||
| spfxVersion: template.spfxVersion, | ||
| fileCount: template.fileCount | ||
| }); | ||
| } | ||
| return JSON.stringify(items, undefined, 2); | ||
| } | ||
|
|
||
| /** | ||
| * Returns a formatted table string representation of the collection. | ||
| * Uses cli-table3, which is loaded asynchronously to reduce startup cost. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`SPFxTemplateCollection toJsonString should serialize multiple templates 1`] = ` | ||
| "[ | ||
| { | ||
| \\"name\\": \\"WebPart\\", | ||
| \\"category\\": \\"webpart\\", | ||
| \\"description\\": \\"A web part template\\", | ||
| \\"version\\": \\"1.0.0\\", | ||
| \\"spfxVersion\\": \\"1.18.0\\", | ||
| \\"fileCount\\": 1 | ||
| }, | ||
| { | ||
| \\"name\\": \\"Extension\\", | ||
| \\"category\\": \\"extension\\", | ||
| \\"description\\": null, | ||
| \\"version\\": \\"2.0.0\\", | ||
| \\"spfxVersion\\": \\"1.18.0\\", | ||
| \\"fileCount\\": 0 | ||
| } | ||
| ]" | ||
| `; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,16 @@ | |
| // See LICENSE in the project root for license information. | ||
|
|
||
| import type { Terminal } from '@rushstack/terminal'; | ||
| import type { IRequiredCommandLineChoiceParameter } from '@rushstack/ts-command-line'; | ||
| import { type SPFxTemplateCollection, SPFxTemplateRepositoryManager } from '@microsoft/spfx-template-api'; | ||
|
|
||
| import { SPFxActionBase } from './SPFxActionBase'; | ||
|
|
||
| export type OutputFormat = 'json' | 'text'; | ||
|
|
||
| export class ListTemplatesAction extends SPFxActionBase { | ||
| private readonly _outputParameter: IRequiredCommandLineChoiceParameter<OutputFormat>; | ||
|
|
||
| public constructor(terminal: Terminal) { | ||
| super( | ||
| { | ||
|
|
@@ -18,10 +23,21 @@ export class ListTemplatesAction extends SPFxActionBase { | |
| }, | ||
| terminal | ||
| ); | ||
|
|
||
| this._outputParameter = this.defineChoiceParameter({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider making this a |
||
| parameterLongName: '--output', | ||
| parameterShortName: '-o', | ||
| description: | ||
| 'Output format. "json" writes machine-readable JSON to stdout (informational ' + | ||
| 'messages go to stderr). "text" writes a human-readable table.', | ||
|
Comment on lines
+31
to
+32
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Generally avoid printing non-erroneous data to stderr. In JSON mode, why not just not output anything that can't be JSON-parsed? |
||
| alternatives: ['json', 'text'], | ||
| defaultValue: 'json' | ||
| }); | ||
| } | ||
|
|
||
| protected override async onExecuteAsync(): Promise<void> { | ||
| const terminal: Terminal = this._terminal; | ||
| const isJson: boolean = this._outputParameter.value === 'json'; | ||
|
|
||
| try { | ||
| const manager: SPFxTemplateRepositoryManager = new SPFxTemplateRepositoryManager(); | ||
|
|
@@ -37,8 +53,12 @@ export class ListTemplatesAction extends SPFxActionBase { | |
|
|
||
| const templates: SPFxTemplateCollection = await this._fetchTemplatesAsync(manager); | ||
|
|
||
| const formattedTable: string = await templates.toFormattedStringAsync(); | ||
| terminal.writeLine(formattedTable); | ||
| if (isJson) { | ||
| terminal.write(templates.toJsonString() + '\n'); | ||
| } else { | ||
| const formattedTable: string = await templates.toFormattedStringAsync(); | ||
| terminal.writeLine(formattedTable); | ||
| } | ||
| } catch (error: unknown) { | ||
| const message: string = error instanceof Error ? error.message : String(error); | ||
| terminal.writeErrorLine(`Error listing templates: ${message}`); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be a type union?