@sbuilder/cli 0.9.1 → 0.12.0

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 (49) hide show
  1. package/README.md +302 -6
  2. package/README.vi.md +296 -7
  3. package/dist/bin/sb.js +190 -6
  4. package/dist/bin/sb.js.map +1 -1
  5. package/dist/src/agentdocs.js +12 -3
  6. package/dist/src/agentdocs.js.map +1 -1
  7. package/dist/src/components.js +574 -0
  8. package/dist/src/components.js.map +1 -0
  9. package/dist/src/doctor.js +520 -0
  10. package/dist/src/doctor.js.map +1 -0
  11. package/dist/src/envtypes.gen.js +1761 -0
  12. package/dist/src/envtypes.gen.js.map +1 -0
  13. package/dist/src/host.js +414 -22
  14. package/dist/src/host.js.map +1 -1
  15. package/dist/src/hostlink.js +161 -0
  16. package/dist/src/hostlink.js.map +1 -0
  17. package/dist/src/login.js +1 -1
  18. package/dist/src/login.js.map +1 -1
  19. package/dist/src/next.js +82 -6
  20. package/dist/src/next.js.map +1 -1
  21. package/dist/src/repo.js +24 -2
  22. package/dist/src/repo.js.map +1 -1
  23. package/dist/src/roottsconfig.js +154 -0
  24. package/dist/src/roottsconfig.js.map +1 -0
  25. package/dist/src/route.js +18 -1
  26. package/dist/src/route.js.map +1 -1
  27. package/dist/src/site.js +621 -71
  28. package/dist/src/site.js.map +1 -1
  29. package/dist/src/sitedev.js +99 -0
  30. package/dist/src/sitedev.js.map +1 -0
  31. package/dist/src/siteinit.js +76 -0
  32. package/dist/src/siteinit.js.map +1 -1
  33. package/dist/src/sitekinds.js +1048 -0
  34. package/dist/src/sitekinds.js.map +1 -0
  35. package/dist/src/sitepaths.js +12 -0
  36. package/dist/src/sitepaths.js.map +1 -0
  37. package/dist/src/sitestatus.js +104 -0
  38. package/dist/src/sitestatus.js.map +1 -0
  39. package/dist/src/source/meta.js +118 -0
  40. package/dist/src/source/meta.js.map +1 -0
  41. package/dist/src/source/parse.js +21 -2
  42. package/dist/src/source/parse.js.map +1 -1
  43. package/dist/src/starter.js +61 -0
  44. package/dist/src/starter.js.map +1 -0
  45. package/dist/src/vercel.js +379 -0
  46. package/dist/src/vercel.js.map +1 -0
  47. package/package.json +9 -1
  48. package/dist/src/export.js +0 -167
  49. package/dist/src/export.js.map +0 -1
package/README.md CHANGED
@@ -16,6 +16,9 @@ Account
16
16
  that renews itself
17
17
  sb logout sign this machine out and forget that credential
18
18
  sb whoami which account (and platform) the CLI acts as
