feat(cli): live preview in the browser with gram view --serve #255

Merged
abiwab merged 5 commits from feat/view-serve into dev/1.6 2026-11-25 17:51:55 +01:00
Owner

Step C of the 1.6 plan (.notes/v1.6/plan-etape-c.md): gram view <file> --serve serves a live preview of the recipe on 127.0.0.1, for any editor.

  • C0: PipelineResult.dependencies (every file the recipe was read from, or tried to be, missing @use targets included), ExportOptions.onPipeline, NothingToShowError.
  • C1: services/preview-server.ts, generic node:http server with SSE: one page with a tab per view that has an html format, a print button loading /print (the print format, same as gram print) in a hidden iframe, /client.js = the renderer's TOOLTIP_SCRIPT. Host check against DNS rebinding, per-route CSP, a directory watcher robust to atomic saves, renders serialized with a generation counter, last valid page kept under a banner on error.
  • C2: services/view-serve.ts (flags, validation, render reading config/DB/scale/kitchen on every render) and --serve, --port, --no-open, --view in gram view, which now takes the run sheet settings too and refuses an extra positional (--serve "<date>" written the gram plan way).
  • C3: renderer.print, renderer.disconnected in en and fr.
  • C5: CLI reference and editors matrix (en + fr), minor changeset on @gram-lang/cli.

Deviations from the plan, forced by the code:

  • CSP of / allows style-src 'unsafe-inline': the renderer's HTML has inline style attributes (copy button). Scripts stay 'self' only.
  • A syntax error of the entry file is a graph diagnostic, not an exception: it is detected (MODULE_PARSE_ERROR on the entry) to show the banner.
  • loadDb / resolveScaleFactor instead of their *Safe variants in the render, so a broken config mid-session shows the banner instead of exiting.
  • settings-parity.test.ts: gram view now exposes --clock.

The last commit aligns openInBrowser() with #254 (fix on main, without its changeset), so merging main into dev/1.6 brings no conflict.

Tests: preview-server.test.ts (real HTTP on port 0: routes, Host, CSP, live updates incl. rename saves, @use written before its file, errors, concurrency, shutdown), view-serve-args.test.ts, exporter tests. Not done: a manual run in Neovim/Helix and in a real browser.

Step C of the 1.6 plan (`.notes/v1.6/plan-etape-c.md`): `gram view <file> --serve` serves a live preview of the recipe on `127.0.0.1`, for any editor. - **C0**: `PipelineResult.dependencies` (every file the recipe was read from, or tried to be, missing `@use` targets included), `ExportOptions.onPipeline`, `NothingToShowError`. - **C1**: `services/preview-server.ts`, generic `node:http` server with SSE: one page with a tab per view that has an `html` format, a print button loading `/print` (the `print` format, same as `gram print`) in a hidden iframe, `/client.js` = the renderer's `TOOLTIP_SCRIPT`. Host check against DNS rebinding, per-route CSP, a directory watcher robust to atomic saves, renders serialized with a generation counter, last valid page kept under a banner on error. - **C2**: `services/view-serve.ts` (flags, validation, render reading config/DB/scale/kitchen on every render) and `--serve`, `--port`, `--no-open`, `--view` in `gram view`, which now takes the run sheet settings too and refuses an extra positional (`--serve "<date>"` written the `gram plan` way). - **C3**: `renderer.print`, `renderer.disconnected` in `en` and `fr`. - **C5**: CLI reference and editors matrix (en + fr), minor changeset on `@gram-lang/cli`. Deviations from the plan, forced by the code: - CSP of `/` allows `style-src 'unsafe-inline'`: the renderer's HTML has inline `style` attributes (copy button). Scripts stay `'self'` only. - A syntax error of the entry file is a graph diagnostic, not an exception: it is detected (`MODULE_PARSE_ERROR` on the entry) to show the banner. - `loadDb` / `resolveScaleFactor` instead of their `*Safe` variants in the render, so a broken config mid-session shows the banner instead of exiting. - `settings-parity.test.ts`: `gram view` now exposes `--clock`. The last commit aligns `openInBrowser()` with #254 (fix on `main`, without its changeset), so merging `main` into `dev/1.6` brings no conflict. Tests: `preview-server.test.ts` (real HTTP on port 0: routes, Host, CSP, live updates incl. rename saves, `@use` written before its file, errors, concurrency, shutdown), `view-serve-args.test.ts`, exporter tests. Not done: a manual run in Neovim/Helix and in a real browser.
abiwab merged commit a97d0df3e8 into dev/1.6 2026-11-25 17:51:55 +01:00
abiwab deleted branch feat/view-serve 2026-11-25 17:51:55 +01:00
Sign in to join this conversation.
No description provided.