@sbuilder/cli 0.12.0 → 0.13.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.
- package/README.md +61 -6
- package/README.vi.md +63 -7
- package/dist/bin/sb.js +47 -14
- package/dist/bin/sb.js.map +1 -1
- package/dist/src/agentdocs.js +163 -2
- package/dist/src/agentdocs.js.map +1 -1
- package/dist/src/appblocks.js +5 -1
- package/dist/src/appblocks.js.map +1 -1
- package/dist/src/components.js +226 -8
- package/dist/src/components.js.map +1 -1
- package/dist/src/doctor.js +28 -3
- package/dist/src/doctor.js.map +1 -1
- package/dist/src/envtypes.gen.js +6 -2
- package/dist/src/envtypes.gen.js.map +1 -1
- package/dist/src/host.js +31 -0
- package/dist/src/host.js.map +1 -1
- package/dist/src/login.js +1 -1
- package/dist/src/route.js +1 -1
- package/dist/src/route.js.map +1 -1
- package/dist/src/site.js +64 -10
- package/dist/src/site.js.map +1 -1
- package/dist/src/siteinit.js +168 -17
- package/dist/src/siteinit.js.map +1 -1
- package/dist/src/sitekinds.js +26 -7
- package/dist/src/sitekinds.js.map +1 -1
- package/dist/src/source/derived.js +96 -0
- package/dist/src/source/derived.js.map +1 -0
- package/dist/src/source/meta.js +3 -3
- package/dist/src/source/meta.js.map +1 -1
- package/dist/src/source/parse.js +3 -1
- package/dist/src/source/parse.js.map +1 -1
- package/dist/src/source/print.js +8 -1
- package/dist/src/source/print.js.map +1 -1
- package/package.json +1 -1
package/dist/src/agentdocs.js
CHANGED
|
@@ -7,8 +7,14 @@
|
|
|
7
7
|
* example below with parsePage; the app rules come from the server's own validators
|
|
8
8
|
* (server/internal/apps, webhooks.CheckURL), which tests/appblock.test.ts already runs.
|
|
9
9
|
*/
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
import { VERSION } from './login.js';
|
|
11
|
+
/**
|
|
12
|
+
* The CLI range the generated files pin (major.minor of THIS CLI), so CI and MCP never drift onto
|
|
13
|
+
* a version they were not written for. Derived, never typed: a literal here stayed at 0.11 while
|
|
14
|
+
* the package moved to 0.13, and every repo it wrote ran a CLI with no `sb site components`.
|
|
15
|
+
* VERSION is held to package.json by tests/auth.test.ts; tests/agentfiles.test.ts holds this to it.
|
|
16
|
+
*/
|
|
17
|
+
export const CLI_RANGE = VERSION.split('.').slice(0, 2).join('.');
|
|
12
18
|
/** Where the skills land, relative to the repo root. */
|
|
13
19
|
export const PAGE_SKILL_PATH = '.claude/skills/sbuilder-page/SKILL.md';
|
|
14
20
|
export const APP_SKILL_PATH = '.claude/skills/sbuilder-app/SKILL.md';
|
|
@@ -142,6 +148,161 @@ each with a line like \`<Tag appBlockRef="…" />\` to paste into a page file an
|
|
|
142
148
|
|
|
143
149
|
Run these as \`npx @sbuilder/cli@${CLI_RANGE} site <pull|push|publish|blocks>\`.
|
|
144
150
|
`;
|
|
151
|
+
export const COMPONENTS_SKILL_PATH = '.claude/skills/sbuilder-components/SKILL.md';
|
|
152
|
+
/**
|
|
153
|
+
* Code components. Every number and rule here is the platform's or the CLI's own, and
|
|
154
|
+
* tests/components.test.ts holds each to its source (server/internal/codecomponents, components.ts)
|
|
155
|
+
* and BUILDS the example with the real buildComponent and parses the page snippet with parsePage.
|
|
156
|
+
*/
|
|
157
|
+
export const COMPONENTS_SKILL = `---
|
|
158
|
+
name: sbuilder-components
|
|
159
|
+
description: Use when a page needs interactivity or markup no Store Builder element covers, or when editing components/*.tsx — when to write a code component, the file and props rules, the sandbox it renders in, placing it on a page, and the push/pin/upgrade/delete loop.
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
# Code components: your own React on a Store Builder page
|
|
163
|
+
|
|
164
|
+
A code component is a React (Preact) component in \`components/<Name>.tsx\`. The CLI bundles it,
|
|
165
|
+
the platform renders it to HTML on the server and stores that HTML with the page, and the
|
|
166
|
+
published page hydrates it in the browser.
|
|
167
|
+
|
|
168
|
+
## Native first
|
|
169
|
+
|
|
170
|
+
Build with the platform's elements first (the page skill, \`sb_catalog_search\`). They are
|
|
171
|
+
editable in the editor, responsive and translatable. Write a code component only for a gap you
|
|
172
|
+
can name: interactivity no element has (a calculator, a configurator, a quiz) or markup no
|
|
173
|
+
element can produce. It is not a way to paste a design in as HTML.
|
|
174
|
+
|
|
175
|
+
## The file
|
|
176
|
+
|
|
177
|
+
\`components/<Name>.tsx\` — the name matches \`^[A-Za-z][A-Za-z0-9_]{0,63}$\` (letters, digits, \`_\`;
|
|
178
|
+
starts with a letter). A file without \`export const schema\` is not a component and is skipped
|
|
179
|
+
(Next.js keeps its own components in the same folder). It exports its props as a LITERAL schema
|
|
180
|
+
and a default function component:
|
|
181
|
+
|
|
182
|
+
\`\`\`tsx
|
|
183
|
+
import { useState } from 'preact/hooks';
|
|
184
|
+
import { defineProps, type Props } from '@sbuilder/sdk/component';
|
|
185
|
+
|
|
186
|
+
export const schema = defineProps({
|
|
187
|
+
question: { kind: 'text', default: 'Giao hàng mất bao lâu?' },
|
|
188
|
+
answer: { kind: 'richtext', default: '<p>Hai đến ba ngày trong nội thành.</p>' },
|
|
189
|
+
open: { kind: 'boolean', default: false },
|
|
190
|
+
tone: { kind: 'enum', options: ['light', 'dark'], default: 'light' },
|
|
191
|
+
'gap-size': { kind: 'css', default: '12px' },
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
export default function Faq({ question, answer, open, tone }: Props<typeof schema>) {
|
|
195
|
+
const [shown, setShown] = useState(open);
|
|
196
|
+
return (
|
|
197
|
+
<section class={\`faq faq-\${tone}\`} style="display: grid; gap: var(--gap-size, 12px)">
|
|
198
|
+
<button type="button" aria-expanded={shown} onClick={() => setShown(!shown)}>
|
|
199
|
+
{question}
|
|
200
|
+
</button>
|
|
201
|
+
{shown && <div dangerouslySetInnerHTML={{ __html: answer }} />}
|
|
202
|
+
</section>
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
\`\`\`
|
|
206
|
+
|
|
207
|
+
\`sb site components init\` sets a repo up for this (esbuild, preact and \`@sbuilder/sdk\` as
|
|
208
|
+
devDependencies, a starter \`components/Hero.tsx\`); run \`npm install\` after it.
|
|
209
|
+
|
|
210
|
+
## Props
|
|
211
|
+
|
|
212
|
+
- **Content props** change the markup and are the same on every breakpoint: \`text\`, \`richtext\`
|
|
213
|
+
(HTML the platform sanitises), \`link\` and \`image\` (an http(s) or site-relative URL; an \`image\`
|
|
214
|
+
has no default), \`enum\` (with \`options\`), \`boolean\`, \`number\`. A prop name matches
|
|
215
|
+
\`^[A-Za-z][A-Za-z0-9_]{0,39}$\`; at most 50 props.
|
|
216
|
+
- **A length or a colour is a \`css\` prop.** It never reaches the component: the page sets it per
|
|
217
|
+
breakpoint as the custom property \`--<name>\`, and the markup reads \`var(--<name>, <default>)\`.
|
|
218
|
+
Its name is lowercase kebab-case (\`gap-size\`), never camelCase.
|
|
219
|
+
- **The schema must be a literal object.** \`sb site push\` reads it from the source without
|
|
220
|
+
running anything; a schema built by code is refused with the fix.
|
|
221
|
+
|
|
222
|
+
## Where it renders, and what that forbids
|
|
223
|
+
|
|
224
|
+
The platform renders the SSR bundle on every save that changes a node's component, version or
|
|
225
|
+
props, inside a sandbox, and the page keeps that HTML:
|
|
226
|
+
|
|
227
|
+
- the clock is frozen at 2000-01-01T00:00:00Z (\`Date.now()\`, \`new Date()\`), and \`Math.random\` is
|
|
228
|
+
seeded — the same sequence on every render;
|
|
229
|
+
- \`fetch\`, \`setTimeout\`, \`setInterval\`, \`crypto\` and \`queueMicrotask\` throw: no network, no
|
|
230
|
+
timers. Load data in the browser after hydration, or pass it in as props;
|
|
231
|
+
- no Server Components, no Suspense data, no \`use\`;
|
|
232
|
+
- one second per render.
|
|
233
|
+
|
|
234
|
+
**In the browser** the CLI's client bundle hydrates the stored markup on the PUBLISHED page with
|
|
235
|
+
the same props, so state and event handlers (\`useState\`, \`onClick\`) work there. The editor shows
|
|
236
|
+
the server-rendered markup only. A bundle that fails to load or throws while hydrating is logged
|
|
237
|
+
once and leaves the markup in place.
|
|
238
|
+
|
|
239
|
+
**Limits:** SSR bundle 512 KiB, client bundle 256 KiB (said by name before any upload), 200
|
|
240
|
+
live versions per component and 200 components per site.
|
|
241
|
+
|
|
242
|
+
## On a page
|
|
243
|
+
|
|
244
|
+
\`\`\`tsx
|
|
245
|
+
export default (
|
|
246
|
+
<Page id="ROOT">
|
|
247
|
+
<FlexSection id="fs_faq" name="FAQ">
|
|
248
|
+
<Heading id="he_faq" level={2} text="Câu hỏi thường gặp" />
|
|
249
|
+
<CodeComponent
|
|
250
|
+
id="cc_faq"
|
|
251
|
+
component="Faq"
|
|
252
|
+
props={{
|
|
253
|
+
answer: "<p>Hai đến ba ngày trong nội thành.</p>",
|
|
254
|
+
question: "Giao hàng mất bao lâu?",
|
|
255
|
+
tone: "light",
|
|
256
|
+
}}
|
|
257
|
+
version={3}
|
|
258
|
+
style={{ "--gap-size": "16px" }}
|
|
259
|
+
mobile={{ style: { "--gap-size": "8px" } }}
|
|
260
|
+
/>
|
|
261
|
+
</FlexSection>
|
|
262
|
+
</Page>
|
|
263
|
+
);
|
|
264
|
+
\`\`\`
|
|
265
|
+
|
|
266
|
+
\`component\` is the name, \`version\` the pinned number, \`props\` the content props, and each \`css\`
|
|
267
|
+
prop is a \`--<name>\` key in \`style\` (per breakpoint in \`tablet\` / \`mobile\`). **Never write \`html\`,
|
|
268
|
+
\`clientUrl\` or \`hydrateProps\`:** the platform renders them on every save, a file holding one is read without
|
|
269
|
+
it, and push says so ("rendered by the platform; your edit is ignored").
|
|
270
|
+
|
|
271
|
+
## The loop
|
|
272
|
+
|
|
273
|
+
1. Write or change \`components/<Name>.tsx\`.
|
|
274
|
+
2. \`npx @sbuilder/cli@${CLI_RANGE} doctor --build\` — the schema is a literal, esbuild is
|
|
275
|
+
installed, and every component bundles within the caps (this runs the repo's own esbuild).
|
|
276
|
+
3. \`npx @sbuilder/cli@${CLI_RANGE} site components preview <Name> --props '{"question":"Q?","open":true}'\`
|
|
277
|
+
— builds it here and has the platform render it ONCE, in the same sandbox, with those props
|
|
278
|
+
(\`--props @file.json\` reads them from a file; omitted ones take their default). It prints the
|
|
279
|
+
HTML a page save would store. A refusal prints its code and the sandbox's own message
|
|
280
|
+
(\`bundle_rejected\`, \`invalid_props\`, …) and exits 1: fix the component and preview again.
|
|
281
|
+
Nothing is uploaded and no page changes. A busy platform says when to try again; wait.
|
|
282
|
+
4. \`npx @sbuilder/cli@${CLI_RANGE} site push\` — uploads every component BEFORE any page and
|
|
283
|
+
prints the version the platform assigned (\`→ Faq version 3 (new, …)\`); identical content
|
|
284
|
+
gets the existing version back (\`unchanged\`). Preview, push and delete need the repo's API key
|
|
285
|
+
(\`.env\`).
|
|
286
|
+
5. Pin that version on the page (\`version={3}\`) and push again; the platform renders the node.
|
|
287
|
+
6. If push prints \`the platform says codeComponentRender (… nodeId …, reason …)\`, that node's
|
|
288
|
+
render failed: it is stored BLANK until a save renders it. Fix the component, preview it, push
|
|
289
|
+
it, push the page again.
|
|
290
|
+
7. A new version of a component pinned on many pages: \`site components upgrade <Name>\`
|
|
291
|
+
(or \`--to <n>\`) rewrites every pin in \`design/\`, names props the new version no longer takes,
|
|
292
|
+
and sends nothing — review the diff, then push.
|
|
293
|
+
8. \`site components list\` shows every component, its versions, sizes and which ones this repo
|
|
294
|
+
pins. \`site components delete <Name> <n> --dry-run\` first, then without it.
|
|
295
|
+
|
|
296
|
+
## Traps
|
|
297
|
+
|
|
298
|
+
- **Versions are immutable, and a number is never reused.** A deleted version's number never
|
|
299
|
+
comes back, so a pin can never come to mean different code.
|
|
300
|
+
- **A pinned version cannot be deleted:** the platform refuses while any draft or live page,
|
|
301
|
+
shared section or overlay uses it, and names the pages. Upgrade those pins first.
|
|
302
|
+
- **A failed render stores the node blank and warns** — never the old markup. Read push's output.
|
|
303
|
+
- **Nothing on a page moves by itself** when you upload a new version: pins change only by
|
|
304
|
+
\`upgrade\` or a hand edit.
|
|
305
|
+
`;
|
|
145
306
|
export const APP_SKILL = `---
|
|
146
307
|
name: sbuilder-app
|
|
147
308
|
description: Use when working on this Store Builder app (server.js, sb.json, blocks.json, islands/) — the dev loop, sb.json keys, the OAuth callback and embed, and the rules the platform enforces on a version.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"agentdocs.js","sourceRoot":"","sources":["../../src/agentdocs.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH
|
|
1
|
+
{"version":3,"file":"agentdocs.js","sourceRoot":"","sources":["../../src/agentdocs.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAElE,wDAAwD;AACxD,MAAM,CAAC,MAAM,eAAe,GAAG,uCAAuC,CAAC;AACvE,MAAM,CAAC,MAAM,cAAc,GAAG,sCAAsC,CAAC;AAErE,MAAM,CAAC,MAAM,UAAU,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mDAuGyB,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;mCAyBzB,SAAS;CAC3C,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,6CAA6C,CAAC;AAEnF;;;;GAIG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;yBAqHP,SAAS;;yBAET,SAAS;;;;;;yBAMT,SAAS;;;;;;;;;;;;;;;;;;;;;;;CAuBjC,CAAC;AAEF,MAAM,CAAC,MAAM,SAAS,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2ExB,CAAC"}
|
package/dist/src/appblocks.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
+
import { stripDerived } from './source/derived.js';
|
|
2
3
|
const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
3
4
|
/** JSON with object keys sorted, so the hash does not depend on key order. */
|
|
4
5
|
function canonical(v) {
|
|
@@ -27,7 +28,10 @@ function subtree(doc, root) {
|
|
|
27
28
|
return out;
|
|
28
29
|
}
|
|
29
30
|
/** The part of a block a merchant cannot change: everything but its settings and placement. */
|
|
30
|
-
function fingerprint(
|
|
31
|
+
function fingerprint(stored, root) {
|
|
32
|
+
// The seal is taken over the stored document and checked against a parsed file, which never
|
|
33
|
+
// holds the platform's derived specials (source/derived.ts): both sides drop them.
|
|
34
|
+
const doc = stripDerived(stored).document;
|
|
31
35
|
const nodes = subtree(doc, root).map((n) => {
|
|
32
36
|
if (n.id !== root)
|
|
33
37
|
return n;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"appblocks.js","sourceRoot":"","sources":["../../src/appblocks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"appblocks.js","sourceRoot":"","sources":["../../src/appblocks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAyBnD,MAAM,KAAK,GAAG,CAAC,CAAU,EAAY,EAAE,CAAC,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAEjG,8EAA8E;AAC9E,SAAS,SAAS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,EAAE,CAAC;IAC5F,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,+EAA+E;AAC/E,MAAM,UAAU,eAAe,CAC7B,GAAY,EACZ,KAA+C;IAE/C,MAAM,GAAG,GAAmB,EAAE,CAAC;IAC/B,KAAK,MAAM,CAAC,EAAE,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,CAAC;QACrD,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,EAAE,CAAC;YAAE,SAAS,CAAC,oCAAoC;QACpE,IAAI,WAAW,CAAC,GAAG,EAAE,EAAE,CAAC,KAAK,IAAI,CAAC,MAAM;YAAE,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAC3D,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,IAAY,EAAE,MAAsB,EAAE,EAAE,CACvE,OAAO,IAAI,oCAAoC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG;IAC9E,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,gDAAgD;IAC/F,+FAA+F;IAC/F,oCAAoC,CAAC"}
|
package/dist/src/components.js
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { existsSync } from 'node:fs';
|
|
2
2
|
import { lstat, mkdir, readdir, readFile, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { createRequire } from 'node:module';
|
|
4
|
-
import { join } from 'node:path';
|
|
4
|
+
import { dirname, join, resolve } from 'node:path';
|
|
5
5
|
import { sbJsonSite } from './code.js';
|
|
6
6
|
import { DevError } from './dev.js';
|
|
7
|
-
import {
|
|
7
|
+
import { COMPONENTS_SKILL, COMPONENTS_SKILL_PATH } from './agentdocs.js';
|
|
8
|
+
import { errorOf, linkOn, open, put, putIfStill, recordAgentFiles, refusal, send, sha256, SITE_MANIFEST, } from './site.js';
|
|
8
9
|
import { parseFile, printFile } from './source/meta.js';
|
|
9
10
|
import { parseSource } from './source/parse.js';
|
|
11
|
+
import { derivedWarning } from './source/derived.js';
|
|
10
12
|
/**
|
|
11
13
|
* `sb site components` — code components: React (Preact) components a developer writes in a site
|
|
12
14
|
* repo's `components/` folder, bundled HERE with the repo's own esbuild and uploaded as IMMUTABLE
|
|
@@ -150,12 +152,18 @@ export async function componentsInit(dir, log) {
|
|
|
150
152
|
for (const [file, body] of [
|
|
151
153
|
[`${COMPONENTS_DIR}/Hero.tsx`, HERO],
|
|
152
154
|
[`${COMPONENTS_DIR}/tsconfig.json`, TSCONFIG],
|
|
155
|
+
// What an AI agent reads before writing one (agentdocs.ts) — the same file sb site init writes.
|
|
156
|
+
[COMPONENTS_SKILL_PATH, COMPONENTS_SKILL],
|
|
153
157
|
]) {
|
|
154
158
|
await refuseLink(dir, file);
|
|
159
|
+
await mkdir(dirname(join(dir, file)), { recursive: true });
|
|
155
160
|
try {
|
|
156
161
|
await writeFile(join(dir, file), body, { encoding: 'utf8', flag: 'wx' });
|
|
157
162
|
out.wrote.push(file);
|
|
158
163
|
log(` + ${file}`);
|
|
164
|
+
// Recorded in a site repo, so `sb site agents --refresh` may update it later.
|
|
165
|
+
if (file === COMPONENTS_SKILL_PATH && existsSync(join(dir, SITE_MANIFEST)))
|
|
166
|
+
await recordAgentFiles(dir, { [file]: { sha256: sha256(body) } });
|
|
159
167
|
}
|
|
160
168
|
catch (err) {
|
|
161
169
|
if (err.code !== 'EEXIST')
|
|
@@ -331,6 +339,11 @@ export async function listComponents(dir, note) {
|
|
|
331
339
|
}
|
|
332
340
|
return out;
|
|
333
341
|
}
|
|
342
|
+
/** The two refusals a component upload waits out once (status + code), and how it waits — injectable for tests. */
|
|
343
|
+
const RENDER_BUSY = new Set(['429 site_render_busy', '503 renderer_busy']);
|
|
344
|
+
export const uploadTiming = {
|
|
345
|
+
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
346
|
+
};
|
|
334
347
|
/** Builds and uploads each component; the version the platform assigned is printed. */
|
|
335
348
|
export async function pushComponents(ctx, o) {
|
|
336
349
|
const out = { sent: 0, failed: 0 };
|
|
@@ -369,14 +382,25 @@ export async function pushComponents(ctx, o) {
|
|
|
369
382
|
out.sent++;
|
|
370
383
|
continue;
|
|
371
384
|
}
|
|
372
|
-
const
|
|
385
|
+
const upload = () => send(ctx, `${ctx.base}/code-components`, 'POST', {
|
|
373
386
|
name: built.name,
|
|
374
387
|
propsSchema: built.schema,
|
|
375
388
|
ssrBundle: built.ssr,
|
|
376
389
|
clientBundle: built.client,
|
|
377
390
|
});
|
|
378
|
-
|
|
379
|
-
|
|
391
|
+
let res = await upload();
|
|
392
|
+
let err = res.ok ? undefined : await errorOf(res);
|
|
393
|
+
// A slow test render cools the site's renders for a few seconds, so the NEXT upload of the same
|
|
394
|
+
// push can meet a busy renderer. That one case waits (Retry-After, at most 15 s) and tries ONCE
|
|
395
|
+
// more; anything else, or a second refusal, fails as before.
|
|
396
|
+
if (err && RENDER_BUSY.has(`${res.status} ${err.code}`)) {
|
|
397
|
+
const secs = Math.min(15, Math.max(0, Number(res.headers.get('retry-after')) || 5));
|
|
398
|
+
ctx.log(` … ${file}: the platform's renderer is busy (${err.code}) — waiting ${secs}s, then one retry.`);
|
|
399
|
+
await uploadTiming.sleep(secs * 1000);
|
|
400
|
+
res = await upload();
|
|
401
|
+
err = res.ok ? undefined : await errorOf(res);
|
|
402
|
+
}
|
|
403
|
+
if (err) {
|
|
380
404
|
const wait = res.headers.get('retry-after');
|
|
381
405
|
ctx.warn(` ! ${file}: refused (${refusal(err)})${res.status === 429 && wait ? ` — try again in ${wait}s` : ''}`);
|
|
382
406
|
out.failed++;
|
|
@@ -420,8 +444,9 @@ async function designFiles(dir) {
|
|
|
420
444
|
if (await linkOn(dir, file))
|
|
421
445
|
continue;
|
|
422
446
|
try {
|
|
423
|
-
const
|
|
424
|
-
|
|
447
|
+
const text = await readFile(join(dir, file), 'utf8');
|
|
448
|
+
const { document, meta, derived } = parseFile(text, kind);
|
|
449
|
+
out.push({ file, kind, doc: document, meta, derived, text });
|
|
425
450
|
}
|
|
426
451
|
catch {
|
|
427
452
|
// said by the files check; an upgrade must not touch a file it cannot read
|
|
@@ -464,6 +489,7 @@ export async function upgrade(o) {
|
|
|
464
489
|
const schema = target.propsSchema ?? {};
|
|
465
490
|
let changed = 0;
|
|
466
491
|
let files = 0;
|
|
492
|
+
let clashed = 0;
|
|
467
493
|
for (const f of await designFiles(ctx.dir)) {
|
|
468
494
|
const nodes = Object.entries(f.doc.nodes).filter(([id]) => pinsOf(f.doc).some((p) => p.id === id && p.name === o.name && p.version !== target.version));
|
|
469
495
|
if (!nodes.length)
|
|
@@ -481,9 +507,17 @@ export async function upgrade(o) {
|
|
|
481
507
|
ctx.warn(` ! ${f.file}: ${id} sets ${dropped.join(', ')}, which version ${target.version} does not take — the platform drops ${dropped.length > 1 ? 'them' : 'it'} on the next save.`);
|
|
482
508
|
}
|
|
483
509
|
files++;
|
|
510
|
+
// The rewrite prints the document as parsed, so a hand-typed html= goes with it: say so.
|
|
511
|
+
for (const d of f.derived)
|
|
512
|
+
ctx.warn(o.dryRun
|
|
513
|
+
? derivedWarning(f.file, d).replace('your edit is ignored', 'the rewrite would drop it')
|
|
514
|
+
: derivedWarning(f.file, d));
|
|
484
515
|
if (!o.dryRun) {
|
|
485
516
|
await refuseLink(ctx.dir, f.file);
|
|
486
|
-
await
|
|
517
|
+
if (!(await putIfStill(join(ctx.dir, f.file), f.text, printFile(f.kind, f.doc, f.meta)))) {
|
|
518
|
+
ctx.warn(` ! ${f.file}: changed while upgrading — kept yours, not rewritten. Run it again.`);
|
|
519
|
+
clashed++;
|
|
520
|
+
}
|
|
487
521
|
}
|
|
488
522
|
}
|
|
489
523
|
if (!changed) {
|
|
@@ -492,8 +526,192 @@ export async function upgrade(o) {
|
|
|
492
526
|
}
|
|
493
527
|
ctx.log(`${changed} pin(s) in ${files} file(s) ${o.dryRun ? 'would move' : 'moved'} to ${o.name} version ${target.version}. ` +
|
|
494
528
|
'html is derived: the platform re-renders it when you push. Review the diff, then sb site push (a draft).');
|
|
529
|
+
return clashed ? 1 : 0;
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* `sb site components preview <Name> [--props <json|@file>] [--json]`: builds the component here,
|
|
533
|
+
* exactly as push does, and asks the platform to render it once (POST
|
|
534
|
+
* /api/v1/code-components/preview) — the HTML a page save would store, with nothing uploaded and no
|
|
535
|
+
* page touched. A refusal (the sandbox's own error, a bad prop) is printed with its code and the
|
|
536
|
+
* exit is 1, so an agent can read it and fix the component. A busy platform is said with its
|
|
537
|
+
* Retry-After and never retried here.
|
|
538
|
+
*/
|
|
539
|
+
export async function preview(o) {
|
|
540
|
+
if (!NAME.test(o.name))
|
|
541
|
+
throw new ComponentRefused(`"${o.name}" is not a component name.`);
|
|
542
|
+
let props;
|
|
543
|
+
if (o.props !== undefined) {
|
|
544
|
+
const raw = o.props.startsWith('@')
|
|
545
|
+
? await readFile(resolve(o.props.slice(1)), 'utf8').catch(() => {
|
|
546
|
+
throw new ComponentRefused(`--props ${o.props}: the file could not be read.`);
|
|
547
|
+
})
|
|
548
|
+
: o.props;
|
|
549
|
+
let v;
|
|
550
|
+
try {
|
|
551
|
+
v = JSON.parse(raw);
|
|
552
|
+
}
|
|
553
|
+
catch (err) {
|
|
554
|
+
throw new ComponentRefused(`--props is not JSON (${err instanceof Error ? err.message : String(err)}). Write an object: --props '{"title":"Hi"}'`);
|
|
555
|
+
}
|
|
556
|
+
if (!isObj(v))
|
|
557
|
+
throw new ComponentRefused(`--props takes a JSON object: --props '{"title":"Hi"}'`);
|
|
558
|
+
props = v;
|
|
559
|
+
}
|
|
560
|
+
const file = `${COMPONENTS_DIR}/${o.name}.tsx`;
|
|
561
|
+
await refuseLink(o.dir, COMPONENTS_DIR);
|
|
562
|
+
await refuseLink(o.dir, file);
|
|
563
|
+
if (!existsSync(join(o.dir, file)))
|
|
564
|
+
throw new ComponentRefused(`${file} is not here — write it first (sb site components init starts one).`);
|
|
565
|
+
const built = await buildComponent(o.dir, `${o.name}.tsx`);
|
|
566
|
+
const size = `${(Buffer.byteLength(built.ssr) / 1024).toFixed(1)} KiB SSR`;
|
|
567
|
+
const log = o.log ?? console.log;
|
|
568
|
+
if (o.dryRun) {
|
|
569
|
+
log(` · ${file} builds (${size}); dry run — nothing sent, nothing rendered.`);
|
|
570
|
+
return 0;
|
|
571
|
+
}
|
|
572
|
+
const ctx = await open(o);
|
|
573
|
+
if (!ctx.v1)
|
|
574
|
+
throw new ComponentRefused('Previewing needs an API key (wbk_… with pages.write) in SB_TOKEN or .env — a browser sign-in cannot.');
|
|
575
|
+
const res = await send(ctx, `${ctx.base}/code-components/preview`, 'POST', {
|
|
576
|
+
ssrBundle: built.ssr,
|
|
577
|
+
propsSchema: built.schema,
|
|
578
|
+
...(props ? { props } : {}),
|
|
579
|
+
});
|
|
580
|
+
if (res.ok) {
|
|
581
|
+
const html = (await res.json()).html ?? '';
|
|
582
|
+
ctx.log(o.json ? JSON.stringify({ html }) : html);
|
|
583
|
+
return 0;
|
|
584
|
+
}
|
|
585
|
+
const err = await errorOf(res);
|
|
586
|
+
const wait = res.headers.get('retry-after');
|
|
587
|
+
ctx.warn(res.status === 429 || res.status === 503
|
|
588
|
+
? ` ! ${file}: the platform is busy (${refusal(err)})${wait ? ` — try again in ${wait}s` : ''}. Not retried.`
|
|
589
|
+
: ` ! ${file}: ${refusal(err)}`);
|
|
590
|
+
return 1;
|
|
591
|
+
}
|
|
592
|
+
const kib = (n) => `${(n / 1024).toFixed(1)} KiB`;
|
|
593
|
+
/**
|
|
594
|
+
* `sb site components list [--json]`: each component on the platform with its versions, sizes and
|
|
595
|
+
* dates, plus the versions THIS repo's files pin. Which pages pin a version on the platform (drafts
|
|
596
|
+
* and live pages) is not something the API lists; `sb site components delete` names them on refusal.
|
|
597
|
+
*/
|
|
598
|
+
export async function listRemote(o) {
|
|
599
|
+
const ctx = await open(o);
|
|
600
|
+
const read = async (url, what) => {
|
|
601
|
+
const res = await send(ctx, url, 'GET');
|
|
602
|
+
if (!res.ok)
|
|
603
|
+
throw new ComponentRefused(`Reading ${what} failed: ${refusal(await errorOf(res))}`);
|
|
604
|
+
return (await res.json());
|
|
605
|
+
};
|
|
606
|
+
const latest = (await read(`${ctx.base}/code-components`, 'the code components')).codeComponents ?? [];
|
|
607
|
+
const pins = await pinsInRepo(ctx.dir);
|
|
608
|
+
const out = [];
|
|
609
|
+
for (const c of [...latest].sort((a, b) => a.name.localeCompare(b.name))) {
|
|
610
|
+
// A component holds at most 200 live versions, which is also the page ceiling: one request.
|
|
611
|
+
const all = await read(`${ctx.base}/code-components/${encodeURIComponent(c.name)}/versions?limit=200`, c.name);
|
|
612
|
+
const here = new Map();
|
|
613
|
+
for (const p of pins.filter((p) => p.name === c.name))
|
|
614
|
+
here.set(p.version, (here.get(p.version) ?? new Set()).add(p.file));
|
|
615
|
+
out.push({
|
|
616
|
+
name: c.name,
|
|
617
|
+
latest: c.version,
|
|
618
|
+
count: all.total ?? all.codeComponents?.length ?? 0,
|
|
619
|
+
versions: (all.codeComponents ?? []).map((v) => ({
|
|
620
|
+
version: v.version,
|
|
621
|
+
ssrSize: v.ssrSize,
|
|
622
|
+
clientSize: v.clientSize,
|
|
623
|
+
createdAt: v.createdAt,
|
|
624
|
+
})),
|
|
625
|
+
pinnedHere: [...here.keys()]
|
|
626
|
+
.sort((a, b) => a - b)
|
|
627
|
+
.map((version) => ({ version, files: [...here.get(version)].sort() })),
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
if (o.json) {
|
|
631
|
+
ctx.log(JSON.stringify({ codeComponents: out }, null, 2));
|
|
632
|
+
return 0;
|
|
633
|
+
}
|
|
634
|
+
if (!out.length) {
|
|
635
|
+
ctx.log('No code components on the platform yet. sb site components init, then sb site push.');
|
|
636
|
+
return 0;
|
|
637
|
+
}
|
|
638
|
+
for (const c of out) {
|
|
639
|
+
ctx.log(`${c.name} latest version ${c.latest}, ${c.count} version(s)`);
|
|
640
|
+
for (const v of c.versions) {
|
|
641
|
+
const pin = c.pinnedHere.find((p) => p.version === v.version);
|
|
642
|
+
ctx.log(` v${v.version} ${kib(v.ssrSize)} SSR, ${kib(v.clientSize)} client ${v.createdAt}` +
|
|
643
|
+
(pin ? ` pinned here: ${pin.files.join(', ')}` : ''));
|
|
644
|
+
}
|
|
645
|
+
for (const p of c.pinnedHere)
|
|
646
|
+
if (!c.versions.some((v) => v.version === p.version))
|
|
647
|
+
ctx.warn(` ! v${p.version} is pinned by ${p.files.join(', ')} and is not on the platform (sb site components upgrade ${c.name})`);
|
|
648
|
+
}
|
|
495
649
|
return 0;
|
|
496
650
|
}
|
|
651
|
+
/**
|
|
652
|
+
* `sb site components delete <Name> <version> [--yes]`: deletes one version on the platform. Asked
|
|
653
|
+
* first on a terminal; off one, `--yes` is required. Refused (409 version_pinned) while any draft or
|
|
654
|
+
* live page, shared section or overlay pins it — the pages the platform names are printed and the
|
|
655
|
+
* exit is non-zero. A deleted number is never reused: the next upload gets a new one.
|
|
656
|
+
*/
|
|
657
|
+
export async function deleteRemote(o) {
|
|
658
|
+
if (!NAME.test(o.name))
|
|
659
|
+
throw new ComponentRefused(`"${o.name}" is not a component name.`);
|
|
660
|
+
if (!Number.isInteger(o.version) || o.version < 1)
|
|
661
|
+
throw new ComponentRefused('Name the version to delete: sb site components delete Hero 3');
|
|
662
|
+
const ctx = await open(o);
|
|
663
|
+
if (!ctx.v1)
|
|
664
|
+
throw new ComponentRefused('Deleting a version needs an API key (wbk_… with pages.write) in SB_TOKEN or .env — a browser sign-in cannot.');
|
|
665
|
+
const what = `${o.name} version ${o.version}`;
|
|
666
|
+
const versionURL = `${ctx.base}/code-components/${encodeURIComponent(o.name)}/versions/${o.version}`;
|
|
667
|
+
for (const p of (await pinsInRepo(ctx.dir)).filter((p) => p.name === o.name && p.version === o.version))
|
|
668
|
+
ctx.warn(` ! ${p.file}: node ${p.node} pins ${what} — once it is deleted, pushing this file renders that node empty.`);
|
|
669
|
+
if (o.dryRun) {
|
|
670
|
+
// A read, so a missing version is said as missing; whether a page pins it only the real
|
|
671
|
+
// DELETE can tell (the platform checks drafts and live pages then).
|
|
672
|
+
const got = await send(ctx, versionURL, 'GET');
|
|
673
|
+
if (got.status === 404) {
|
|
674
|
+
ctx.warn(` ! the platform has no ${what}; nothing would be deleted.`);
|
|
675
|
+
return 1;
|
|
676
|
+
}
|
|
677
|
+
if (!got.ok) {
|
|
678
|
+
ctx.warn(` ! reading ${what} failed (${refusal(await errorOf(got))}) — nothing sent.`);
|
|
679
|
+
return 1;
|
|
680
|
+
}
|
|
681
|
+
ctx.log(` ✗ ${what} would be deleted (dry run) — nothing sent. Only the real delete can tell whether a page still pins it.`);
|
|
682
|
+
return 0;
|
|
683
|
+
}
|
|
684
|
+
if (!o.yes) {
|
|
685
|
+
if (!o.confirm) {
|
|
686
|
+
ctx.warn(` ! off a terminal, deleting ${what} needs --yes; nothing deleted.`);
|
|
687
|
+
return 1;
|
|
688
|
+
}
|
|
689
|
+
if (!(await o.confirm(`Delete ${what} from the platform? Its number is never reused. [y/N] `))) {
|
|
690
|
+
ctx.log('Nothing deleted.');
|
|
691
|
+
return 0;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
const res = await send(ctx, versionURL, 'DELETE');
|
|
695
|
+
if (res.ok) {
|
|
696
|
+
ctx.log(` ✗ ${what} deleted. Its number is never reused: the next upload gets a new one.`);
|
|
697
|
+
return 0;
|
|
698
|
+
}
|
|
699
|
+
const err = await errorOf(res);
|
|
700
|
+
if (res.status === 409 && err.code === 'version_pinned') {
|
|
701
|
+
const pages = err.pages ?? [];
|
|
702
|
+
ctx.warn(` ! ${what} is still used — not deleted. Move these off it first (sb site components upgrade ${o.name}, then sb site push):`);
|
|
703
|
+
// A shared section or overlay pin may come without a page id: no empty "()" for it.
|
|
704
|
+
for (const p of pages)
|
|
705
|
+
ctx.warn(` - ${p.name || p.pageId || '?'}${p.pageId ? ` (${p.pageId})` : ''}`);
|
|
706
|
+
if (!pages.length)
|
|
707
|
+
ctx.warn(` ${err.error}`);
|
|
708
|
+
return 1;
|
|
709
|
+
}
|
|
710
|
+
ctx.warn(res.status === 404
|
|
711
|
+
? ` ! the platform has no ${what}; nothing deleted (sb site components list shows what it has).`
|
|
712
|
+
: ` ! ${what} was not deleted (${refusal(err)})`);
|
|
713
|
+
return 1;
|
|
714
|
+
}
|
|
497
715
|
// ── doctor ────────────────────────────────────────────────────────────────────────────────────
|
|
498
716
|
const make = (id, title) => (status, detail, fix = null) => ({
|
|
499
717
|
id,
|