19
+ sb doctor [--json] [--build] check this machine, repo and Next.js app; every
20
+ problem names its fix; exit 1 on any failure (--build
21
+ also bundles components/, running the repo's esbuild)
19
22
 
20
23
  Sites
21
24
  sb site new "<description>" [--dir <path>] [--github] [--editor]
@@ -24,9 +27,13 @@ Sites
24
27
  sb site init [dir] [--site <id>] [--github]
25
28
  a repo holding one site's pages, synced with
26
29
  GitHub both ways
27
- sb site pull | push [--force] [--dry-run]
28
- editor edits into design/pages, or your edits
29
- back as DRAFTS
30
+ sb site pull | push [--force] [--dry-run] [--prune] [--yes] [--theme]
31
+ editor edits into design/, or your edits back as
32
+ DRAFTS (--prune: delete what a gone file stood for;
33
+ --theme: also send the theme, which is live at once)
34
+ sb site status what differs from the last sync, and translations
35
+ whose source text changed (offline)
36
+ sb site dev [--theme] watch design/ and code/, push each save as a DRAFT
30
37
  sb site publish <file>… | --all
31
38
  make pages LIVE — named files, or every page
32
39
  sb site blocks the blocks your installed apps offer, with the line
@@ -38,7 +45,12 @@ Sites
38
45
  sb site code push [--site <id>] [--dir <path>] [--force] [--dry-run] [--delete]
39
46
  send local edits back; refuses a file changed
40
47
  in the Code Dock since your pull unless --force
48
+ sb site components init | upgrade <Name> [--to <n>] [--dry-run]
49
+ code components: set the repo up (components/Hero.tsx);
50
+ move a component's pins to another version
41
51
  sb site agents write the AI-agent files into this repo (never overwrites)
52
+ sb site types [--dir <path>] [--quiet]
53
+ write design/sbuilder-env.d.ts: types for the .page.tsx files
42
54
  sb site mcp the Store Builder MCP server, with this repo's key
43
55
 
44
56
  Apps (for developers building on the platform)
@@ -84,6 +96,7 @@ has no dependencies and nothing to build.
84
96
  - [Refusals, and what they mean](#refusals-and-what-they-mean)
85
97
  - [What it deliberately does not do](#what-it-deliberately-does-not-do)
86
98
  - [Troubleshooting](#troubleshooting)
99
+ - [`@sbuilder/sdk`](#sbuildersdk--the-same-platform-from-your-own-code)
87
100
  - [License](#license)
88
101
 
89
102
  ---
@@ -691,9 +704,17 @@ sb site new "a shop selling handmade candles" # the same, from the command lin
691
704
  sb site init my-shop --github # a repo for one of your sites, on a private GitHub repo
692
705
  sb site pull # take what was edited in the editor
693
706
  sb site push # save edited page files as DRAFTS
707
+ sb site push --prune --yes # also delete the pages whose file is gone (--yes off a terminal)
708
+ sb site push --theme # also send design/theme.json (a theme save is live at once)
709
+ sb site status # what differs from the last sync; stale translations
710
+ sb site dev # watch design/ and code/ and push each save as a DRAFT
694
711
  sb site publish --all # make pages live (or name files)
695
712
  sb site next # serve the pages from a Next.js app (npm run dev)
713
+ sb site next --link # mint the host secret into .env: a deployed host signs its requests
714
+ sb site deploy --vercel # one command to production on Vercel, with your own vercel CLI (--dry-run: the plan; --rotate replaces a link made elsewhere)
696
715
  sb site agents # the AI-agent files, for a repo made by an older CLI
716
+ sb site types # design/sbuilder-env.d.ts: completion + checks for .page.tsx
717
+ sb site types --quiet # the same, silent: what a Next app's predev / prebuild run
697
718
  sb site mcp # the Store Builder MCP server, with this repo's key
698
719
  ```
699
720
 
@@ -703,6 +724,8 @@ description), writes the repo below into `./<name>` (`--dir` picks another folde
703
724
  offers to start it in that folder with your description. Signed out, it runs the
704
725
  `sb login` flow first. It is exactly what choice 1 of `sb init` does.
705
726
 
727
+ **The Next.js storefront starter** is choice 4 of `sb init` (the menu `npm create sbuilder-app` shows). It picks one of your sites, writes its repo like `sb site init`, runs `sb site next`, adds `@sbuilder/sdk` and one hand-written route, `app/api/featured/route.ts` (JSON), which lists the store's products through `@sbuilder/sdk/storefront` (no API key, so it also works on a deployed host). The page types (`design/sbuilder-env.d.ts`) come with the repo. Then `npm install && npm run dev`, and `sb site deploy --vercel` when you want it online.
728
+
706
729
  **`sb site new --editor "<description>"`** builds in the editor instead: it opens the
707
730
  editor, which creates the site and starts building it. If `sb agent` is already
708
731
  running it does the work; if not and Claude Code is installed, the command serves as
@@ -744,6 +767,7 @@ takes `--site <id>`), and writes:
744
767
  | `AGENTS.md`, `CLAUDE.md`, `GEMINI.md` | How an AI agent works in this repo; the other two import `AGENTS.md` |
745
768
  | `.claude/skills/sbuilder-page/SKILL.md` | The page syntax, the section-building loop with the tools, app-block rules and what pull / push / publish do — `AGENTS.md` points at it, so every agent reads the same text |
746
769
  | `.github/workflows/sbuilder.yml` | The GitHub half of the sync |
770
+ | `design/sbuilder-env.d.ts`, `design/tsconfig.json` | Types for the page files (see *Page types* below) — commit them; `sb site types` regenerates the first |
747
771
 
748
772
  None of the agent files above is overwritten. One that is already there is kept — silently when it
749
773
  is what init would write, otherwise with a note saying what it lacks and the exact text to add.
@@ -801,6 +825,111 @@ checkout, forms, the islands runtime) and any path with no page file go to the l
801
825
  (`SB_STOREFRONT` in `.env`). A route you add under `app/` wins over the page catch-all, so
802
826
  hand-written React pages live beside the builder's. Deploy it anywhere Next.js runs.
803
827
 
828
+ **Two modes.** The host renders only where it is safe to. In **preview** (`next dev`, or an
829
+ explicit `SB_MODE=preview`) a page file is rendered through `render-context` with the repo's
830
+ key, every response says `X-Robots-Tag: noindex`, and `/robots.txt` disallows everything — a
831
+ preview must never compete with the real site. A **Vercel preview deployment** is *not* this
832
+ mode: it passes the published pages through like a deployed host, needs no key, and is simply
833
+ never indexed (`noindex` on everything, `robots.txt` disallowing all, no origin check) — it follows the store's redirects as they are, so if the built-in address redirects to the primary domain a visitor leaves the preview for production (said once on stderr); the
834
+ generated `next.config.mjs` adds the header to your own routes too, read when Next *builds* (set
835
+ `SB_MODE=preview` for the build as well). In a **deployed** host (`next start`, `NODE_ENV=production`, `SB_MODE` unset) a builder page is served
836
+ from the store's **published** HTML, passed through: byte-identical by construction, cached by the
837
+ platform, and the host holds **no key** — `render-context` is a write-scoped, uncached endpoint that
838
+ must never have its key in a Vercel environment. Set `SB_STOREFRONT` to the store's built-in
839
+ address (not your own domain).
840
+
841
+ **The host link: no setting to flip.** The store redirects its built-in address to the primary
842
+ domain by default; the host forwards to the built-in address, so once the primary domain points at
843
+ the deployment that redirect would send every visitor back to the host, forever. `sb site next
844
+ --link` mints a per-site **host secret** (`sbh_…`; it is *not* an API key and has no scope: it reads
845
+ and writes nothing) into the repo's `.env` (mode 0600, never printed), together with `SB_SITE_ID`.
846
+ A deployed host that has both signs every request it forwards — pages, crawl files and `/_wb/*` —
847
+ with `X-SB-Host` (an HMAC over the time, method, path and visitor address) and `X-SB-Client-IP`; the
848
+ store, having verified it (the same site, within a minute), rate-limits the **visitor** instead of
849
+ treating every shopper as the host, and does not redirect that request. Put `SB_HOST_SECRET`,
850
+ `SB_SITE_ID`, `SB_PUBLIC_URL` and `SB_STOREFRONT` in the host's own environment (Vercel: Project →
851
+ Settings → Environment Variables). Rotating (`--link` again, with `--yes`) stops the old secret at
852
+ once. Rotate it any time with `sb site next --link --yes` (also when the site already has a link made elsewhere — another machine, CI — which `--link` asks about first: the deployed host's current secret dies at once). Revoke it with `DELETE /api/v1/host-link`, or in **Manage → Domains → Host link (Next.js)**: *Create link*, *Rotate* and *Revoke* are there too (a created or rotated secret is shown once — set it as `SB_HOST_SECRET` in your hosting provider).
853
+
854
+ The visitor's address is the **first** hop of `x-forwarded-for` on Vercel (`VERCEL=1`), which
855
+ overwrites that header with what it saw. Anywhere else the header is whatever the client sent, and a
856
+ proxy that appends puts the address it saw on the **right**, so the leftmost hop is visitor-controlled:
857
+ set `SB_TRUST_PROXY=N` — N proxies you control sit in front of the host, `1` being the usual case —
858
+ and the visitor is the Nth hop from the RIGHT (fewer hops than N and nothing is signed). With no
859
+ trustworthy address (or no secret) a request goes unsigned, the store sees the host, and the
860
+ fallback below applies.
861
+
862
+ **Without a host link** the host stops with a named error rather than loop (and refuses to start).
863
+ In **Manage → Domains → Default address** set *When someone visits this address* to **Serve this
864
+ site directly** (not *Redirect to …*).
865
+
866
+ **Deploy to Vercel: `sb site deploy --vercel`.** One command takes a repo set up with `sb site next` to a production deployment, using **your own `vercel` CLI**. It is detected; if it is not on your PATH the command prints the install and login steps and exits non-zero — `sb` never installs anything globally. In order: it makes sure `.vercelignore` lists `.env*` — `vercel deploy` reads `.vercelignore` alone and never the contents of `.gitignore`, so without it `.env` (the repo's API key) would be uploaded; `sb site next` writes the file, and here a missing line is added only after you say yes (or with `--yes`); it reuses the host link in `.env` (it mints one only when there is none; a link made elsewhere is replaced only with `--rotate`, which stops the old secret at once — `--yes` never rotates, and the addresses are validated before anything is minted); it checks that the site's primary domain **is** `SB_PUBLIC_URL`, the same check the deployed host makes at startup, and stops with the fix before changing anything on Vercel; it runs `vercel link` (once; off a terminal that needs `--yes`); it sets `SB_HOST_SECRET`, `SB_SITE_ID`, `SB_PUBLIC_URL` and `SB_STOREFRONT` for Production with `vercel env add`, each value on **stdin** (never in argv, never printed; the secret is masked even in a failing `vercel` message); and it runs `vercel deploy --prod` and prints the URL. `SB_PUBLIC_URL` and `SB_STOREFRONT` are read from the shell, else from `.env`. A variable Vercel already has is replaced only after you confirm (or pass `--yes`), so running it again is safe. **Preview deployments get `SB_STOREFRONT` only:** they pass published pages through with `noindex` and skip the origin check, so they need nothing else, and Vercel gives preview variables to every branch's build — the secret stays out of reach of a pull request's build script. (With no secret a preview follows the store's redirect from its built-in address, said once on stderr.) `--dry-run` prints the plan without running `vercel`, calling the platform or printing a value. The domain itself is added to the project with `vercel domains add <domain>`; the command reminds you. It names the Vercel project and organization before it sets anything, and refuses a `.vercel/project.json` that git tracks (a cloned repo could aim your deploy, and your secrets, at someone else's project) unless you pass `--yes` (which accepts that project). Before minting anything it checks the primary domain without the secret; when a link was replaced and a later step fails, it says the live host's old secret is already rejected, so the live site is degraded until the deploy finishes. If it fails after minting a host link it says so: production keeps the previous secret until a deploy finishes, and running the same command again reuses the new link.
867
+
868
+ **Addresses.** Besides an exact page path, a preview maps `/products/<slug>`, `/collections/<slug>`,
869
+ `/blog/<slug>` and `/courses/<slug>` to the page file of that type (the one marked
870
+ `defaultTemplate: true`, else the only one), and `/<locale>/…` — for each locale in
871
+ `design/translations/` — to the same file, including a translated page address
872
+ (`/en/about-us`). The request path is sent unchanged, so the store resolves the entity and the
873
+ translated slug. What the render could not carry is named on stderr and in
874
+ `x-sbuilder-unsupported`: `appIslands` (an app's island script is a relative URL), `entityRoutes`
875
+ (a Collections record address), `images` (an image outside the store's own media),
876
+ `translatedSlugs` (more translated addresses than one render verifies) and `gates` (a members-only
877
+ or course-gated page renders as an anonymous visitor sees it).
878
+
879
+ **One origin, and the crawl files.** `/sitemap.xml`, `/robots.txt`, `/llms.txt` and
880
+ `/<locale>/llms.txt` are **the store's own**, fetched — never regenerated here — so gates, noindex
881
+ and drafts are excluded exactly as on the store. Your own routes are merged in, each once, from
882
+ `app/sb-crawl.js` (`sitemap`: Next's sitemap entries; `robots`: extra lines; `llms`: extra lines);
883
+ do not add `app/sitemap.*` or `app/robots.*`, which would replace the store's part instead of
884
+ merging with it. Every absolute URL is on the site's one origin: a deployed host **refuses to
885
+ start** unless its public address (`SB_PUBLIC_URL`, or Vercel's `VERCEL_PROJECT_PRODUCTION_URL`)
886
+ is the site's primary domain, because canonicals, hreflang and the sitemap would otherwise point
887
+ elsewhere. Cookies the store sets come back host-only (no `Domain=`), and a redirect onto the
888
+ store's own address is rewritten to this one. Page SEO (title, description, `og*`, canonical,
889
+ `noIndex`, JSON-LD, `metaTags`) lives in `export const page`'s `settings`, merged **key by key** (an editor edit to `seo.title` and a file edit to `seo.description` both survive; the same key changed differently on both sides is named, e.g. `settings.seo.title changed here AND in the editor`, and a key removed in the file against the same key changed in the editor is a clash too); a
890
+ product's, article's or collection's SEO is catalogue data, edited in the dashboard or through
891
+ `/api/v1`, not in page files.
892
+
893
+ **On Vercel**, `sb site next` also writes a `.vercelignore` with `.env` and `.env*` (`vercel deploy` uploads `.env` otherwise: it never reads `.gitignore`'s contents), and writes `outputFileTracingIncludes` for the renderer's `.wasm`, the
894
+ page files and the manifest, so the deployed function contains what it reads at run time; the Edge
895
+ runtime is not supported (WebAssembly and the file system).
896
+
897
+ **Page types.** `design/sbuilder-env.d.ts` declares every element as a typed tag (`<Button text="…" tablet={{ style: {…} }} />`),
898
+ so VS Code, Cursor and an AI agent complete the props and flag a wrong one while you type: a
899
+ number where a text is expected, CSS written straight into `tablet={{ … }}` instead of under
900
+ `style`, a `bind` without its `field`, a typo in `<Ref id>`. It is
901
+ generated from the platform's element list, so **`sb site types`** (run it after upgrading `sb`)
902
+ rewrites it; `sb site init` and `sb site pull` write it, and a `tsconfig.json`, only where there is
903
+ none — an existing `design/tsconfig.json` is kept, with a note saying what to add. The types are data
904
+ only: they never change what a page file means or how it is read, and an unknown special is
905
+ accepted and kept (so a misspelt prop name is not flagged). An element type the editor has removed
906
+ but an old page still holds is reported as an unknown tag until
907
+ a newer `sb` lists it (run `sb site types` after upgrading).
908
+ The page types are their own TypeScript project (`design/tsconfig.json`: no DOM, `jsx: preserve`),
909
+ because the element names are globals that would clash with the DOM's (`Image`, `Text`, `Node`);
910
+ an editor uses the nearest tsconfig, so `design/` gets the page types and `app/` keeps Next's. The
911
+ app's own root `tsconfig.json` must therefore exclude `design`: `sb site next`, `sb site init`,
912
+ `sb site pull` and `sb site types` add it when the file exists (and say so when they cannot edit it),
913
+ a JS-only app gets no root tsconfig (it would make Next demand TypeScript), but once the app has a
914
+ `.ts` file of its own, `sb site types` writes one with Next's defaults plus the exclude, before Next
915
+ writes its own without it. The Next app `sb site next` writes runs `sb site types --quiet` as
916
+ `predev` and `prebuild`, so the first build never type-checks the page files with the DOM's names;
917
+ `sb doctor` checks it (`next:tsconfig`). A root tsconfig `sb` cannot edit is kept: with comments (and no `exclude` naming `design`), a
918
+ non-array `exclude` or a symlink, `sb site types` says what to add and exits 1, so `prebuild`
919
+ stops there instead of failing later inside `next build`.
920
+
921
+ ### Next.js compatibility
922
+
923
+ The host is plain `Request → Response` code (`@sbuilder/cli/host`) that **never imports `next`**
924
+ (a test fails if it does); a generated app uses only stable, public Next.js surfaces: route
925
+ handlers, `rewrites().beforeFiles`, `serverExternalPackages`, `outputFileTracingIncludes` and
926
+ `instrumentation.js`. Supported: `next` **>=15.0.0 <17** (the CLI's optional peer range; 15.0 is the
927
+ first release with all of those). `.github/workflows/next-compat.yml` builds and starts a generated
928
+ app on **15.0** (oldest), **latest** (both required) and **canary** (weekly, never blocking: a red
929
+ canary opens an issue) and checks the page route, a hand-written route, cookie filtering, noindex,
930
+ both modes and the refusal to start. A new Next.js major joins the range only after it is green
931
+ there; until then it is untested, not forbidden.
932
+
804
933
  **The workflow is the whole GitHub integration** — no GitHub App, no webhook. A push to
805
934
  `main` touching `design/` or `code/` saves drafts; every 30 minutes (and on demand) it
806
935
  pulls what changed in the editor and commits it. Running it by hand with *publish*
@@ -813,11 +942,154 @@ since. A page edited on both sides is named and skipped (exit 1); `--force` pick
813
942
  side — the editor's on pull, yours on push.
814
943
 
815
944
  **Push saves drafts; `sb site publish` is the only thing here that goes live**, and it
816
- publishes only the files you name or, with `--all`, every page. Pages are not created
817
- or deleted from files: create one in the editor (or let the agent), then pull; a page
818
- deleted on the platform keeps its file and is named. With an API key it uses
945
+ publishes only the files you name or, with `--all`, every page. With an API key it uses
819
946
  `/api/v1/pages/{id}/document` and needs no `--site`.
820
947
 
948
+ **`export const page` is authoritative.** The last export in a page file states the page's
949
+ own facts: `export const page = { name: "Pricing", slug: "pricing", type: "page",
950
+ isHomepage: false }` (plus `settings`, and `defaultTemplate: true` for an entity template).
951
+ A new `design/pages/*.page.tsx` with that export and no `id` **creates the page** on push;
952
+ changing `name`, `slug`, `type`, `isHomepage`, `settings` or `defaultTemplate` in a tracked
953
+ file renames or re-types it. `path` is derived and only informational. When the platform
954
+ settles on something other than what you asked (a slug is made unique) push says so, and
955
+ after every save the file is rewritten to what the platform stored — minted ids and the
956
+ facts it settled on — so the next `sb site pull` changes nothing. A file with no
957
+ `export const page` and no manifest entry is still only reported.
958
+
959
+ Facts are merged three ways like the document: what was last synced is the base. A fact only
960
+ your file changed is sent; one only the editor changed is left alone (the file takes it); both
961
+ changed to the same value is nothing; both changed differently refuses that page
962
+ (`name changed here AND in the editor — kept both`, exit 1, nothing sent) until you pull the
963
+ editor's or run `sb site push --force`, which picks the file. A pull keeps a file whose fact
964
+ changed on both sides and names it.
965
+
966
+ With an API key (`/api/v1`) the platform also fences the write itself: each fact a push changes is sent with the value this push read (`base`), and one the editor saved in between is refused and named (`name changed in the editor while pushing — nothing sent`; `--force` re-reads and retries once). A session login (`/api/sites/…`) has no such fence — the API-key surface is the fenced one.
967
+
968
+ **Nothing is deleted unless you say so.** A tracked page file that is gone is printed as
969
+ `missing` and the page stays on the platform (exit 1). `sb site push --prune` deletes those
970
+ pages: it lists them first, asks on a terminal, and off a terminal also needs `--yes`
971
+ (nothing is deleted otherwise). `--prune` refuses when `design/pages/` is absent, since
972
+ every page would then look deleted. A page deleted on the platform keeps its file, named
973
+ by pull.
974
+
975
+ ### The rest of the site as files
976
+
977
+ A repo made by `sb site init` also holds, next to `design/pages/`:
978
+
979
+ | Kind | Where | Facts at the end of the file |
980
+ | --- | --- | --- |
981
+ | Shared sections (header, footer, custom) | `design/globals/<name>.page.tsx` | `export const global = { id, name, kind }` |
982
+ | Overlays (cart, pop-ups, quick view) | `design/overlays/<name>.page.tsx` | `export const overlay = { id, name, kind, allPages, pages }`: `allPages` is "shown on every page" and `pages` the page ids it is attached to — two separate facts (a pop-up shown everywhere keeps its page list); a cart and a quick view have `allPages: false, pages: []` |
983
+ | The theme | `design/theme.json` | the theme document, keys sorted |
984
+ | Translations | `design/translations/<locale>.json` | nested `{ "<entityType>": { "<entityId>": { "<field>": { value, source, sourceHash } } } }` (an id or field may contain a `/`) |
985
+
986
+ These need the repo's API key (a session login skips them and says so). Every one follows the
987
+ rule the page files do: the last sync is the base, your file is ours, the platform now is
988
+ theirs; only your change is sent, only the editor's is left alone (the file takes it), the same
989
+ change on both sides is nothing, and a different change on both sides refuses that file by name
990
+ (exit 1, nothing sent) until you pull or run `sb site push --force`. A new global or overlay file
991
+ with `name` and `kind` and no `id` is created. A shared section reaches every page that shows it
992
+ at that page's next publish.
993
+
994
+ Nothing here is deleted by a missing file or a removed entry. `sb site push --prune` (and `--yes`
995
+ off a terminal) deletes a shared section or overlay whose file is gone, or translation entries
996
+ the file dropped, after listing them. A section still shown on pages comes back as `in_use`
997
+ with the page count and is kept: the CLI never forces it.
998
+
999
+ **The theme is live the moment it is saved** (there is no publish step), so `sb site push` sends
1000
+ `design/theme.json` only with `--theme`; without it the file is named as withheld and nothing is
1001
+ sent. Translations you add without a `source` are sent as `machine`; `sourceHash` is the editor's
1002
+ fingerprint of the source text, and `sb site status` and `sb site pull` name every translation
1003
+ whose source text has changed since it was translated as STALE.
1004
+
1005
+ `sb site dev` watches `design/` and `code/` and pushes each saved file as a DRAFT after 300 ms of
1006
+ quiet. It is `sb site push` per file: a parse error is printed as `file:line:col` and sends
1007
+ nothing, a file that got a 409 (the editor changed it) is stopped and named, and it never
1008
+ publishes or prunes, and it does not take `--force` (that would overwrite the editor on every save). A page whose repo has no record of the last sync (a manifest from an older CLI) names any fact the file states differently from the platform instead of sending it: `sb site pull` records them.
1009
+
1010
+ ### Code components — your own React in a page
1011
+
1012
+ ```
1013
+ sb site components init # esbuild + preact into devDependencies, components/Hero.tsx
1014
+ sb site push # bundles each components/<Name>.tsx and uploads it, then the pages
1015
+ sb site components upgrade Hero # move every page's pin of Hero to its latest version
1016
+ sb site components upgrade Hero --to 3 --dry-run
1017
+ ```
1018
+
1019
+ A code component is a React (Preact) component in `components/<Name>.tsx` that exports its props
1020
+ as `export const schema = defineProps({ … })` (from `@sbuilder/sdk/component`) and a default
1021
+ function component. The editor lists it as the **Code component** element; the platform renders
1022
+ it to HTML on the server, inside a sandbox with no network, a frozen clock and a seeded
1023
+ `Math.random`, and the page keeps that HTML. The CLI also builds a client bundle for every
1024
+ component, so the published page hydrates it in the browser — every code component ships its JavaScript.
1025
+
1026
+ - **Props.** `text`, `richtext`, `link`, `image`, `enum` (with `options`), `boolean` and `number`
1027
+ are content: they change the markup and are the same on every breakpoint. **A length or a colour is a `css` prop**:
1028
+ the page sets it per breakpoint as the custom property `--<name>` and the component reads
1029
+ `var(--<name>, <default>)`. A `css` name is lowercase kebab-case (`gap-size`); camelCase is a type
1030
+ error in `defineProps` and refused by the platform. The schema must be a literal object —
1031
+ `sb site push` reads it from the source without running anything.
1032
+ - **Versions are immutable.** `sb site push` uploads a new version only when the bundle changed
1033
+ (the platform answers with the existing one otherwise) and prints the version it assigned. Every
1034
+ component is uploaded **before** any page, so a page's pin (`version={3}` on its
1035
+ `<CodeComponent …/>`) always names a version that exists. Nothing on any page moves by itself.
1036
+ - **Upgrading is a local edit.** `sb site components upgrade <Name> [--to <n>]` rewrites the pins
1037
+ in `design/` (pages, shared sections, overlays), names a prop the new version no longer takes,
1038
+ and sends nothing. Review the diff, then `sb site push` saves a draft. `html` in a page file is
1039
+ derived — the platform re-renders it on every save — so leave it alone.
1040
+ - **Limits.** SSR bundle 512 KiB, client bundle 256 KiB (named locally before any upload), one
1041
+ second to render, a clock frozen at 2000-01-01 and a seeded `Math.random` (`fetch`, timers and
1042
+ `crypto` throw), no
1043
+ Server Components, no Suspense data, no `use`. A bundle that throws when the platform renders it
1044
+ once with its defaults is refused with its own error (`bundle_rejected`).
1045
+ - **The bundles are the CLI's.** Both bundle contracts (SSR assigns `globalThis.__sbComponent = { render }`; the client is an IIFE `__sbClient` exporting `hydrate(el, props)`, which the platform attaches to `Name@Version`) are generated by the CLI; the author never writes them, only the component.
1046
+ - **What runs where.** `sb site push` runs **your repo's esbuild** (from `node_modules`, put there
1047
+ by `npm install`) on your machine, as `npm run build` would. The CLI has no dependency of its own;
1048
+ `init` pins `esbuild`, `preact` and `preact-render-to-string` to exact versions, so the same
1049
+ source builds the same bytes. Uploading needs an API key (`wbk_…`, `pages.write`) in `SB_TOKEN` or
1050
+ `.env`; a browser sign-in cannot upload (pages are still pushed). A file in `components/` without
1051
+ `export const schema` is not a component and is skipped by name — `components/` is also the folder
1052
+ a Next.js app keeps its own components in.
1053
+
1054
+ ## `sb doctor` — check the setup
1055
+
1056
+ ```
1057
+ sb doctor # check this machine, this repo and its Next.js app
1058
+ sb doctor --json # the same, as JSON for CI (exit 1 when a check fails)
1059
+ sb doctor --build # also bundle components/ (runs the repo's esbuild)
1060
+ ```
1061
+
1062
+ Read-only: it reads files and sends GET requests, nothing else. Every check that does not pass
1063
+ names the exact command or Manage path that fixes it (`fix:`). `ok` and `skip` pass; a `warn`
1064
+ works but something you probably want is off; only a `fail` makes the exit code `1`.
1065
+
1066
+ `sb doctor` never runs code from the repo it checks. `sb doctor --build` is the exception, and says
1067
+ so: it also bundles `components/`, which loads and **runs the repo's own esbuild** from
1068
+ `node_modules`. Use it in a repo you trust, as you would `npm run build`.
1069
+
1070
+ | Check | What it looks at | Fails when |
1071
+ | --- | --- | --- |
1072
+ | `node` | Node.js version | older than 20 |
1073
+ | `auth` | the key (`SB_TOKEN`, the repo's `.env`) or your `sb login` | not signed in, key refused or revoked, a deploy key (`sbdk_…`), platform unreachable |
1074
+ | `scope:pages`, `scope:theme`, `scope:translations` | the read scopes of a key, and what each missing one disables | `pages.read` missing (a `warn` for the other two: they only switch off that kind of file) |
1075
+ | `scope:write` | the write scopes — **not probed** (a GET cannot test a write); it says what each one gates | never |
1076
+ | `site` | `sb.json` names a site this key or login reaches, and `.sb-site.json` is from the same one | no `siteId`, bad JSON, another site's manifest, a site you cannot read |
1077
+ | `manifest` | `.sb-site.json`: every path inside its kind's folder under `design/`, no symbolic links | a path outside `design/`, a symbolic link (a missing file is a `warn`) |
1078
+ | `files` | every file under `design/` parses | any does not: it prints `file:line:col` |
1079
+ | `components:build` | `components/<Name>.tsx` files with a literal `schema`, and `esbuild` installed in the repo (its `package.json` is looked for — never loaded). Static: **no repo code runs** | a file's `schema` is not a literal, a symbolic link, esbuild missing. With `--build` also: a component that does not build or is over a size cap |
1080
+ | `components:pins` | every `<CodeComponent version={n}>` in `design/` names a version the platform has (GET) | one does not (`sb site components upgrade`, or `sb site push` to upload it) |
1081
+ | `domain` | the store's primary domain against `SB_PUBLIC_URL`, and the built-in address's redirect | they differ; the built-in address redirects to your deployment (the loop — fix: Manage → Domains → Default address → *Serve this site directly*) |
1082
+ | `host-secret` | the site's host link (asked with a GET), whether `SB_HOST_SECRET` / `SB_SITE_ID` are in this environment or `.env`, and — with `SB_STOREFRONT` — whether the store still accepts that secret (a signed and an unsigned GET of `robots.txt` are compared; a rotation elsewhere is a `fail`) | a secret is set but the site has no link (revoked) or no site id; a Next app with no link, or a link with no secret here, is a `warn` |
1083
+ | `next:version` | the installed `next` is in `>=15.0.0 <17` | it is outside the range |
1084
+ | `next:files` | the files `sb site next` wrote are present and as generated | one is missing (a modified file is a `warn`; `app/sb-crawl.js` is yours to edit) |
1085
+ | `next:tsconfig` | the app's root `tsconfig.json` (if there is one) excludes `design` | it does not: `next build` would type-check the page files with the DOM's names — `sb site types` fixes it |
1086
+ | `next:trace` | `.next` traces `renderpage.wasm` into the page route | built, and the wasm is not in the trace |
1087
+ | `next:noindex` | `next.config.mjs` sends noindex for a preview | never — a `warn` |
1088
+
1089
+ `--json` prints `{ "version": 1, "ok": boolean, "dir": string|null, "api": string, "checks":
1090
+ [{ "id", "title", "status": "ok"|"warn"|"fail"|"skip", "detail", "fix": string|null }] }`; the
1091
+ `id`s are stable, so a CI script can match on them.
1092
+
821
1093
  ## What changed in 0.9
822
1094
 
823
1095
  The commands moved into groups. **The old names still work in 0.9** and print one
@@ -925,6 +1197,11 @@ Three are read by the CLI itself:
925
1197
  | `SB_API` | every command but `sb app init` | `https://api.sbuilder.io.vn` — set `http://localhost:8080` for a local platform |
926
1198
  | `SB_NO_BROWSER` | `sb login` | unset — set it to stop `sb login` opening the `/device` page (CI and non-terminals never open it) |
927
1199
  | `SB_STOREFRONT` | the `sb site next` app | the live store's address (`sb site init` writes it into `.env`) — where `/_wb/*` and paths with no page file are forwarded |
1200
+ | `SB_MODE` | the `sb site next` app | `preview` renders page files with the repo's key and noindexes everything; `deployed` serves the store's published pages with no key — unset, `NODE_ENV=production` means deployed (a Vercel preview deployment is deployed, never indexed) |
1201
+ | `SB_PUBLIC_URL` | a deployed `sb site next` app | the host's public address; it must be the site's primary domain or the host refuses to start (`VERCEL_PROJECT_PRODUCTION_URL` stands in on Vercel) |
1202
+ | `SB_HOST_SECRET` | a deployed `sb site next` app | the host secret `sb site next --link` wrote into `.env`; the host signs every request it forwards with it (never logged, never in a URL) |
1203
+ | `SB_SITE_ID` | a deployed `sb site next` app | the site the host signs for (written beside the secret) |
1204
+ | `SB_TRUST_PROXY` | a `sb site next` app | `N` = the number of proxies you control in front of the host: the visitor is the Nth hop from the right of `x-forwarded-for` (on Vercel the first hop is used and this is not needed) |
928
1205
 
929
1206
  `SB_TOKEN` **overrides** the stored `sb login` credential when set. For `sb app dev`
930
1207
  and `sb app deploy` it is a token for the **organization that owns the app** — the
@@ -1017,6 +1294,25 @@ Stated so you can plan around it rather than discover it.
1017
1294
 
1018
1295
  ---
1019
1296
 
1297
+ ## `@sbuilder/sdk` — the same platform from your own code
1298
+
1299
+ Not part of `sb`: a separate package for code that is not a terminal — your server, a
1300
+ webhook receiver, a hand-built storefront.
1301
+
1302
+ ```
1303
+ npm install @sbuilder/sdk
1304
+ ```
1305
+
1306
+ - `createClient({ apiKey })` — typed calls to `/api/v1` (products, orders, pages, …). Server-side
1307
+ only: it refuses to start where `window` exists, because an API key in a page is public.
1308
+ - `verifyWebhook(secret, headers, rawBody)` — checks a webhook delivery's signature and age.
1309
+ - `@sbuilder/sdk/storefront` — a shopper cart and the routes a front end on **another origin**
1310
+ may call; the ones that need the shopper's session cookie say so and name the proxy to mount.
1311
+
1312
+ Versioned on its own (`sdk-v*` tags); see [`sdk/README.md`](./sdk/README.md).
1313
+
1314
+ ---
1315
+
1020
1316
  ## License
1021
1317
 
1022
1318
  [MIT](./LICENSE) © Store Builder