-
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 |
|---|---|---|
| @@ -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\\": \\"\\", | ||
| \\"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
Author
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. I would strongly recommend adding a universal |
||
| 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?
Author
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. but if we avoid any other output (like verbose or debug or other) in the stderr then for |
||
| 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}`); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.