Repository navigation
Conversation
🦋 Changeset detectedLatest commit: 54d4a84 The changes in this PR will be included in the next version bump. This PR includes changesets to release 2 packages
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 |
VarshithaPamisetty
left a comment
There was a problem hiding this comment.
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'], |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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.
31f876a to
54d4a84
Compare
|
Superseded by #1610, which carries the terminal-text and piped-JSON behavior into package-manager tasks without shipping a standalone CLI. |
Problem
cdk-blocksprints 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 thecdk-blockscommands that have a structured result, and for the npm scripts that run them.Issue #, if available: none
Changes
Behaviour. The format is resolved in one place:
resultFormat()incontext.ts, where theOutputis built.--outputdeploy,sandbox deploy,sandbox preview,whoami,dev stop,migrate(and their aliases:dev:stop,cleanup,sandbox:deploy,sandbox --deploy-only, …)--output jsonprints today; child stdout goes to stderr)textjson--query <path>json--help,help <command>,--versionGlobals.outputis nowundefinedwhen--outputisn'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 pipednpm run deployprints the JSON result, in CI too, and a terminal shows text. npm prints its own> <app>@<version> deploylines on stdout before the script runs, so stdout is only the JSON document withnpm run -s deploy; the docs say so.Consumers of these commands' stdout
AGENTS.mdtells them to runnpm 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:deploygives the same withoutlive, withurl: null, andsandbox:previewgives the preview URL. With--wait-live,"live": falseand exit 1 mean the deploy succeeded but the new build isn't confirmed.scripts/ci/windows-template-e2e.mjsrunsnpm run deploywith inherited stdio and keys readiness off files, andtemplates-e2e.mjsdeploys nothing; neither reads these commands' stdout. The test apps and the native-bindings workflow call core's scripts, notcdk-blocks. No workflow calls these commands directly.@aws-blocks/blocksREADME (Output and exit codes), the scaffolded appAGENTS.mdand 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 docsstack. #1521 (open, on top of #1511) givescdk-blocks docsa JSON result for an explicit--output jsononly, and registers it withjsonOnRequest: trueon itsCommandSpec: pipeddocsoutput stays Markdown. Whichever of the two merges second must makeresultFormat()treat a command withjsonOnRequestlike a command without a JSON result unless--outputis 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.tsis changed by both, so that merge may also need a trivial rebase.Validation
Node 22 (
.nvmrc), at the head of this branch:npm run buildnpm test -w @aws-blocks/blocksnpm test -w @aws-blocks/create-blocks-appnpx biome lint packages/blocks/src/cliThe earlier revision also passed
npm run lint,npm run lint:deps, the fullnpm testandnpm 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:stopprints npm's>header lines before the JSON;npm run -s dev:stop -- --output textprints 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 jsonon both; every command without a JSON result, plus root,--versionand--help, on a pipe;--querywith and without--output text.commands.test.ts: pipeddeployprints the recorded result as JSON with child stdout on stderr; pipeddeploy --output textkeeps child stdout; pipedwhoamiis JSON,--output textis text, and--queryprints its field on a pipe and on a terminal; pipedsandbox destroy,dev,--versionandwhoami --helpstay text;sandbox destroy --output jsonstill exits 2.bin.test.ts: the builtcdk-blocksbin with stdout a pipe prints{"stopped": []}fordev stop --all, text with--output text, and text forsandbox destroyand--version. No AWS.cli.test.ts: the background commands inAGENTS.mdmay write stdout and stderr to separate*.logfiles, which the templates ignore.The fake stdout of
runCliintest-helpers.tsis a terminal by default (stdoutIsTTY), so the existing text-output tests keep their meaning.No sandbox e2e: the JSON document is the one
--output jsonalready printed, and no serialization changed.Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.