@astryxdesign/cli 0.6.4-canary.f0355e3 → 0.6.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/README.md +10 -6
  3. package/api/index.d.mts +2 -0
  4. package/api/index.mjs +2 -0
  5. package/api/json/index.ts +1 -0
  6. package/api/layout/_adapter.d.mts +34 -0
  7. package/api/layout/_adapter.mjs +148 -0
  8. package/api/layout/check/check.d.mts +16 -0
  9. package/api/layout/check/check.mjs +40 -0
  10. package/api/layout/expand/expand.d.mts +22 -0
  11. package/api/layout/expand/expand.mjs +155 -0
  12. package/api/layout/expand/expand.path-safety.test.mjs +53 -0
  13. package/api/layout/grammar/grammar.d.mts +13 -0
  14. package/api/layout/grammar/grammar.mjs +87 -0
  15. package/api/layout/layout.d.mts +6 -0
  16. package/api/layout/layout.mjs +17 -0
  17. package/api/layout/layout.test.mjs +297 -0
  18. package/api/layout/layout.type.d.mts +89 -0
  19. package/api/layout/layout.type.mjs +103 -0
  20. package/api/layout/layoutCheck.doc.d.mts +11 -0
  21. package/api/layout/layoutCheck.doc.mjs +85 -0
  22. package/api/layout/layoutExpand.doc.d.mts +11 -0
  23. package/api/layout/layoutExpand.doc.mjs +107 -0
  24. package/api/layout/layoutGrammar.doc.d.mts +11 -0
  25. package/api/layout/layoutGrammar.doc.mjs +57 -0
  26. package/api/template/template-integration.test.mjs +65 -1
  27. package/api/template/template.mjs +1 -1
  28. package/api/upgrade/run/run.mjs +1 -1
  29. package/authoring/config/config.doc.mjs +1 -1
  30. package/authoring/config/type.ts +2 -2
  31. package/authoring/doctypes/command/command.doc.mjs +1 -1
  32. package/authoring/doctypes/command/type.ts +1 -1
  33. package/clients/cli/command-result-coverage.test.mjs +7 -7
  34. package/clients/cli/commands/debug-result-summary.test.mjs +2 -2
  35. package/clients/cli/commands/layout-check.doc.mjs +65 -0
  36. package/clients/cli/commands/layout-expand.doc.mjs +83 -0
  37. package/clients/cli/commands/layout-grammar.doc.mjs +30 -0
  38. package/clients/cli/commands/layout.doc.mjs +34 -0
  39. package/clients/cli/commands/layout.error-codes.test.mjs +66 -0
  40. package/clients/cli/commands/layout.exit-parity.test.mjs +41 -0
  41. package/clients/cli/commands/layout.mjs +275 -0
  42. package/clients/cli/commands/layout.path-help.test.mjs +33 -0
  43. package/clients/cli/commands/layout.stdin-cap.test.mjs +47 -0
  44. package/clients/cli/commands/layout.text-fields.test.mjs +39 -0
  45. package/clients/cli/commands/text-json-parity.test.mjs +17 -0
  46. package/clients/cli/index.mjs +4 -0
  47. package/clients/cli/lib/exit-codes.test.mjs +8 -1
  48. package/clients/cli/lib/json-shim.test.mjs +20 -6
  49. package/clients/cli/lib/manifest.mjs +8 -0
  50. package/foundation/discovery/template-adapter.mjs +1 -1
  51. package/foundation/doc-compiler/doc-loads.test.mjs +12 -0
  52. package/foundation/response/response-types.doc.mjs +17 -0
  53. package/foundation/xle/browser.d.mts +3 -3
  54. package/foundation/xle/browser.mjs +3 -3
  55. package/foundation/xle/expand.mjs +2 -2
  56. package/foundation/xle/parse.mjs +1 -1
  57. package/foundation/xle/print.mjs +2 -2
  58. package/foundation/xle/splice.mjs +1 -1
  59. package/package.json +9 -9
package/CHANGELOG.md CHANGED
@@ -1,5 +1,61 @@
1
1
  # @xds/cli
2
2
 
