Skip to content

feat(blocks): print the JSON result when stdout isn't a terminal - #1516

Closed
xyos wants to merge 1 commit into
aws:mainfrom
xyos:feat/cli-json-default
Closed

xyos wants to merge 1 commit into
aws:mainfrom
xyos:feat/cli-json-default

Conversation

@xyos

@xyos xyos commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

cdk-blocks prints text unless the caller passes --output json. Scripts, CI steps and coding agents that read a command's stdout have to know to ask for JSON, and a caller that forgets gets text written for a person. Some CLIs built for coding agents make JSON the default when stdout isn't a terminal; this PR does the same for the cdk-blocks commands that have a structured result, and for the npm scripts that run them.

Issue #, if available: none

Changes

Breaking change: please flag for maintainer review. The piped output of the commands with a JSON result changes from text to JSON, and so does the piped output of npm run deploy, npm run sandbox:deploy and npm run sandbox:preview in a generated app. In JSON format the BLOCKS_DEPLOYED line goes to stderr. --output text restores the old output (npm run deploy -- --output text).

Behaviour. The format is resolved in one place: resultFormat() in context.ts, where the Output is built.

Command stdout --output Result format
deploy, sandbox deploy, sandbox preview, whoami, dev stop, migrate (and their aliases: dev:stop, cleanup, sandbox:deploy, sandbox --deploy-only, …) terminal not given text (as today)
same pipe or file not given JSON (exactly what --output json prints today; child stdout goes to stderr)
same any text text
same any json JSON
same any any, with --query <path> the field, raw (unchanged)
every other command any not given text; never an error
every other command any json exit 2 (unchanged)
--help, help <command>, --version any not given text

Globals.output is now undefined when --output isn't given, so an explicit flag can win; migrate's refusal document follows the resolved format.

npm scripts. The generated scripts stay plain aliases ("deploy": "cdk-blocks deploy"), so a piped npm run deploy prints the JSON result, in CI too, and a terminal shows text. npm prints its own > <app>@<version> deploy lines on stdout before the script runs, so stdout is only the JSON document with npm run -s deploy; the docs say so.

Consumers of these commands' stdout

  • Coding agents: the scaffolded app AGENTS.md tells them to run npm run -s deploy (in the background, npm run -s deploy > deploy-result.log 2> deploy.log &) and read {apiUrl, url, stack, account, region, live}; sandbox:deploy gives the same without live, with url: null, and sandbox:preview gives the preview URL. With --wait-live, "live": false and exit 1 mean the deploy succeeded but the new build isn't confirmed.
  • CI and e2e: scripts/ci/windows-template-e2e.mjs runs npm run deploy with inherited stdio and keys readiness off files, and templates-e2e.mjs deploys nothing; neither reads these commands' stdout. The test apps and the native-bindings workflow call core's scripts, not cdk-blocks. No workflow calls these commands directly.
  • Docs: the @aws-blocks/blocks README (Output and exit codes), the scaffolded app AGENTS.md and the create-blocks-app README; the root help says it, and so does the help of each command with a JSON result.

Interaction with the cdk-blocks docs stack. #1521 (open, on top of #1511) gives cdk-blocks docs a JSON result for an explicit --output json only, and registers it with jsonOnRequest: true on its CommandSpec: piped docs output stays Markdown. Whichever of the two merges second must make resultFormat() treat a command with jsonOnRequest like a command without a JSON result unless --output is given, and leave such commands out of the "every command with a JSON result prints JSON on a pipe" test. packages/blocks/src/cli/parse.ts is changed by both, so that merge may also need a trivial rebase.

Validation

Node 22 (.nvmrc), at the head of this branch:

Command Result
npm run build exit 0
npm test -w @aws-blocks/blocks exit 0: 623 tests, 620 pass, 0 fail, 3 skipped
npm test -w @aws-blocks/create-blocks-app exit 0: 352 tests, 352 pass
npx biome lint packages/blocks/src/cli exit 0

The earlier revision also passed npm run lint, npm run lint:deps, the full npm test and npm run test:e2e:local.

A temporary app whose script runs the built CLI, with stdout piped (npm 11.13): npm run -s dev:stop | jq -c . prints {"stopped":[]}; npm run dev:stop prints npm's > header lines before the JSON; npm run -s dev:stop -- --output text prints the text.

New tests:

  • context.test.ts, "the result format": each command with a JSON result and its aliases on a pipe, a non-TTY stream and a terminal; an explicit --output text/--output json on both; every command without a JSON result, plus root, --version and --help, on a pipe; --query with and without --output text.
  • commands.test.ts: piped deploy prints the recorded result as JSON with child stdout on stderr; piped deploy --output text keeps child stdout; piped whoami is JSON, --output text is text, and --query prints its field on a pipe and on a terminal; piped sandbox destroy, dev, --version and whoami --help stay text; sandbox destroy --output json still exits 2.
  • bin.test.ts: the built cdk-blocks bin with stdout a pipe prints {"stopped": []} for dev stop --all, text with --output text, and text for sandbox destroy and --version. No AWS.
  • create-blocks-app's cli.test.ts: the background commands in AGENTS.md may write stdout and stderr to separate *.log files, which the templates ignore.

