Skip to content

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

TypeScript

fix(cli): return the build playbook as data, and render the text from it (#6566)

Joeya860a5010f5a
diff --git a/.changeset/cli-spec-build-playbook-data.md b/.changeset/cli-spec-build-playbook-data.md
new file mode 100644
index 0000000000..78ea6e1db8
--- /dev/null
+++ b/.changeset/cli-spec-build-playbook-data.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/cli': patch
+---
+
+[fix] `astryx build --json` with no query, and `build()` with no query, now return the playbook itself — a title, the ordered steps with their commands, the on-system rules, and related lookups — instead of only `{playbook: true}`. The terminal output is rendered from the same data, and `playbook: true` is still there.
+
+@josephfarina
diff --git a/packages/cli/README.md b/packages/cli/README.md
index 25fa003ee4..000550fe0b 100644
--- a/packages/cli/README.md
+++ b/packages/cli/README.md
@@ -418,7 +418,7 @@ Every response has a `type` discriminant. The full set is below (generated from
| `discover.detail.doc` | The validated ComponentDoc for one external component: an @scope/name/Component query, or a free-text term resolving to exactly one component. |
| `discover.search` | The echoed query plus the matching {package, component} pairs, when a free-text term matches several components. |
| `search` | The echoed query, `matchCount` (how many candidates matched in total, before `limit`), plus a ranked SearchResultEntry[] bounded by `limit` (domain, name, score, reason, description, follow-up command, and import path where relevant). |
-| `build.help` | A marker (`playbook: true`) that the renderer expands into the how-to-build-a-page workflow; emitted when no query is given. |
+| `build.help` | The how-to-build-a-page playbook, emitted when no query is given: `playbook: true`, a title, the ordered steps (title, commands, optional returns), the on-system rules, and related lookups. Commands are bare subcommands for the caller to render with its own invocation. |
| `build.kit` | The grouped composition kit: echoed query, hasResults/matchCount/directMatch fields (matchCount is the total matched, never a cap read back), the closest page templates, drop-in block patterns, idea-specific components/hooks, and the always-on frame + foundation component-name arrays. |
| `swizzle.list` | The names of swizzlable components discoverable from cwd's @astryxdesign/core. |
| `swizzle.copy` | An eject receipt: component name, owning package, output directory, files-copied count, the written file names, whether any file uses StyleX, and an optional maintainer note. |
diff --git a/packages/cli/api/build/build.doc.mjs b/packages/cli/api/build/build.doc.mjs
index 3d9fbc1646..63a67caa48 100644
--- a/packages/cli/api/build/build.doc.mjs
+++ b/packages/cli/api/build/build.doc.mjs
@@ -16,8 +16,8 @@ export const doc = {
'Page-building assistant: the how-to-build playbook, or a composition kit for an idea.',
description:
'The "assemble a page" entry point. Called with no query it returns the ' +
- 'playbook signal that the renderer expands into the how-to-build-a-page ' +
- 'workflow. Called with a query it runs the unified search and groups the ' +
+ 'how-to-build-a-page playbook as data: the workflow steps with their ' +
+ 'commands, the on-system rules, and related lookups. Called with a query it runs the unified search and groups the ' +
'hits into a composition KIT: the closest page templates, drop-in blocks, ' +
'and idea-specific components/hooks, plus the always-on frame + foundation.',
importPath: '@astryxdesign/cli/api',
@@ -54,7 +54,7 @@ export const doc = {
{
type: 'build.help',
description:
- 'Emitted when the query is omitted: a pure marker (`data.playbook: true`) that the command renderer expands into the page-building workflow prose.',
+ 'Emitted when the query is omitted: the page-building playbook — `playbook: true`, a `title`, the ordered `steps` (each a `title`, its `commands`, and optionally what the step `returns`), the on-system `rules`, and `related` lookups. Each command is a bare subcommand ({command, purpose?}) for the caller to render with its own CLI invocation.',
},
{
type: 'build.kit',
diff --git a/packages/cli/api/build/build.test.mjs b/packages/cli/api/build/build.test.mjs
index a6c81c2668..b2f551b062 100644
--- a/packages/cli/api/build/build.test.mjs
+++ b/packages/cli/api/build/build.test.mjs
@@ -1,7 +1,7 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
/**
- * @file Tests for the build API (playbook signal + composition kit).
+ * @file Tests for the build API (playbook + composition kit).
*/
import {describe, it, expect, vi} from 'vitest';
@@ -20,10 +20,21 @@ const REPO = path.resolve(
vi.setConfig({testTimeout: 30000});
describe('build API', () => {
- it('no query → build.help playbook signal', async () => {
+ it('no query → build.help carries the playbook as data', async () => {
const r = await build();
expect(r.type).toBe('build.help');
- expect(r.data).toEqual({playbook: true});
+ if (r.type !== 'build.help') return;
+ expect(r.data.playbook).toBe(true);
+ expect(r.data.title).toMatch(/build a page/i);
+ expect(r.data.steps.length).toBeGreaterThan(0);
+ for (const step of r.data.steps) {
+ expect(step.title).toBeTruthy();
+ expect(step.commands.length).toBeGreaterThan(0);
+ }
+ expect(r.data.rules.length).toBeGreaterThan(0);
+ // Bare subcommands: the caller adds its own invocation.
+ const commands = [...r.data.steps.flatMap(s => s.commands), ...r.data.related];
+ for (const {command} of commands) expect(command).not.toMatch(/^(astryx|npx|pnpm|yarn|bunx?)\b/);
});
it('query → build.kit with raw entries + static frame/foundation', async () => {
diff --git a/packages/cli/api/build/build.type.mjs b/packages/cli/api/build/build.type.mjs
index aa9aeee021..f893ffd7c0 100644
--- a/packages/cli/api/build/build.type.mjs
+++ b/packages/cli/api/build/build.type.mjs
@@ -2,17 +2,38 @@
/**
* @file Colocated types for the `build` command — source of truth for the
- * `build.help` (playbook signal) and `build.kit` (composition kit) JSON
- * responses. Re-exported by types/build.d.ts.
+ * `build.help` (playbook) and `build.kit` (composition kit) JSON responses.
+ * Re-exported by types/build.d.ts.
*/
/**
- * astryx --json build (no query) — the "how to build a page" playbook signal.
+ * A command the playbook tells the caller to run.
+ *
+ * @typedef {object} BuildPlaybookCommand
+ * @property {string} command Bare subcommand with `<placeholder>` arguments (e.g. `template <name> --skeleton`) and no package-manager prefix — render it with your own CLI invocation.
+ * @property {string} [purpose] What running it is for.
+ */
+
+/**
+ * One step of the page-building workflow.
+ *
+ * @typedef {object} BuildPlaybookStep
+ * @property {string} title What to do.
+ * @property {BuildPlaybookCommand[]} commands The commands for this step, in the order to run them.
+ * @property {string} [returns] What the step's command gives back, when that decides the next step.
+ */
+
+/**
+ * astryx --json build (no query) — the "how to build a page" playbook.
*
* @typedef {object} BuildHelpResponse
* @property {'build.help'} type
* @property {object} data
* @property {true} data.playbook Always true; marks this envelope as the playbook rather than a result set.
+ * @property {string} data.title The playbook's heading.
+ * @property {BuildPlaybookStep[]} data.steps The workflow, in order.
+ * @property {string[]} data.rules The rules that keep a page on-system.
+ * @property {BuildPlaybookCommand[]} data.related Lookups to reach for alongside the workflow.
*/
/**
diff --git a/packages/cli/api/build/help/help.mjs b/packages/cli/api/build/help/help.mjs
index ede1494789..32eb64fe92 100644
--- a/packages/cli/api/build/help/help.mjs
+++ b/packages/cli/api/build/help/help.mjs
@@ -1,18 +1,68 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
/**
- * @file build.help leaf — the "how to build a page" playbook signal.
+ * @file build.help leaf — the "how to build a page" playbook.
*
- * `build` with no query returns this envelope: a pure marker that the command
- * renderer expands into the workflow playbook prose. It carries no data beyond
- * `playbook: true`, so the JSON shape stays environment-agnostic and stable.
+ * `build` with no query returns this envelope. The playbook is data — steps,
+ * rules, and related lookups — so a `--json` or programmatic caller gets the
+ * same guidance the terminal shows. Commands are bare subcommands with no
+ * package-manager prefix, which keeps the JSON environment-agnostic; the CLI
+ * renders each one with the caller's invocation, as it does build.kit's
+ * `hint.commands`.
*/
/**
- * The page-building playbook signal (emitted when `build` runs with no query).
+ * The page-building playbook (emitted when `build` runs with no query).
*
* @returns {import('../build.type.mjs').BuildHelpResponse}
*/
export function buildHelp() {
- return {type: 'build.help', data: {playbook: true}};
+ return {
+ type: 'build.help',
+ data: {
+ playbook: true,
+ title: 'How to build a page with Astryx',
+ steps: [
+ {
+ title: "Find a starting point for what you're building",
+ commands: [{command: 'build "<what you\'re building>"'}],
+ returns:
+ 'the closest [page] template, the [block]s that cover parts, and the [component]s to fill the gaps, with a recommended start',
+ },
+ {
+ title: 'If a [page] template matches, scaffold it and adapt',
+ commands: [{command: 'template <name> [path]'}],
+ },
+ {
+ title: 'If nothing matches exactly, compose',
+ commands: [
+ {
+ command: 'template <name> --skeleton',
+ purpose: "study a close page's layout",
+ },
+ {
+ command: 'template <BlockName>',
+ purpose: 'drop in each block from the kit',
+ },
+ {
+ command: 'component <Name>',
+ purpose: 'fill remaining gaps (read props)',
+ },
+ ],
+ },
+ ],
+ rules: [
+ 'No <div>/raw HTML for layout — use VStack/HStack/Grid/Stack/Card etc.',
+ 'No style={{}} — use component props, and design tokens for values.',
+ 'Wrap the app in <Theme theme={...}> and import core reset.css + astryx.css.',
+ ],
+ related: [
+ {command: 'docs tokens', purpose: 'the design tokens'},
+ {
+ command: 'search <query>',
+ purpose: 'a neutral lookup of any component, doc, or template',
+ },
+ ],
+ },
+ };
}
diff --git a/packages/cli/clients/cli/commands/build.mjs b/packages/cli/clients/cli/commands/build.mjs
index 744e0999fc..cd04d363c0 100644
--- a/packages/cli/clients/cli/commands/build.mjs
+++ b/packages/cli/clients/cli/commands/build.mjs
@@ -22,9 +22,9 @@ import {
emit,
section,
text,
+ list,
record,
records,
- ARROW,
} from '../formatters/index.mjs';
import {cliError} from '../lib/cli-error.mjs';
import {defineCommand} from '../lib/define-command.mjs';
@@ -33,46 +33,33 @@ import {doc as buildCommand} from './build.doc.mjs';
import {doc as buildFn} from '../../../api/build/build.doc.mjs';
/**
- * Emit the build playbook (shown when `build` is run with no query).
- * @param {string} run - The CLI invocation prefix (e.g. `npx astryx`).
+ * Playbook commands as records whose field names are the JSON keys, so a
+ * reader can grep `^command:`. The command is run with the caller's invocation.
+ * @type {import('../formatters/index.mjs').RecordOptions}
*/
-function printPlaybook(run) {
+const COMMAND_RECORDS = {
+ fields: ['command', 'purpose'],
+ format: {command: command => formatCliCommand(command)},
+};
+
+/**
+ * Emit the build playbook (shown when `build` is run with no query) — a
+ * projection of the `build.help` data, so text and JSON carry the same steps.
+ * @param {import('../../../api/build/build.type.mjs').BuildHelpResponse['data']} playbook
+ */
+function printPlaybook(playbook) {
emit(
- section('How to build a page with Astryx'),
- text(
- [
- "1. Find a starting point for what you're building:",
- ` ${run} build "<what you're building>"`,
- ` ${ARROW} returns the closest [page] template, the [block]s that cover parts,`,
- ' and the [component]s to fill the gaps, with a "Compose:" suggestion.',
- ].join('\n'),
- ),
- text(
- [
- `2. If a [page] template matches ${ARROW} scaffold it and adapt:`,
- ` ${run} template <name> [path]`,
- ].join('\n'),
- ),
- text(
- [
- `3. If nothing matches exactly ${ARROW} compose:`,
- ` ${run} template <name> --skeleton # study a close page's layout`,
- ` ${run} template <BlockName> # drop in each block from the kit`,
- ` ${run} component <Name> # fill remaining gaps (read props)`,
- ].join('\n'),
- ),
- text(
- [
- '4. Rules (keep it on-system):',
- ' - No <div>/raw HTML for layout — use VStack/HStack/Grid/Stack/Card etc.',
- ` - No style={{}} — use component props; design tokens via \`${run} docs tokens\`.`,
- ' - Wrap the app in <Theme theme={...}> and import core reset.css + astryx.css.',
- ].join('\n'),
- ),
- text(
- `Tip: \`${run} build "<idea>"\` is the fastest way in. For a neutral ` +
- `lookup of any component/doc/template, use \`${run} search <query>\`.`,
- ),
+ section(playbook.title),
+ ...playbook.steps.flatMap((step, i) => [
+ section(`${i + 1}. ${step.title}`),
+ records(step.commands, COMMAND_RECORDS),
+ step.returns ? record({returns: step.returns}) : null,
+ ]),
+ section(`${playbook.steps.length + 1}. Rules (keep it on-system)`),
+ list(playbook.rules),
+ ...(playbook.related.length > 0
+ ? [section('Related'), records(playbook.related, COMMAND_RECORDS)]
+ : []),
);
}
@@ -91,7 +78,10 @@ export function registerBuild(program) {
// No query → the playbook. Still routed through the API for the envelope.
if (!query || !String(query).trim()) {
- const result = await buildApi(undefined, {cwd: process.cwd()});
+ const result =
+ /** @type {import('../../../api/build/build.type.mjs').BuildHelpResponse} */ (
+ await buildApi(undefined, {cwd: process.cwd()})
+ );
// The playbook is a document, not a lookup: one doc, always the same
// one. Counting it as a result keeps "what did this run answer with"
// true for the no-argument form too.
@@ -100,7 +90,7 @@ export function registerBuild(program) {
jsonOut(result);
return playbook;
}
- printPlaybook(run);
+ printPlaybook(result.data);
return playbook;
}
diff --git a/packages/cli/clients/cli/commands/build.playbook.test.mjs b/packages/cli/clients/cli/commands/build.playbook.test.mjs
new file mode 100644
index 0000000000..a3abe77427
--- /dev/null
+++ b/packages/cli/clients/cli/commands/build.playbook.test.mjs
@@ -0,0 +1,75 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file The no-query `build` playbook is data. `--json` carries every step,
+ * rule and command; the text output is rendered from those values and shows
+ * nothing the envelope lacks.
+ */
+
+import {describe, it, expect} from 'vitest';
+import * as path from 'node:path';
+import {fileURLToPath} from 'node:url';
+import {runCli} from '../../../test-utils/run-cli.mjs';
+import {program} from '../index.mjs';
+
+// Run against the monorepo root so @astryxdesign/core is discoverable.
+const REPO = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../../..');
+const SLOW = 30_000;
+
+/** @param {string} s */
+const ascii = s => s.replace(/[\u2014\u2013]/g, '-');
+
+describe('build playbook: text is a projection of the JSON', () => {
+ it('prints exactly the commands, steps and rules the envelope carries', async () => {
+ const json = await runCli(['build', '--json'], REPO);
+ expect(json.status).toBe(0);
+ const {type, data} = JSON.parse(json.stdout);
+ expect(type).toBe('build.help');
+ expect(data.steps.length).toBeGreaterThan(0);
+ expect(data.rules.length).toBeGreaterThan(0);
+
+ const human = await runCli(['build'], REPO);
+ expect(human.status).toBe(0);
+ const out = human.stdout;
+
+ expect(out).toContain(ascii(data.title));
+ for (const step of data.steps) {
+ expect(out).toContain(ascii(step.title));
+ if (step.returns) expect(out).toContain(ascii(step.returns));
+ }
+ for (const rule of data.rules) expect(out).toContain(ascii(rule));
+
+ /** @type {Array<{command: string, purpose?: string}>} */
+ const commands = [
+ ...data.steps.flatMap((/** @type {any} */ s) => s.commands),
+ ...data.related,
+ ];
+ // Each command is one record whose field names are the JSON keys:
+ // `command:` (run with the caller's invocation), then `purpose:`.
+ const printed = out
+ .split('\n')
+ .filter(line => line.startsWith('command: '))
+ .map(line => line.slice('command: '.length).trim());
+ expect(printed).toHaveLength(commands.length);
+ commands.forEach(({command, purpose}, i) => {
+ expect(printed[i].endsWith(` ${command}`)).toBe(true);
+ if (purpose) expect(out).toContain(`purpose: ${ascii(purpose)}`);
+ });
+ // No column the handler drew itself.
+ expect(out).not.toMatch(/ {2,}# /);
+ }, SLOW);
+
+ it('names only commands this CLI registers', async () => {
+ const {stdout} = await runCli(['build', '--json'], REPO);
+ const {data} = JSON.parse(stdout);
+ const registered = new Set(program.commands.map(c => c.name()));
+ const commands = [
+ ...data.steps.flatMap((/** @type {any} */ s) => s.commands),
+ ...data.related,
+ ];
+ expect(commands.length).toBeGreaterThan(0);
+ for (const {command} of commands) {
+ expect(registered.has(command.split(' ')[0])).toBe(true);
+ }
+ }, SLOW);
+});
diff --git a/packages/cli/foundation/response/response-types.doc.mjs b/packages/cli/foundation/response/response-types.doc.mjs
index 5d4e36087e..f55d991db5 100644
--- a/packages/cli/foundation/response/response-types.doc.mjs
+++ b/packages/cli/foundation/response/response-types.doc.mjs
@@ -123,7 +123,7 @@ export const doc = {
{
value: 'build.help',
description:
- 'A marker (`playbook: true`) that the renderer expands into the how-to-build-a-page workflow; emitted when no query is given.',
+ 'The how-to-build-a-page playbook, emitted when no query is given: `playbook: true`, a title, the ordered steps (title, commands, optional returns), the on-system rules, and related lookups. Commands are bare subcommands for the caller to render with its own invocation.',
},
{
value: 'build.kit',