Skip to content

fix(cli): validate --lang, fix --detail level inversion, expose global flags in subcommand help - #2407

Closed
josephfarina wants to merge 4 commits into
mainfrom
fix/cli-lang-detail-help
Closed

josephfarina wants to merge 4 commits into
mainfrom
fix/cli-lang-detail-help

Conversation

@josephfarina

Copy link
Copy Markdown
Contributor

Fixes four CLI usability issues surfaced by recent agent-facing audits:

1. `--lang` was silently lying

`--lang fr`, `--lang ja`, `--lang zh` all returned English. The flag is advertised in `--help` but had no validator, so unsupported locales were silently swallowed and `zh` was only partially translated (props translated, prose not).

Fix:

  • Validate against the supported set: `en`, `zh`, `dense`. Anything else → exit 1 with a clear message (or JSON error envelope when `--json`).
  • Reject combining `--lang` with the legacy `--zh` / `--dense` shortcuts (was previously ambiguous).
  • Warn on stderr when `--lang zh` is requested:
    ```
    Warning: --lang zh translations are incomplete. Some prose will fall back to English.
    ```

Follow-up (not in this PR): flesh out `docsZh` coverage on every `.doc.mjs` so prose (descriptions, best practices, anatomy) is actually translated. Today `docsZh` exists but most files only translate prop tables. Once coverage is complete, drop the warning and remove `PARTIAL_LANGS` in `packages/cli/src/lib/lang.mjs`.

2. `--detail` levels were inverted on list views

Before:

Command bytes
`xds component --list --detail brief` 66,952
`xds component --list --detail compact` 17,430
`xds component --list --detail full` 2,538

So `brief` was 26× larger than `full` — exactly backwards.

After:

Command bytes
`xds component --list --detail brief` 2,538
`xds component --list --detail compact` 17,430
`xds component --list --detail full` 66,952

Same fix applied to `xds hook --list`. Detail levels now follow the documented `brief < compact < full` ordering, and all three produce different output.

JSON envelope types reflect the new contract:

  • `brief` → `component.list` (just names)
  • `compact` → `component.compact-list` (names + short description) — new
  • `full` → `component.detailed-list` (names + description + import path)

Implicit defaults preserved: when no `--detail` flag is passed on a list view, we default to `brief` so `xds component --list` stays terse for humans. Single-component views still default to `full`.

Bonus fix: `formatHookFull` was missing the `## Import` / `## Related` / `## Examples` sections that `formatHookCompact` already had, so `compact` could be longer than `full` for single hooks. `formatHookFull` is now a strict superset.

3. Subcommand `--help` omitted all global flags & had no examples

Before:
```
$ xds component --help
Usage: xds component [options] [name]
…
Options:
--list …
--category …
-h, --help display help for command
```

No mention of `--json`, `--lang`, or `--detail` — even though those flags significantly change the output. No `Examples:` section anywhere in the CLI.

Fix: new `addCommonHelp(cmd, examples)` helper in `packages/cli/src/utils/help.mjs` injects an `Examples:` block plus a "Global Options (inherited from `xds`)" reference into every subcommand. Wired into: `component`, `hook`, `docs`, `init`, `swizzle`, `discover`, `upgrade`.

4. `xds --version --json` returned plain text

```
$ xds --version --json
0.0.14
```

Fix:

```
$ xds --version --json
{
"type": "version",
"data": {
"version": "0.0.14"
}
}
```

Plain `xds --version` still returns plain text.

Tests

  • New: `packages/cli/src/lib/lang.test.mjs` (10 tests) — `validateLang` unit tests.
  • New: `packages/cli/src/commands/cli-flags.test.mjs` (16 tests) — end-to-end CLI tests covering version-json, lang validation, detail level ordering on list views, and subcommand --help global-flags / Examples surfacing.
  • Existing: 590 → 616 cli vitest tests, all passing.
  • `pnpm lint` clean.