The fake stdout of runCli in test-helpers.ts is a terminal by default (stdoutIsTTY), so the existing text-output tests keep their meaning.

No sandbox e2e: the JSON document is the one --output json already printed, and no serialization changed.

Checklist

  • PR description included
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@changeset-bot

changeset-bot Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 54d4a84

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@aws-blocks/blocks Minor
@aws-blocks/create-blocks-app Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@xyos
xyos marked this pull request as ready for review November 16, 2026 13:14
@xyos
xyos requested a review from a team as a code owner November 16, 2026 13:14

@VarshithaPamisetty VarshithaPamisetty 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.

In registry.ts, the cdk-blocks deploy --output json example says Print {apiUrl, url, stack, account, region}; the command description now lists live too.

Suggestion: add live to the example.

['dev:stop'],
['cleanup'],
['sandbox:deploy'],
['sandbox', '--deploy-only'],

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.

The alias loop covers dev:stop, cleanup, sandbox:deploy and sandbox --deploy-only, but not sandbox:preview or sandbox --preview, which also run sandbox preview.

Suggestion: add ['sandbox:preview'] and ['sandbox', '--preview'] to the array.

'cdk-blocks config',
'cdk-blocks vendorize',
]);
const RENAMED_ALIASES: ReadonlyMap<string, string> = new Map(

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.

migrate renames blocks deploy but not cdk-blocks deploy. Every template before this PR scaffolded "deploy": "cdk-blocks deploy", "sandbox:deploy": "cdk-blocks sandbox deploy" and "sandbox:preview": "cdk-blocks sandbox preview", and the previous migrate wrote the same values. For those apps migrate reports nothing to do and their piped output becomes JSON; the changeset tells them to add --output text by hand.

Suggestion: have migrate rewrite those three values to their --output text forms.

const { globals, command } = parsed;
if (globals.output !== undefined) return globals.output;
if (globals.help || globals.version || !command?.json) return 'text';
return stdout.isTTY === true ? 'text' : 'json';

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.

When this picks json, nothing on stderr says the format changed or that --output text exists, so a user whose piped script breaks gets a JSON document with no explanation. There is also no environment variable or config setting to keep text everywhere; each script is edited one by one. colorEnabled in output.ts makes the same isTTY check but honours NO_COLOR and TERM=dumb, and isCi is already used for interactivity; this check consults neither.

const json = await runCli(['deploy'], piped);
assert.strictEqual(json.code, 0, json.stderr);
// As with --output json: the child's stdout, BLOCKS_DEPLOYED with it, goes to stderr.
assert.strictEqual(json.spawned[0].stdoutToStderr, true);

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.

This checks the flag given to the fake spawn, not where the child's output ends up. No test runs a child on a real pipe: bin.test.ts only runs dev stop, sandbox destroy and --version. If the stdio mapping in context.ts sent the child's stdout to stdout instead of stderr, BLOCKS_DEPLOYED would land inside the JSON and every test here would stay green.

"dev:server": "cdk-blocks dev",
"dev": "cdk-blocks dev",
"dev:stop": "cdk-blocks dev stop",
"sandbox": "cdk-blocks sandbox",

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.

cdk-blocks sandbox --deploy-only and --preview resolve to sandbox deploy and sandbox preview, which now print JSON on a pipe, and this script has no --output text. npm run sandbox -- --deploy-only > log prints JSON while npm run sandbox:deploy > log prints text, for the same deploy. Same in all 12 templates.

Suggestion: "sandbox": "cdk-blocks sandbox --output text"; --output text is accepted on every command.

Without --output, the cdk-blocks commands with a JSON result (deploy,
sandbox deploy, sandbox preview, whoami, dev stop, migrate) print what
--output json prints when stdout is a pipe or a file, and text in a
terminal. An explicit --output wins; --query is unchanged; every other
command prints text and still rejects an explicit --output json.

The scripts that create-blocks-app generates stay plain aliases, so a
piped npm run deploy prints the JSON result, in CI too. The scaffolded
AGENTS.md tells agents to run npm run -s deploy, which keeps npm's
lifecycle header off stdout, and to read that document.
@xyos

xyos commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by #1610, which carries the terminal-text and piped-JSON behavior into package-manager tasks without shipping a standalone CLI.

@xyos xyos closed this Nov 18, 2026
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.

2 participants