An open source design system that's fully customizable and agent ready
TypeScript
fix(cli): describe both full envelopes in the --json help text (#6549)
Joey1fdc13061826
diff --git a/.changeset/cli-spec-json-flag-help-envelopes.md b/.changeset/cli-spec-json-flag-help-envelopes.mdnew file mode 100644index 0000000000..8b1fa5facd--- /dev/null+++ b/.changeset/cli-spec-json-flag-help-envelopes.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] The `--json` option's description in `astryx --help`, in the manifest, and in the CLI README now lists every envelope field: `{ apiVersion, type, data, meta? }` on success and `{ apiVersion, error, code, suggestions? }` on failure. It used to omit `apiVersion`, `meta`, and the stable `code` field that consumers branch on.++@josephfarinadiff --git a/packages/cli/README.md b/packages/cli/README.mdindex 8bea47e594..11e1459529 100644--- a/packages/cli/README.md+++ b/packages/cli/README.md@@ -84,7 +84,7 @@ Options:These flags work with any command:-- `--json`: Output as typed JSON envelope: `{ type, data }` (errors: `{ error, code, suggestions? }`)+- `--json`: Output as typed JSON envelope: `{ apiVersion, type, data, meta? }` (errors: `{ apiVersion, error, code, suggestions? }`)- `--detail <level>`: Detail level for list views, increasing in size: `brief` (names only, default for `--list`) < `compact` (names + 1-line descriptions) < `full` (full docs per entry). Single-item views default to `full`.- `--zh`: Output docs in Chinese Simplified- `--dense`: Compressed format (token-efficient, useful for AI agents)@@ -95,13 +95,14 @@ These flags work with any command:Every command supports `--json` for machine-readable output. Responses are typed envelopes:```json-{"type": "component.detail", "data": {"name": "Button", ...}}+{"apiVersion": 1, "type": "component.detail", "data": {"name": "Button", ...}}```Errors:```json{+ "apiVersion": 1,"error": "No component named \"Buttn\"","code": "ERR_UNKNOWN_COMPONENT","suggestions": [{"name": "Button", "reason": "similar name"}]@@ -343,9 +344,9 @@ import type {// ...import the response types for the commands you consume} from '@astryxdesign/cli/json';-// parseResponse returns the structural { type, data, meta? } envelope; `data`-// is `unknown` until you narrow it. Reconstruct the union you care about from-// the per-command response types, then narrow on `type`:+// parseResponse returns the structural { apiVersion, type, data, meta? }+// envelope; `data` is `unknown` until you narrow it. Reconstruct the union you+// care about from the per-command response types, then narrow on `type`:type MyResponse =ComponentDetailResponse | ComponentListResponse | DocsListResponse;diff --git a/packages/cli/clients/cli/commands/json-contract.test.mjs b/packages/cli/clients/cli/commands/json-contract.test.mjsindex 29b6ea3ad8..6a09dd9260 100644--- a/packages/cli/clients/cli/commands/json-contract.test.mjs+++ b/packages/cli/clients/cli/commands/json-contract.test.mjs@@ -172,3 +172,36 @@ describe('--json contract: supported commands emit valid envelopes', () => {expect(parsed.data.summary.fail).toBeGreaterThan(0);});});++describe('--json contract: the flag describes the envelopes it emits', () => {+ /** @param {string} shape e.g. ` apiVersion, error, code, suggestions? ` */+ const fields = shape => shape.split(',').map(f => f.trim().replace(/\?$/, ''));++ it('names every field of the success and error envelopes', async () => {+ const manifest = parseJson((await runCli(['manifest', '--json'], {cwd: tmpDir})).stdout);+ const {description} = manifest.data.globalOptions.find(o => o.flag === '--json');+ const match = /Success envelope: \{([^}]*)\}.*Error envelope: \{([^}]*)\}/.exec(description);+ expect(match, description).not.toBeNull();+ const [, success, error] = /** @type {RegExpExecArray} */ (match);++ for (const key of Object.keys(manifest)) expect(fields(success)).toContain(key);+ expect(fields(success)).toContain('meta');++ // An unknown command's envelope carries every error field, suggestions included.+ const failure = parseJson((await runCli(['bogus-cmd', '--json'], {cwd: tmpDir})).stdout);+ expect(Object.keys(failure)).toContain('suggestions');+ for (const key of Object.keys(failure)) expect(fields(error)).toContain(key);+ });++ it('the CLI README describes the same envelopes', () => {+ const readme = fs.readFileSync(new URL('../../../README.md', import.meta.url), 'utf8');+ const line = readme+ .split('\n')+ .find(l => l.startsWith('- `--json`: Output as typed JSON envelope:'));+ expect(line, 'README global --json line').toBeDefined();+ const [success, error] = [...String(line).matchAll(/`\{([^}]*)\}`/g)].map(m => fields(m[1]));+ expect(success).toEqual(expect.arrayContaining(['apiVersion', 'type', 'data', 'meta']));+ expect(error).toEqual(expect.arrayContaining(['apiVersion', 'error', 'code', 'suggestions']));+ expect(readme).toContain('{"apiVersion": 1, "type": "component.detail"');+ });+});diff --git a/packages/cli/clients/cli/index.mjs b/packages/cli/clients/cli/index.mjsindex f6875a164b..a9679d451f 100644--- a/packages/cli/clients/cli/index.mjs+++ b/packages/cli/clients/cli/index.mjs@@ -352,7 +352,7 @@ export async function createProgram() {).option('--json',- 'Output as typed JSON. Success envelope: { type, data }. Error envelope: { error, suggestions? }.',+ 'Output as typed JSON. Success envelope: { apiVersion, type, data, meta? }. Error envelope: { apiVersion, error, code, suggestions? }.',).addHelpCommand('help', 'Show all commands').action((options, cmd) => {