Files

  • `packages/cli/src/lib/lang.mjs` (new) — `validateLang`, `SUPPORTED_LANGS`.
  • `packages/cli/src/utils/help.mjs` (new) — `addCommonHelp`.
  • `packages/cli/src/index.mjs` — `--version --json` interceptor.
  • `packages/cli/src/api/{component,hook}.mjs` — list-mode detail routing.
  • `packages/cli/src/commands/{component,hook,docs,init,swizzle,discover,upgrade}/...` — wire validation + help.
  • `packages/cli/src/lib/hook-format.mjs` — `formatHookFull` superset of `formatHookCompact`.
  • `packages/cli/src/types/component.d.ts` — new envelope types.

…l flags in subcommand help

- Validate --lang against the supported set (en, zh, dense). Unsupported
  values now exit 1 with a clear message instead of silently falling
  back to English. JSON callers get a typed error envelope.
- Warn on stderr when --lang zh is requested: the translation coverage
  is incomplete and prose still falls back to English. (Follow-up:
  flesh out zh translations across all .doc.mjs files.)
- Reject combining --lang with the legacy --zh / --dense shortcuts
  instead of silently picking one.
- Fix --detail level inversion on list views. Previously
  'component --list --detail brief' produced ~26x MORE output than
  '--detail full'; now brief < compact < full as documented:
    brief   -> just names                        (component.list)
    compact -> names + short description         (component.compact-list)
    full    -> names + description + import path (component.detailed-list)
  When the user does not pass --detail, list views default to brief
  (just names) so 'xds component --list' stays terse.
- Make formatHookFull a true superset of formatHookCompact (adds Import,
  Examples, Related sections) so single-hook detail levels are also
  brief < compact < full.
- Surface global flags (--json, --lang, --detail, --zh, --dense) and
  add an Examples: section in the --help output of component, hook,
  docs, init, swizzle, discover, and upgrade subcommands.
- 'xds --version --json' now emits a typed JSON envelope:
  { type: 'version', data: { version: ... } }.

Tests: +26 (lib/lang.test.mjs, commands/cli-flags.test.mjs).
All 616 cli vitest tests pass; pnpm lint clean.
@vercel

vercel Bot commented May 28, 2026

Copy link
Copy Markdown

Someone is attempting to deploy this pull request to the Meta Open Source Team on Vercel.

No GitHub account was found matching the commit author email address.

To deploy this pull request, the commit author's email address needs to be associated with a GitHub account.

Learn more about how to change the commit author information.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 28, 2026
@github-actions

github-actions Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Vercel Preview Deployment

Status ✅ Deployed
Preview Open Preview
Commit 21ca02f
Inspect Vercel Dashboard
Workflow View Logs

No authentication required — anyone with the link can view the preview.

@github-actions

github-actions Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

No new or modified components detected.

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@xds/core 21.1KB 32.5KB 4.8KB

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@josephfarina
josephfarina marked this pull request as draft May 29, 2026 20:17
@vercel

vercel Bot commented May 29, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
xds-sandbox Error Error May 29, 2026 9:07pm

Request Review

template, template get, theme, and theme build subcommands did not
re-surface the inherited global flags (--json/--lang/--detail) or an
Examples block in their --help, unlike component/hook/docs/discover/
upgrade. Wire addCommonHelp into all four and add subprocess tests
proving each --help shows the global flags and Examples.
gap-report and its setup subcommand did not re-surface the inherited
global flags (--json/--lang/--detail) or an Examples block in --help.
Wire addCommonHelp into both and add subprocess tests.
josephfarina added a commit that referenced this pull request Jun 1, 2026
The global --lang flag is advertised on every subcommand's help (declared
on the root program), but only component/hook/discover/docs translate.
Write/action commands (init, template, theme, upgrade, swizzle, build,
gap-report) silently ignored it and produced English.

Add a global preAction hook that validates --lang / --zh / --dense before
any side effects:
  - unsupported locale -> exit 1 (or structured { error } under --json)
  - partially-translated locale (zh) -> exit 0 + one-time stderr warning
  - --lang combined with legacy --zh/--dense -> exit 1 (ambiguous)

init --all --lang xx now exits 1 with zero files written (no half-writes).

Adds 9 subprocess/e2e tests proving the behavior across read and write
commands, the --json contract, side-effect-free rejection, and i18n honesty.

Closes checklist items #1 (flag validation) and #10 (i18n honesty) for the
write/action commands not covered by #2407.

This branch had an error being deployed

1 failed deployment
Preview — 21ca02fc Deployed May 29, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant