Adds JSON output mode for the spfx list-templates command. Closes #235#253
Adds JSON output mode for the spfx list-templates command. Closes #235#253Adam-it wants to merge 2 commits into
spfx list-templates command. Closes #235#253Conversation
There was a problem hiding this comment.
Pull request overview
Adds a JSON output mode (now the default) for spfx list-templates, and refactors command output so structured results go to stdout while informational output is routed to stderr.
Changes:
- Added
--output/-o {json,text}tolist-templates, defaulting tojson - Implemented JSON serialization for template collections (
toJsonString()) and updated CLI action to write JSON to stdout - Introduced
StderrTerminalProviderand added/updated unit tests + docs/snapshots accordingly
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| common/changes/@microsoft/spfx-cli/json-output-mode_2026-04-25.json | Adds a change record for the CLI package. |
| apps/spfx-cli/src/utilities/StderrTerminalProvider.ts | New terminal provider intended to route CLI messages to stderr for JSON output scenarios. |
| apps/spfx-cli/src/utilities/tests/StderrTerminalProvider.test.ts | Unit tests validating stderr routing behavior. |
| apps/spfx-cli/src/cli/actions/ListTemplatesAction.ts | Adds --output option; writes JSON to stdout by default and table output for text mode. |
| apps/spfx-cli/src/cli/actions/SPFxActionBase.ts | Makes _terminal mutable so actions can temporarily swap terminal behavior. |
| apps/spfx-cli/src/cli/actions/tests/ListTemplatesAction.test.ts | Updates existing tests to force text output; adds JSON-output tests using stdout spying. |
| apps/spfx-cli/src/cli/test/snapshots/CommandLineHelp.test.ts.snap | Updates help snapshot to include the new --output parameter and its description. |
| apps/spfx-cli/README.md | Documents --output and provides examples for JSON (default) and text. |
| api/spfx-template-api/src/repositories/SPFxTemplateCollection.ts | Adds toJsonString() for structured template list serialization. |
| api/spfx-template-api/src/repositories/test/SPFxTemplateCollection.test.ts | Adds unit tests for toJsonString(). |
| api/spfx-template-api/src/repositories/test/snapshots/SPFxTemplateCollection.test.ts.snap | Snapshot for JSON serialization formatting. |
| api/spfx-template-api/etc/spfx-template-api.api.md | API report update to include toJsonString(). |
| api/spfx-template-api/README.md | Updates docs to mention JSON/table output helpers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
26860dd to
65b297d
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
apps/spfx-cli/README.md:81
- The
spfx list-templatesdocs list the new--outputoption, but the command also now supports--verbose(and help output advertises it). The README’s optional flags table should include--verboseand clarify that verbose/info messages are written to stderr (especially important with JSON output).
### Optional flags
| Flag | Default | Description |
|------|---------|-------------|
| `-o`, `--output {json,text}` | `json` | Output format. `json` writes machine-readable JSON to stdout (informational messages go to stderr). `text` writes a human-readable table |
| `--spfx-version VERSION` | `version/latest` branch | Branch/tag in the default template repo to use (e.g. `1.22`, `1.23-rc.0`) |
| `--template-url URL` | `https://github.com/SharePoint/spfx` | Custom GitHub template repository (default source) |
| `--local-source PATH` | — | Path to a local template folder to include (repeatable) |
| `--remote-source URL` | — | Additional public GitHub repo to include as a template source (repeatable) |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
9e88690 to
77d8b1c
Compare
| Array [ | ||
| "[ log] Using GitHub template source: https://github.com/SharePoint/spfx[n]", | ||
| "[ log] Adding local template source: /path/to/templates[n]", | ||
| "[verbose] Using GitHub template source: https://github.com/SharePoint/spfx[n]", |
There was a problem hiding this comment.
Those are related. Please check the changes done in apps/spfx-cli/src/cli/actions/SPFxActionBase.ts. Those two messages are now output using writeVerboseLine
| 'Output format. "json" writes machine-readable JSON to stdout (informational ' + | ||
| 'messages go to stderr). "text" writes a human-readable table.', |
There was a problem hiding this comment.
Generally avoid printing non-erroneous data to stderr. In JSON mode, why not just not output anything that can't be JSON-parsed?
There was a problem hiding this comment.
but if we avoid any other output (like verbose or debug or other) in the stderr then for json output mode it will be really hard to debug what the CLI is doing when using --verbose option, right?
| terminal | ||
| ); | ||
|
|
||
| this._outputParameter = this.defineChoiceParameter({ |
There was a problem hiding this comment.
Consider making this a --json flag instead.
There was a problem hiding this comment.
I would strongly recommend adding a universal --output option in which we may specify the output type of the command we want to use. For example, what if in the near future we would like to add a --csv output mode with is better for reporting and for LLMs when used for very large collections. Or maybe we could like to add an md output mode to be even more friendly for humans.
What do you think?
|
@iclanton thanks for the review 👍. You Rock 🤩 |
|
@iclanton did you have a chance to take a look at my responses? I think it would be cool to start moving this PR forward |
77d8b1c to
1d11338
Compare
1d11338 to
377b7d7
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
apps/spfx-cli/README.md:80
- The CLI now exposes a
--verboseflag (shown in the generated--helpoutput), but thespfx list-templatesREADME section doesn’t document it in the Optional flags table. This makes the README inconsistent with the actual CLI surface area.
### Optional flags
| Flag | Default | Description |
|------|---------|-------------|
| `-o`, `--output {json,text}` | `json` | Output format. `json` writes machine-readable JSON to stdout (informational messages go to stderr). `text` writes a human-readable table |
| `--spfx-version VERSION` | `version/latest` branch | Branch/tag in the default template repo to use (e.g. `1.22`, `1.23-rc.0`) |
| `--template-url URL` | `https://github.com/SharePoint/spfx` | Custom GitHub template repository (default source) |
| `--local-source PATH` | — | Path to a local template folder to include (repeatable) |
| `--remote-source URL` | — | Additional public GitHub repo to include as a template source (repeatable) |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
apps/spfx-cli/README.md:80
--verboseis now supported (and shown in--help), but it isn’t documented in thespfx list-templatesoptional flags table. This makes it easy to miss how to surface the source-registration messages when needed.
| Flag | Default | Description |
|------|---------|-------------|
| `-o`, `--output {json,text}` | `json` | Output format. `json` writes machine-readable JSON to stdout (informational messages go to stderr). `text` writes a human-readable table |
| `--spfx-version VERSION` | `version/latest` branch | Branch/tag in the default template repo to use (e.g. `1.22`, `1.23-rc.0`) |
| `--template-url URL` | `https://github.com/SharePoint/spfx` | Custom GitHub template repository (default source) |
| `--local-source PATH` | — | Path to a local template folder to include (repeatable) |
| `--remote-source URL` | — | Additional public GitHub repo to include as a template source (repeatable) |
| const verboseEnabled: boolean = process.argv.includes('--verbose'); | ||
| const terminal: Terminal = new Terminal(new ConsoleTerminalProvider({ verboseEnabled })); |
|
@iclanton I fixed up the review comments to which I did not have any response. Please recheck also some of my comments to your feedback |
Description
The aim is to:
list-templatescommand that supports JSON (which will be the default mode) and TEXT.--verboseflag and moved some of the information command outputs to be part of the verbose, so that they are not part of stdout but instead are present in stderrHow was this tested?
Result
Help now also has new
--outputand--verboseflagThe default output mode of the
list-templatescommand is now JSON, or you may use the--output jsonas welltext works as before but
--output textneeds to be usedCommand output is part of stdout, and all other messages go to stderr
To see additional info the
--verboseoption needs to be usedType of change
spfx list-templatescommand #235