Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

docs(cli): show apiVersion and the real search flag in the CLI README (#6587)

Joey444da2a51ce9
diff --git a/.changeset/cli-spec-readme-envelope-prose.md b/.changeset/cli-spec-readme-envelope-prose.md
new file mode 100644
index 0000000000..cf571004b1
--- /dev/null
+++ b/.changeset/cli-spec-readme-envelope-prose.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[docs] The CLI README now shows `apiVersion` in every hand-written envelope shape and example, and lists `astryx search --verbose` in place of `--detail`, which needs a level and does not add a result's score or reason.
+
+@josephfarina
diff --git a/packages/cli/README.md b/packages/cli/README.md
index 69ecb5fd5c..25fa003ee4 100644
--- a/packages/cli/README.md
+++ b/packages/cli/README.md
@@ -51,8 +51,8 @@ Options:
- `--type <component|hook|doc|template>`: restrict to a single domain
- `--limit <n>`: cap the number of results (default 20)
-- `--detail`: include the import path and the match reason/score
-- `--json`: typed `{ type: 'search', data: { query, matchCount, results } }` envelope — `matchCount` is how many candidates matched in total, `results` the slice `--limit` allowed
+- `--verbose`: also print each result's match score and reason
+- `--json`: typed `{ apiVersion, type: 'search', data: { query, matchCount, results } }` envelope — `matchCount` is how many candidates matched in total, `results` the slice `--limit` allowed
## Commands
diff --git a/packages/cli/test/readme-prose.test.mjs b/packages/cli/test/readme-prose.test.mjs
new file mode 100644
index 0000000000..d9cb1ec9c5
--- /dev/null
+++ b/packages/cli/test/readme-prose.test.mjs
@@ -0,0 +1,143 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file The README's hand-written envelope shapes and `search` flags match the
+ * CLI. The generated tables are covered by readme-gen.test.mjs.
+ */
+
+import * as fs from 'node:fs';
+import * as os from 'node:os';
+import * as path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import {describe, it, expect} from 'vitest';
+import {program, JSON_SUPPORTED} from '../clients/cli/index.mjs';
+import {buildManifest} from '../clients/cli/lib/manifest.mjs';
+import {runCli} from '../test-utils/run-cli.mjs';
+
+const README = fs
+ .readFileSync(path.join(path.dirname(fileURLToPath(import.meta.url)), '../README.md'), 'utf8')
+ .replace(/<!-- BEGIN GENERATED: (\S+) -->[\s\S]*?<!-- END GENERATED: \1 -->/g, '');
+
+/** INV2: success is {apiVersion, type, data} + meta?; failure is {apiVersion, error, code} + suggestions?. */
+const SHAPES = {
+ success: {required: ['apiVersion', 'type', 'data'], optional: ['meta']},
+ failure: {required: ['apiVersion', 'error', 'code'], optional: ['suggestions']},
+};
+
+/** Every balanced `{...}` group in `text`. @param {string} text */
+function braceGroups(text) {
+ const groups = [];
+ const open = [];
+ for (let i = 0; i < text.length; i++) {
+ if (text[i] === '{') open.push(i);
+ else if (text[i] === '}' && open.length > 0) groups.push(text.slice(open.pop(), i + 1));
+ }
+ return groups;
+}
+
+/**
+ * Top-level keys of an object literal or shape: `{ a, b?: x, "c": {...} }`.
+ * @param {string} group
+ */
+function topLevelKeys(group) {
+ const keys = [];
+ let depth = 0;
+ let quote = '';
+ let entry = '';
+ const flush = () => {
+ const m = /^\s*["']?([A-Za-z_$][\w$]*)["']?\??\s*(?::|$)/.exec(entry);
+ if (m) keys.push(m[1]);
+ entry = '';
+ };
+ const body = group.slice(1, -1);
+ for (let i = 0; i < body.length; i++) {
+ const ch = body[i];
+ if (quote) {
+ entry += ch;
+ if (ch === '\\') entry += body[++i] ?? '';
+ else if (ch === quote) quote = '';
+ } else if (ch === '"' || ch === "'") {
+ quote = ch;
+ entry += ch;
+ } else if ('{[('.includes(ch)) {
+ depth++;
+ entry += ch;
+ } else if ('}])'.includes(ch)) {
+ depth--;
+ entry += ch;
+ } else if (ch === ',' && depth === 0) {
+ flush();
+ } else {
+ entry += ch;
+ }
+ }
+ flush();
+ return keys;
+}
+
+/** The README's envelope shapes: groups keyed like a success or a failure. */
+function readmeEnvelopes() {
+ return braceGroups(README)
+ .map((group) => ({group, keys: topLevelKeys(group)}))
+ .flatMap(({group, keys}) => {
+ if (keys.includes('error')) return [{group, keys, kind: 'failure'}];
+ if (keys.includes('type') && keys.includes('data')) return [{group, keys, kind: 'success'}];
+ return [];
+ });
+}
+
+describe('README envelope shapes', () => {
+ it('finds the hand-written envelopes', () => {
+ const kinds = readmeEnvelopes().map((e) => e.kind);
+ expect(kinds.filter((k) => k === 'success').length).toBeGreaterThanOrEqual(4);
+ expect(kinds.filter((k) => k === 'failure').length).toBeGreaterThanOrEqual(2);
+ });
+
+ it('real envelopes carry exactly the documented keys', async () => {
+ const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-readme-'));
+ const success = JSON.parse((await runCli(['--json', 'docs'], {cwd})).stdout);
+ const failure = JSON.parse((await runCli(['--json', 'docs', 'no-such-topic'], {cwd})).stdout);
+ for (const [kind, envelope] of /** @type {const} */ ([
+ ['success', success],
+ ['failure', failure],
+ ])) {
+ const {required, optional} = SHAPES[kind];
+ for (const key of required) expect(Object.keys(envelope), kind).toContain(key);
+ for (const key of Object.keys(envelope)) expect([...required, ...optional], kind).toContain(key);
+ }
+ });
+
+ it.each(readmeEnvelopes().map((e) => [e.group.replace(/\s+/g, ' ').slice(0, 70), e]))(
+ '%s names the INV2 keys',
+ (_label, {keys, kind}) => {
+ const {required, optional} = SHAPES[kind];
+ for (const key of required) expect(keys).toContain(key);
+ for (const key of keys) expect([...required, ...optional]).toContain(key);
+ },
+ );
+});
+
+describe('README search options', () => {
+ const manifest = buildManifest(program, {jsonSupported: JSON_SUPPORTED, version: '0.0.0-test'});
+ const search = manifest.commands.find((c) => c.name === 'search');
+ const section = README.slice(
+ README.indexOf('## Finding things: `astryx search`'),
+ README.indexOf('## Commands'),
+ );
+ const bullets = [...section.matchAll(/^- `(--[\w-]+)( <[^>]+>)?`/gm)];
+
+ it('lists the search options', () => {
+ expect(bullets.length).toBeGreaterThanOrEqual(3);
+ });
+
+ it.each(bullets.map((m) => [m[1], Boolean(m[2])]))(
+ '`%s` is a search or global option with the same arity',
+ (flag, takesValue) => {
+ const option = [...search.options, ...manifest.globalOptions].find((o) =>
+ o.flag.split(/[\s,]+/).includes(flag),
+ );
+ expect(option, `${flag} is not accepted by astryx search`).toBeDefined();
+ expect(option.type !== 'boolean', `${flag} value arity`).toBe(takesValue);
+ },
+ );
+});