Skip to content

Adds JSON output mode for the spfx list-templates command. Closes #235#253

Open
Adam-it wants to merge 2 commits into
SharePoint:mainfrom
Adam-it:adds-json-output-mode
Open

Adds JSON output mode for the spfx list-templates command. Closes #235#253
Adam-it wants to merge 2 commits into
SharePoint:mainfrom
Adam-it:adds-json-output-mode

Conversation

@Adam-it

@Adam-it Adam-it commented Apr 25, 2026

Copy link
Copy Markdown

Description

The aim is to:

  • introduce output option for the list-templates command that supports JSON (which will be the default mode) and TEXT.
  • added --verbose flag 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 stderr

How was this tested?

  • added new unit tests for the new functionality
  • checked locally, see the result below with description

Result

Help now also has new --output and --verbose flag

{D8045D9C-FAC3-4025-AC26-CEE59A3132AA}

The default output mode of the list-templates command is now JSON, or you may use the --output json as well

{3BAFD674-74AF-4B6D-B3B3-0C50A96B18BF}

text works as before but --output text needs to be used

{BFE71F7C-8B20-4FFB-955C-403060D3BE2C}

Command output is part of stdout, and all other messages go to stderr

{3FDD88FB-86EA-4289-88EF-5E6648CA0256}

To see additional info the --verbose option needs to be used

{239E417F-B7DF-4F59-9789-A800B63B91B8}

Type of change

Copilot AI review requested due to automatic review settings April 25, 2026 19:56

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

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} to list-templates, defaulting to json
  • Implemented JSON serialization for template collections (toJsonString()) and updated CLI action to write JSON to stdout
  • Introduced StderrTerminalProvider and 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.

Comment thread apps/spfx-cli/src/cli/actions/ListTemplatesAction.ts Outdated
Comment thread apps/spfx-cli/src/utilities/StderrTerminalProvider.ts Outdated
Comment thread apps/spfx-cli/src/cli/actions/tests/ListTemplatesAction.test.ts Outdated
@Adam-it Adam-it marked this pull request as draft April 25, 2026 20:48
@Adam-it Adam-it force-pushed the adds-json-output-mode branch from 26860dd to 65b297d Compare April 25, 2026 21:37
@Adam-it Adam-it marked this pull request as ready for review April 25, 2026 21:38
Copilot AI review requested due to automatic review settings April 25, 2026 21:38

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 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-templates docs list the new --output option, but the command also now supports --verbose (and help output advertises it). The README’s optional flags table should include --verbose and 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.

Comment thread apps/spfx-cli/src/cli/actions/ListTemplatesAction.ts Outdated
@Adam-it Adam-it force-pushed the adds-json-output-mode branch from 9e88690 to 77d8b1c Compare April 25, 2026 21:57
Comment thread api/spfx-template-api/etc/spfx-template-api.api.md Outdated
Comment thread api/spfx-template-api/etc/spfx-template-api.api.md Outdated
Comment thread api/spfx-template-api/src/repositories/SPFxTemplateCollection.ts Outdated
Comment thread api/spfx-template-api/src/repositories/SPFxTemplateCollection.ts
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]",

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.

Unrelated change?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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

Comment thread apps/spfx-cli/src/cli/actions/tests/ListTemplatesAction.test.ts Outdated
Comment thread apps/spfx-cli/src/cli/actions/tests/ListTemplatesAction.test.ts Outdated
Comment thread common/changes/@microsoft/spfx-cli/json-output-mode_2026-04-25.json Outdated
Comment on lines +31 to +32
'Output format. "json" writes machine-readable JSON to stdout (informational ' +
'messages go to stderr). "text" writes a human-readable table.',

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.

Generally avoid printing non-erroneous data to stderr. In JSON mode, why not just not output anything that can't be JSON-parsed?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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 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({

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.

Consider making this a --json flag instead.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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?

@Adam-it

Adam-it commented Jun 7, 2026

Copy link
Copy Markdown
Author

@iclanton thanks for the review 👍. You Rock 🤩
I added a few replies and clarifications for your feedback. Please recheck my comments before I make any unnecessary changes in code 🙂, I think some are really worth reconsidering

@Adam-it

Adam-it commented Jun 12, 2026

Copy link
Copy Markdown
Author

@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

@Adam-it Adam-it force-pushed the adds-json-output-mode branch from 1d11338 to 377b7d7 Compare June 26, 2026 21:31

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 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 --verbose flag (shown in the generated --help output), but the spfx list-templates README 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) |

Copilot AI review requested due to automatic review settings June 26, 2026 21:31

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 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

  • --verbose is now supported (and shown in --help), but it isn’t documented in the spfx list-templates optional 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) |

Comment on lines +8 to +9
const verboseEnabled: boolean = process.argv.includes('--verbose');
const terminal: Terminal = new Terminal(new ConsoleTerminalProvider({ verboseEnabled }));
@Adam-it

Adam-it commented Jun 26, 2026

Copy link
Copy Markdown
Author

@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

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.

Introduce JSON output mode for the spfx list-templates command

3 participants