3
+ # 0.6.5
4
+
5
+ #### New Features
6
+
7
+ - `astryx component` accepts several exact selectors in one call. The JSON response keeps one ordered row per selector, including missing and ambiguous components, and text mode prints every row before exiting nonzero when any lookup fails. The `component()` API accepts selector arrays and always returns `component.batch` for an array, including empty and one-item arrays. Batches accept up to 100 selectors and reject larger arrays before lookup. The public API also exports shared `BatchResponse` and `BatchRow` types for typed receipts.
8
+ - `astryx discover` browses integrations: the ones a project has and, through discover sources, the ones it could add, with every version and what each one adds. It searches every kind of item and filters with `--type`, `--installed`, `--available`, and `--limit`. A project sets a source as `discover` in `astryx.config`, and an integration exports one as a `discover` named export. Discover only reads: it prints the command that adds a package and never runs it. Existing `--json` fields keep their meaning. A free-text query now always lists its matches, even an exact component name, and `astryx discover <package>/<Name>` opens one.
9
+ - `astryx integration verify` is the new name of `astryx integration pack --check`.
10
+ The check you run before publishing an integration now has a name that says what it does. `astryx integration verify` packs the package with npm, installs the tarball into a temporary app, and checks that the app sees the same components, templates, themes, docs, and codemods. It takes no flags. `astryx integration pack --check` still works as a deprecated alias: it runs the same check with the same output, JSON, and exit codes, prints a note that names `integration verify`, and shows as deprecated in help. It will be removed in a later release. The `integrationPackCheck()` API and its `integration.pack-check` JSON response do not change. With `--json`, a command group given an unknown subcommand now reports `ERR_UNKNOWN_SUBCOMMAND` and lists its subcommands, where it used to say JSON output is not supported.
11
+
12
+ #### Fixes
13
+
14
+ - Fix the CLI's topic docs and how they print.
15
+ - `astryx doctor` no longer reports an integration it could not check as absent or complete (#6619)
16
+ - `upgrade`'s `filesChanged` counts files, not (codemod, file) pairs (#6622)
17
+ One source file that four codemods each changed was reported as four files changed, so `filesChanged` matched `transformsApplied` and the documented meaning, "Total files changed", was not true. The human summary said the same thing: "Found 4 changes across 4 files" for one file.
18
+
19
+ `filesChanged` is now the count of distinct files. `transformsApplied` is unchanged: a code or config codemod counts once for each file it changed, and a project codemod counts once. A file that both a core codemod and an integration codemod changed counts once in `filesChanged`.
20
+ - A parse error prints the Astryx error format in text mode.
21
+ `astryx theme list --lang zh-Hans` printed Commander's own line — `error: option '--lang <locale>' argument 'zh-Hans' is invalid…` — while every other CLI error prints `Error: …`. `--json` was already correct (`ERR_INVALID_LANG`), so the two modes agreed only on the exit code.
22
+
23
+ Commander writes that line before any Astryx code runs, so the JSON shim — the one place that already sees every parse failure — now suppresses it and writes the Astryx line itself, from the same message, for both modes. Every parse failure is covered: unknown option, unknown command, missing argument, and an invalid value for a global option. `--help` and `--version` are untouched and still exit 0.
24
+ - The CLI reference now matches what the commands do. Every `--help` ends with the command's examples and a `More:` line that names its full docs page. Function docs show each parameter's default, mark required parameters, list the error codes each function throws, and use examples that run. The response-type list adds `help`, `version`, and `upgrade.registry`, and `astryx manifest` now lists `upgrade.registry` for `upgrade`. The `--zh`, `--dense`, `--lang`, and `--detail` descriptions name the commands they change, and command summaries say when to use each command. When `astryx template` refuses to overwrite a file, it now says to re-run with `--overwrite` (or `-f`). The `upgrade` command page (`astryx docs cli/commands/upgrade`) now explains which files codemods never edit, what happens when one of them needs a change, and how to regenerate it.
25
+ - `astryx integration pack --check` now checks the tarball when a `prepack`, `prepare`, or `postpack` script prints to stdout. Before, any lifecycle output made the check fail with "npm pack produced unparseable JSON output" before it looked at the tarball. A failing lifecycle script still fails the check, and its output stays in the `pack_failed` message.
26
+ - `astryx doctor integration docs` fails when a namespace doc or a placement fails, as its help says.
27
+ Such a failure hides the doc from the docs tree, so it now exits 1 with an `invalid_doc_graph` error instead of a warning. A link that names no doc still only warns, since it prints as written. `doctor integration docs` and `doctor integration components` also no longer print an `[ok]` line after a check that failed.
28
+
29
+ A mistyped subcommand under `doctor` now fails and lists the subcommands the group has: `astryx doctor integrations` used to run the project checks, and `astryx doctor integration bogus` exited 0 in text though it exited 1 with `--json`.
30
+ - A package that ships a theme, or a doc section with an `id`, now declares the CLI that can read it.
31
+ A stable CLI before 0.7.0 rejects both: it cannot read the typed theme descriptors that `astryx integration add theme` writes, and it rejects a section `id`. Either way it hides the package's themes or doc topics with no warning. `astryx integration add theme` now adds `"@astryxdesign/cli": ">=0.7.0"` to `peerDependencies`, marked optional, and `astryx integration verify` fails with `themes_need_cli` or `section_ids_need_cli` when a package needs that peer range and does not declare it.
32
+ - `astryx integration verify` resolves every public import in the packed package, not in your source folder.
33
+ Before, its temporary app resolved your package's own name through the source `package.json`, so an `exports` target left out of the tarball still passed. It now fails with `component_export_missing`, as an app that installs the tarball would.
34
+ - `astryx theme add` and `astryx theme build` now undo a failed write completely. Before, when one file failed to write after others were written, the written files kept their new content. Now every replaced file gets its previous content back, every new file is removed, and the error names any file that could not be restored. Both commands also refuse to replace a destination that is a symbolic link. (#6852)
35
+
36
+ #### Other Changes
37
+
38
+ - A code block's label now prints above the block instead of as a `// label` line inside it, so copied bash, CSS, JSON, and HTML stay valid. Table cells escape `|`, so a union type stays in one column.
39
+ - `astryx search dark mode` searches for both words; it used to drop every word after the first. A result that matches every word of a query, one of them by name or keyword, now outranks one that matches only some, and a section whose title or heading holds the whole query ranks near the top. Topics can declare search `keywords`, now a documented ReferenceDoc field, and a namespace's `keywords` now count too. A query keeps its phrase when common words such as `make`, `build`, or `an` drop out, so `astryx search make an integration` finds the integration guides, a plural of a doc's name matches it, one step below the exact name, and a component's name typed as words, such as `command palette`, finds the component. Outside an app, where `@astryxdesign/core` is not installed, `astryx search` searches the docs instead of failing, and says so; `--type component`, `hook`, or `template` still needs Core.
40
+ - Snippets that failed when copied now work: StyleX token imports, the `fr-FR.json` locale path, Tailwind `rounded-lg`, `--color-background-muted`, icon and color values, and the Cursor rule path.
41
+ - Claims that did not match the code are corrected: the 30 shipped locales and how RTL mirroring works, what `astryx init` writes, `--detail brief` for a shorter read, the Neutral and Matcha fonts, the components that need anchor positioning, `gap` steps, Card's radius, and the Next.js StyleX example. The deprecated bare classes are still emitted and will be removed in a later release.
42
+ - `astryx docs tokens` lists all 258 tokens, adding the data visualization and syntax groups, and shows both halves of every `light-dark()` value.
43
+ - Long sections are split, vague titles renamed, and the `--dense` and Chinese versions no longer drop blocks. Eleven long section keys are shorter, such as `astryx docs styling stylex-setup`, and every old key still resolves. An integration section that extends a Core topic by a section's old title still replaces that section.
44
+ - `astryx integration add codemod --to` help says it takes the Core version whose upgrade runs the codemod.
45
+ - The agent block that `astryx init` writes now says `upgrade --from <old version> --apply`; `upgrade --apply` alone stops with "Missing required --from".
46
+ - The contributor-only sections, on adding a semantic icon and on strings and text direction inside components, moved to CONTRIBUTING.md.
47
+ - An installed dependency whose `astryx.integration.*` manifest cannot be loaded is still kept out of the loaded set, but `implicit-integrations` now names it and says it contributes nothing. Before, doctor said that no installed dependency ships a manifest. The check stays informational, and `astryx doctor integration validate <package>` gives the details.
48
+ - `implicit-integrations` lists only the roots that exist on disk. A package whose declared roots are missing is reported as contributing nothing, with the missing roots named. Before, it listed every root the manifest declared.
49
+ - `provider-identity` says how many loaded integrations it could not read, instead of counting only the readable ones.
50
+
51
+ #### Contributors
52
+
53
+ Thanks to everyone who contributed to this release:
54
+
55
+ - @josephfarina
56
+
57
+ ---
58
+
3
59
  # 0.6.4
4
60
 
5
61
  #### New Features
package/README.md CHANGED
@@ -90,6 +90,7 @@ Options:
90
90
  | `hook` | List hooks or print hook docs |
91
91
  | `init` | Initialize the design system in your project |
92
92
  | `integration` | Author and verify an Astryx integration package |
93
+ | `layout` | Generate XDS layouts from compressed expressions (XLE/XLO) |
93
94
  | `search` | Search components, hooks, docs, and templates in one ranked list |
94
95
  | `swizzle` | Copy component source for customization |
95
96
  | `template` | List, show, or scaffold page and block templates |
@@ -478,6 +479,9 @@ Every response has a `type` discriminant. The full set is below (generated from
478
479
  | `integration.template-conflicts` | The integration identity, structural issues, and non-blocking Core template-id conflicts as {id, severity: warning, integrationPackage, integrationType, integrationName, coreMatches, message, command}. |
479
480
  | `integration.component-conflicts` | The integration identity, structural issues, and non-blocking conflicts where an integration component name is also owned by Core; each conflict includes the exact package-qualified command. |
480
481
  | `integration.doc-conflicts` | The integration identity, structural issues, and Core doc overlaps. Each finding includes `severity` (`info` \| `error`) and `relationship` (`replaces` \| `extends` \| `accidental`). |
482
+ | `layout.expand` | The expansion: parsed form, generated TSX code, componentsUsed, states (count of useState hooks scaffolded), todos, blocksReferenced (each {name, mode}), warnings, and written (the output path, or null when nothing was written). |
483
+ | `layout.check` | The validation result: a valid flag, the detected form, errors (each with line/col, message, formatted text, and suggestions), warnings, and the expression re-printed in both canonical surfaces (compact and outline). |
484
+ | `layout.grammar` | The XLE/XLO grammar cheatsheet: a text field with the full reference plus an aliases map (short name → canonical component) generated from this install's registry. |
481
485
 
482
486
  <!-- END GENERATED: response-types -->
483
487
  <!-- Generated by scripts/generate-cli-readme.mjs from the response-types EnumDoc. Run `pnpm -F @astryxdesign/cli readme`. -->
@@ -578,12 +582,12 @@ There is no factory: write a plain object. For editor autocomplete and
578
582
  type-checking, annotate it with the `AstryxConfig` type exported from
579
583
  `@astryxdesign/cli/authoring`.
580
584
 
581
- | Field | Type | Purpose |
582
- | ----------------------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------ |
583
- | `integrations` | `string[]` | Integration package names to load (see [Integrations](#integrations)). |
584
- | `issuesUrl` | `string` | Where "report an issue" links point for your project. Defaults to the core issue tracker. |
585
- | `hooks.postCodemod` | `PostCodemodHook[]` | Commands to run after `astryx upgrade` applies codemods (e.g. reinstall, rebuild, reformat). |
586
- | `experimental.xle.components` | `Record<string, XleComponent>` | No effect. Its only reader was the removed `layout` command. Still accepted so existing configs keep loading; delete it. |
585
+ | Field | Type | Purpose |
586
+ | ----------------------------- | ------------------------------ | ----------------------------------------------------------------------------------------------- |
587
+ | `integrations` | `string[]` | Integration package names to load (see [Integrations](#integrations)). |
588
+ | `issuesUrl` | `string` | Where "report an issue" links point for your project. Defaults to the core issue tracker. |
589
+ | `hooks.postCodemod` | `PostCodemodHook[]` | Commands to run after `astryx upgrade` applies codemods (e.g. reinstall, rebuild, reformat). |
590
+ | `experimental.xle.components` | `Record<string, XleComponent>` | Register app-local components so layout (XLE) expressions can reference them by name. Unstable. |
587
591
 
588
592
  The config is validated against a strict schema when the CLI loads it, so an
589
593
  unknown field is a hard error rather than a silent no-op. `astryx doctor`
package/api/index.d.mts CHANGED
@@ -33,12 +33,14 @@ export * from "./gap-report/gap-report.type.mjs";
33
33
  export * from "./upgrade/upgrade.type.mjs";
34
34
  export * from "./init/init.type.mjs";
35
35
  export * from "./doctor/doctor.type.mjs";
36
+ export * from "./layout/layout.type.mjs";
36
37
  export * from "./integration/integration-authoring.type.mjs";
37
38
  export * from "./integration/pack-check.type.mjs";
38
39
  export * from "./integration/validate-integration.type.mjs";
39
40
  export * from "./integration/authoring-checks.type.mjs";
40
41
  export type Logger = import("./logger.mjs").Logger;
41
42
  export { themeBuild, themeAdd, themeTemplate, themeList, themeListAvailable, themeTargets, themePaletteGenerate, generateTonalPalette, listThemes } from "./theme/theme.mjs";
43
+ export { layoutExpand, layoutCheck, layoutGrammar } from "./layout/layout.mjs";
42
44
  export { integrationAdd, integrationAddAgentDoc, integrationAddCodemod, integrationAddComponent, integrationAddDoc, integrationAddTemplate } from "./integration/add-contribution.mjs";
43
45
  export { validateIntegration, summarizeIssues } from "./integration/validate-integration.mjs";
44
46
  export { integrationTemplateConflicts, integrationComponentConflicts, integrationDocConflicts } from "./integration/authoring-checks.mjs";
package/api/index.mjs CHANGED
@@ -44,6 +44,7 @@ export {gapReport} from './gap-report/gap-report.mjs';
44
44
  export {upgrade} from './upgrade/upgrade.mjs';
45
45
  export {init} from './init/init.mjs';
46
46
  export {doctor} from './doctor/doctor.mjs';
47
+ export {layoutExpand, layoutCheck, layoutGrammar} from './layout/layout.mjs';
47
48
  export {
48
49
  integrationAdd,
49
50
  integrationAddAgentDoc,
@@ -92,6 +93,7 @@ export * from './gap-report/gap-report.type.mjs';
92
93
  export * from './upgrade/upgrade.type.mjs';
93
94
  export * from './init/init.type.mjs';
94
95
  export * from './doctor/doctor.type.mjs';
96
+ export * from './layout/layout.type.mjs';
95
97
  export * from './integration/integration-authoring.type.mjs';
96
98
  export * from './integration/pack-check.type.mjs';
97
99
  export * from './integration/validate-integration.type.mjs';
package/api/json/index.ts CHANGED
@@ -26,6 +26,7 @@ export type * from '../gap-report/gap-report.type.mjs';
26
26
  export type * from '../upgrade/upgrade.type.mjs';
27
27
  export type * from '../init/init.type.mjs';
28
28
  export type * from '../doctor/doctor.type.mjs';
29
+ export type * from '../layout/layout.type.mjs';
29
30
  export type * from '../integration/validate-integration.type.mjs';
30
31
  export type * from '../integration/authoring-checks.type.mjs';
31
32
  export type * from '../integration/pack-check.type.mjs';
@@ -0,0 +1,34 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /** @param {import('../../foundation/xle/xle-ast').RawIssue} issue */
5
+ export function formatIssue(issue: import("../../foundation/xle/xle-ast").RawIssue): string;
6
+ /**
7
+ * Parse + validate, throwing structured XDSErrors on failure.
8
+ * Returns {doc, registry, blocks, warnings}.
9
+ *
10
+ * @param {string} expression
11
+ * @param {{form?: 'compact'|'outline'|'auto', loose?: boolean, cwd?: string}} [options]
12
+ */
13
+ export function analyze(expression: string, { form, loose, cwd }?: {
14
+ form?: "compact" | "outline" | "auto";
15
+ loose?: boolean;
16
+ cwd?: string;
17
+ }): Promise<{
18
+ doc: import("../../foundation/xle/xle-ast").XLEDoc;
19
+ registry: import("../../foundation/xle/xle-ast").Registry;
20
+ blocks: LayoutBlock[];
21
+ errors: import("../../foundation/xle/xle-ast").RawIssue[];
22
+ warnings: import("../../foundation/xle/xle-ast").RawIssue[];
23
+ }>;
24
+ export type LayoutBlock = {
25
+ dirName: string;
26
+ name: string;
27
+ kind: "template" | "component";
28
+ type?: string | undefined;
29
+ description?: string | undefined;
30
+ category?: string | undefined;
31
+ importPath?: string | undefined;
32
+ filePath?: string | undefined;
33
+ isDefault?: boolean | undefined;
34
+ };
@@ -0,0 +1,148 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Shared analysis layer for the `layout` command's expand/check leaves.
5
+ *
6
+ * Loads the block catalog (template blocks + app-registered components), builds
7
+ * the branch registry, and parses+validates a layout expression into a doc +
8
+ * issues. Both the expand and check leaves sit on `analyze`; neither parses or
9
+ * validates directly. (The grammar leaf needs none of this — it only reads the
10
+ * registry alias table.)
11
+ *
12
+ * @input expression string (+ options)
13
+ * @output analyze() -> {doc, registry, blocks, errors, warnings}; formatIssue()
14
+ * @position api — shared core over lib/xle; leaves in expand/ + check/ project it
15
+ */
16
+
17
+ import {AstryxError} from '../error.mjs';
18
+ import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
19
+ import {parse, XLEParseError} from '../../foundation/xle/parse.mjs';
20
+ import {validate} from '../../foundation/xle/validate.mjs';
21
+ import {buildRegistry} from '../../foundation/xle/registry.mjs';
22
+ import {discoverTemplates} from '../template/template.mjs';
23
+ import {Project} from '../../foundation/config/project.mjs';
24
+
25
+ /**
26
+ * @typedef {object} LayoutBlock
27
+ * @property {string} dirName
28
+ * @property {string} name
29
+ * @property {'template'|'component'} kind
30
+ * @property {string} [type]
31
+ * @property {string} [description]
32
+ * @property {string} [category]
33
+ * @property {string} [importPath]
34
+ * @property {string} [filePath]
35
+ * @property {boolean} [isDefault]
36
+ */
37
+
38
+ /**
39
+ * The catalog a `{hint}` can resolve to: template blocks (spliced inline) plus
40
+ * any app-registered local components from astryx.config.mjs
41
+ * `experimental.xle.components` (imported by name). App components are how XLE
42
+ * reaches domain pieces — the KpiCard/chart/drawer set that the
43
+ * @astryxdesign/core registry can't see.
44
+ *
45
+ * @param {string} cwd
46
+ * @returns {Promise<LayoutBlock[]>}
47
+ */
48
+ async function loadBlocks(cwd) {
49
+ /** @type {LayoutBlock[]} */
50
+ const blocks = [];
51
+ try {
52
+ const all = await discoverTemplates(cwd);
53
+ const templates = all.filter(template => template.type === 'block');
54
+ /** @type {Map<string, import('../../foundation/discovery/template-adapter.mjs').DiscoveredTemplate>} */
55
+ const byId = new Map();
56
+ // Exact ids are the fallback. Active replacement aliases override them in a
57
+ // second pass, matching template() regardless of discovery/display order.
58
+ for (const template of templates) byId.set(template.dirName, template);
59
+ for (const template of templates) {
60
+ if (template.replaces != null) byId.set(template.replaces, template);
61
+ }
62
+ for (const [id, template] of byId) {
63
+ blocks.push({...template, dirName: id, kind: 'template'});
64
+ }
65
+ } catch {
66
+ // discovery is best-effort
67
+ }
68
+ try {
69
+ const project = await Project.load(cwd);
70
+ /** @type {Record<string, {from?: string, description?: string, default?: boolean}>} */
71
+ const components = project.config.experimental?.xle?.components ?? {};
72
+ for (const [name, spec] of Object.entries(components)) {
73
+ const importPath = spec.from;
74
+ if (!importPath) continue;
75
+ blocks.push({
76
+ type: 'block',
77
+ kind: 'component',
78
+ dirName: name,
79
+ name,
80
+ description: spec.description ?? '',
81
+ category: 'app',
82
+ importPath,
83
+ isDefault: Boolean(spec.default),
84
+ });
85
+ }
86
+ } catch {
87
+ // config is optional
88
+ }
89
+ return blocks;
90
+ }
91
+
92
+ /** @param {import('../../foundation/xle/xle-ast').RawIssue} issue */
93
+ export function formatIssue(issue) {
94
+ const where = issue.line != null ? `line ${issue.line}: ` : '';
95
+ return `${where}${issue.message}`;
96
+ }
97
+
98
+ /**
99
+ * Parse + validate, throwing structured XDSErrors on failure.
100
+ * Returns {doc, registry, blocks, warnings}.
101
+ *
102
+ * @param {string} expression
103
+ * @param {{form?: 'compact'|'outline'|'auto', loose?: boolean, cwd?: string}} [options]
104
+ */
105
+ export async function analyze(
106
+ expression,
107
+ {form = 'auto', loose = false, cwd = process.cwd()} = {},
108
+ ) {
109
+ // Validate inputs in the API (not just the CLI): an empty expression or an
110
+ // unknown --form must error, not silently parse as an empty/compact layout.
111
+ if (typeof expression !== 'string' || expression.trim() === '') {
112
+ throw new AstryxError(
113
+ 'Layout expression is empty.',
114
+ undefined,
115
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
116
+ );
117
+ }
118
+ if (form !== 'compact' && form !== 'outline' && form !== 'auto') {
119
+ throw new AstryxError(
120
+ `Invalid form "${form}". Must be one of: compact, outline, auto.`,
121
+ undefined,
122
+ ERROR_CODES.ERR_INVALID_OPTION,
123
+ );
124
+ }
125
+ const registry =
126
+ /** @type {import('../../foundation/xle/xle-ast').Registry} */ (
127
+ /** @type {unknown} */ (await buildRegistry({cwd}))
128
+ );
129
+ const blocks = await loadBlocks(cwd);
130
+
131
+ /** @type {import('../../foundation/xle/xle-ast').XLEDoc} */
132
+ let doc;
133
+ try {
134
+ doc = parse(expression, {form});
135
+ } catch (e) {
136
+ if (e instanceof XLEParseError) {
137
+ throw new AstryxError(
138
+ `Layout expression syntax error at line ${e.line}, col ${e.col}: ${e.message}`,
139
+ undefined,
140
+ ERROR_CODES.ERR_LAYOUT_PARSE,
141
+ );
142
+ }
143
+ throw e;
144
+ }
145
+
146
+ const {errors, warnings} = validate(doc, registry, blocks, {loose});
147
+ return {doc, registry, blocks, errors, warnings};
148
+ }
@@ -0,0 +1,16 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * `astryx layout check "<expr>" [--form compact|outline]`
6
+ * Validates without expanding; echoes both canonical surfaces.
7
+ *
8
+ * @param {string} expression
9
+ * @param {{form?: 'compact'|'outline'|'auto', loose?: boolean, cwd?: string}} [options]
10
+ * @returns {Promise<import('../layout.type.mjs').LayoutCheckResponse>}
11
+ */
12
+ export function layoutCheck(expression: string, options?: {
13
+ form?: "compact" | "outline" | "auto";
14
+ loose?: boolean;
15
+ cwd?: string;
16
+ }): Promise<import("../layout.type.mjs").LayoutCheckResponse>;
@@ -0,0 +1,40 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file `astryx layout check` leaf — validate a layout expression without
5
+ * expanding, echoing both canonical surfaces (compact + outline). Resolution +
6
+ * validation come from ../_adapter.mjs (analyze); this leaf projects the
7
+ * `layout.check` envelope.
8
+ *
9
+ * @input expression string (+ options)
10
+ * @output {type:'layout.check', data}
11
+ * @position api — leaf over ../_adapter.mjs + lib/xle/print
12
+ */
13
+
14
+ import {toCompact, toOutline} from '../../../foundation/xle/print.mjs';
15
+ import {analyze, formatIssue} from '../_adapter.mjs';
16
+
17
+ /**
18
+ * `astryx layout check "<expr>" [--form compact|outline]`
19
+ * Validates without expanding; echoes both canonical surfaces.
20
+ *
21
+ * @param {string} expression
22
+ * @param {{form?: 'compact'|'outline'|'auto', loose?: boolean, cwd?: string}} [options]
23
+ * @returns {Promise<import('../layout.type.mjs').LayoutCheckResponse>}
24
+ */
25
+ export async function layoutCheck(expression, options = {}) {
26
+ const {form = 'auto', loose = false, cwd = process.cwd()} = options;
27
+ const {doc, errors, warnings} = await analyze(expression, {form, loose, cwd});
28
+
29
+ return {
30
+ type: 'layout.check',
31
+ data: {
32
+ valid: errors.length === 0,
33
+ form: doc.form,
34
+ errors: errors.map(e => ({...e, formatted: formatIssue(e)})),
35
+ warnings: warnings.map(formatIssue),
36
+ compact: toCompact(doc),
37
+ outline: toOutline(doc),
38
+ },
39
+ };
40
+ }
@@ -0,0 +1,22 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * `astryx layout expand "<expr>" [path]`
6
+ *
7
+ * @param {string} expression
8
+ * @param {object} [options]
9
+ * @param {string} [options.targetPath] - write TSX here (validated against cwd)
10
+ * @param {'compact'|'outline'|'auto'} [options.form]
11
+ * @param {boolean} [options.loose] - downgrade unknown {hints} to TODO warnings
12
+ * @param {string} [options.name] - generated component name
13
+ * @param {string} [options.cwd]
14
+ * @returns {Promise<import('../layout.type.mjs').LayoutExpandResponse>}
15
+ */
16
+ export function layoutExpand(expression: string, options?: {
17
+ targetPath?: string | undefined;
18
+ form?: "compact" | "auto" | "outline" | undefined;
19
+ loose?: boolean | undefined;
20
+ name?: string | undefined;
21
+ cwd?: string | undefined;
22
+ }): Promise<import("../layout.type.mjs").LayoutExpandResponse>;
@@ -0,0 +1,155 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file `astryx layout expand` leaf — expand a validated layout expression into
5
+ * XDS TSX (optionally written to disk). Resolution + validation come from
6
+ * ../_adapter.mjs (analyze); this leaf owns block-module assembly, code
7
+ * generation, and the path-safe write, and projects the `layout.expand` envelope.
8
+ *
9
+ * @input expression string (+ options)
10
+ * @output {type:'layout.expand', data}
11
+ * @position api — leaf over ../_adapter.mjs + lib/xle/expand
12
+ */
13
+
14
+ import * as fs from 'node:fs';
15
+ import * as path from 'node:path';
16
+ import {AstryxError} from '../../error.mjs';
17
+ import {ERROR_CODES} from '../../../foundation/response/error-codes.mjs';
18
+ import {assertWithin, isFilePathArg, PathSafetyError} from '../../../foundation/fs/path-safety.mjs';
19
+ import {detectForm} from '../../../foundation/xle/parse.mjs';
20
+ import {expand} from '../../../foundation/xle/expand.mjs';
21
+ import {stripTemplateAssetRefs} from '../../template/template.mjs';
22
+ import {analyze, formatIssue} from '../_adapter.mjs';
23
+
24
+ /** @param {string} name */
25
+ const normKey = (name) => name.toLowerCase().replace(/[^a-z0-9]/g, '');
26
+
27
+ /**
28
+ * Collect the block names referenced by {hints} anywhere in the doc.
29
+ * @param {import('../../../foundation/xle/xle-ast').XLEDoc} doc
30
+ */
31
+ function collectHintNames(doc) {
32
+ /** @type {Set<string>} */
33
+ const names = new Set();
34
+ /** @param {import('../../../foundation/xle/xle-ast').XLEItem} node */
35
+ const visitNode = (node) => {
36
+ if (!node || node.kind === 'group') {
37
+ (node?.children || []).forEach(visit);
38
+ return;
39
+ }
40
+ if (node.hint?.block) names.add(node.hint.block.name);
41
+ for (const slot of node.slots || []) {
42
+ const value = slot.value;
43
+ if (value && typeof value === 'object' && 'hint' in value && value.hint?.block) {
44
+ names.add(value.hint.block.name);
45
+ }
46
+ if (value && typeof value === 'object' && 'subexpr' in value) {
47
+ (value.subexpr || []).forEach(visit);
48
+ }
49
+ }
50
+ (node.children || []).forEach(visit);
51
+ };
52
+ /** @param {import('../../../foundation/xle/xle-ast').XLEItem} item */
53
+ const visit = (item) => (item?.kind === 'group' ? item.children.forEach(visit) : visitNode(item));
54
+ doc.roots.forEach(visit);
55
+ doc.overlays.forEach(visit);
56
+ return names;
57
+ }
58
+
59
+ /**
60
+ * Build the blockModules map expand() needs: import-mode for app components,
61
+ * splice-mode (reading + asset-stripping the block source) for template blocks.
62
+ * Only blocks actually referenced are read.
63
+ *
64
+ * @param {import('../../../foundation/xle/xle-ast').XLEDoc} doc
65
+ * @param {import('../_adapter.mjs').LayoutBlock[]} blocks
66
+ * @returns {Map<string, import('../../../foundation/xle/xle-ast').BlockModule>}
67
+ */
68
+ function buildBlockModules(doc, blocks) {
69
+ const referenced = collectHintNames(doc);
70
+ if (referenced.size === 0) return new Map();
71
+ const byKey = new Map(blocks.map(b => [normKey(b.dirName), b]));
72
+ /** @type {Map<string, import('../../../foundation/xle/xle-ast').BlockModule>} */
73
+ const modules = new Map();
74
+ for (const name of referenced) {
75
+ const block = byKey.get(normKey(name));
76
+ if (!block) continue;
77
+ if (block.kind === 'component') {
78
+ modules.set(name, /** @type {import('../../../foundation/xle/xle-ast').BlockModule} */ (/** @type {unknown} */ ({mode: 'import', componentName: block.name, importPath: block.importPath, isDefault: block.isDefault})));
79
+ } else if (block.filePath && fs.existsSync(block.filePath)) {
80
+ modules.set(name, /** @type {import('../../../foundation/xle/xle-ast').BlockModule} */ ({mode: 'splice', componentName: block.dirName, source: stripTemplateAssetRefs(fs.readFileSync(block.filePath, 'utf-8'))}));
81
+ }
82
+ }
83
+ return modules;
84
+ }
85
+
86
+ /**
87
+ * `astryx layout expand "<expr>" [path]`
88
+ *
89
+ * @param {string} expression
90
+ * @param {object} [options]
91
+ * @param {string} [options.targetPath] - write TSX here (validated against cwd)
92
+ * @param {'compact'|'outline'|'auto'} [options.form]
93
+ * @param {boolean} [options.loose] - downgrade unknown {hints} to TODO warnings
94
+ * @param {string} [options.name] - generated component name
95
+ * @param {string} [options.cwd]
96
+ * @returns {Promise<import('../layout.type.mjs').LayoutExpandResponse>}
97
+ */
98
+ export async function layoutExpand(expression, options = {}) {
99
+ const {targetPath, form = 'auto', loose = false, name, cwd = process.cwd()} = options;
100
+ const {doc, registry, blocks, errors, warnings} = await analyze(expression, {form, loose, cwd});
101
+
102
+ if (errors.length > 0) {
103
+ throw new AstryxError(
104
+ `Layout expression is invalid:\n` + errors.map(e => ` - ${formatIssue(e)}`).join('\n'),
105
+ errors.flatMap(e => (e.suggestions || []).map(s => ({name: s, reason: 'did you mean this?'}))),
106
+ ERROR_CODES.ERR_LAYOUT_INVALID,
107
+ );
108
+ }
109
+
110
+ const componentName = name || 'GeneratedLayout';
111
+ if (!/^[A-Z][A-Za-z0-9]*$/.test(componentName)) {
112
+ throw new AstryxError(
113
+ `--name must be a PascalCase component name, got '${componentName}'`,
114
+ undefined,
115
+ ERROR_CODES.ERR_INVALID_ARGUMENT,
116
+ );
117
+ }
118
+ const blockModules = buildBlockModules(doc, blocks);
119
+ const result = expand(doc, registry, {componentName, blockModules});
120
+
121
+ let written = null;
122
+ if (targetPath) {
123
+ // The guard sees the file that will be written, not only its directory:
124
+ // a symlink at that name would otherwise carry the write outside the root.
125
+ const fileTarget = isFilePathArg(targetPath)
126
+ ? targetPath
127
+ : path.join(targetPath, `${componentName}.tsx`);
128
+ let filePath;
129
+ try {
130
+ filePath = assertWithin(fileTarget, cwd, {label: 'layout target path'});
131
+ } catch (err) {
132
+ if (err instanceof PathSafetyError) {
133
+ throw new AstryxError(err.message, undefined, ERROR_CODES.ERR_PATH_TRAVERSAL);
134
+ }
135
+ throw err;
136
+ }
137
+ fs.mkdirSync(path.dirname(filePath), {recursive: true});
138
+ fs.writeFileSync(filePath, result.code);
139
+ written = path.relative(cwd, filePath);
140
+ }
141
+
142
+ return {
143
+ type: 'layout.expand',
144
+ data: {
145
+ form: form === 'auto' ? detectForm(expression) : form,
146
+ code: result.code,
147
+ componentsUsed: result.componentsUsed,
148
+ states: result.states,
149
+ todos: result.todos,
150
+ blocksReferenced: [...blockModules.entries()].map(([name, m]) => ({name, mode: m.mode})),
151
+ warnings: warnings.map(formatIssue),
152
+ written,
153
+ },
154
+ };
155
+ }
@@ -0,0 +1,53 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Path-safety tests for the `layout.expand` write. The file written, not
5
+ * only the directory it lands in, must stay inside the project root.
6
+ */
7
+
8
+ import {describe, it, expect, beforeAll} from 'vitest';
9
+ import * as fs from 'node:fs';
10
+ import * as os from 'node:os';
11
+ import * as path from 'node:path';
12
+ import {layoutExpand} from '../layout.mjs';
13
+ import {buildRegistry} from '../../../foundation/xle/registry.mjs';
14
+
15
+ const SLOW = 30_000;
16
+
17
+ // The registry imports every component doc on first use; warm it once.
18
+ beforeAll(async () => {
19
+ await buildRegistry();
20
+ }, 120_000);
21
+
22
+ describe('layout.expand — write confinement', () => {
23
+ it('rejects a directory target whose generated file is a symlink leading outside cwd', async () => {
24
+ // Inside the workspace so @astryxdesign/core resolves.
25
+ const cwd = fs.mkdtempSync(path.join(process.cwd(), '.xle-confine-test-'));
26
+ const outside = fs.mkdtempSync(path.join(os.tmpdir(), 'xle-confine-outside-'));
27
+ try {
28
+ const victim = path.join(outside, 'victim.tsx');
29
+ fs.writeFileSync(victim, 'OUTSIDE');
30
+ fs.mkdirSync(path.join(cwd, 'out'));
31
+ fs.symlinkSync(victim, path.join(cwd, 'out', 'GeneratedLayout.tsx'));
32
+
33
+ await expect(
34
+ layoutExpand('V > B', {targetPath: './out', cwd}),
35
+ ).rejects.toMatchObject({code: 'ERR_PATH_TRAVERSAL'});
36
+ expect(fs.readFileSync(victim, 'utf-8')).toBe('OUTSIDE');
37
+ } finally {
38
+ fs.rmSync(cwd, {recursive: true, force: true});
39
+ fs.rmSync(outside, {recursive: true, force: true});
40
+ }
41
+ }, SLOW);
42
+
43
+ it('still writes <Name>.tsx into a plain directory target', async () => {
44
+ const cwd = fs.mkdtempSync(path.join(process.cwd(), '.xle-confine-test-'));
45
+ try {
46
+ const result = await layoutExpand('V > B', {targetPath: './out', name: 'Demo', cwd});
47
+ expect(result.data.written).toBe(path.join('out', 'Demo.tsx'));
48
+ expect(fs.existsSync(path.join(cwd, 'out', 'Demo.tsx'))).toBe(true);
49
+ } finally {
50
+ fs.rmSync(cwd, {recursive: true, force: true});
51
+ }
52
+ }, SLOW);
53
+ });
@@ -0,0 +1,13 @@
1
+ // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
+ // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
+
4
+ /**
5
+ * `astryx layout grammar` — the agent cheatsheet, with the alias table
6
+ * generated from this branch's registry (never hand-maintained).
7
+ *
8
+ * @param {{cwd?: string}} [options]
9
+ * @returns {Promise<import('../layout.type.mjs').LayoutGrammarResponse>}
10
+ */
11
+ export function layoutGrammar(options?: {
12
+ cwd?: string;
13
+ }): Promise<import("../layout.type.mjs").LayoutGrammarResponse>;