@sbuilder/cli 0.12.0 → 0.14.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 +116 -13
- package/README.vi.md +115 -13
- package/dist/bin/sb.js +221 -41
- package/dist/bin/sb.js.map +1 -1
- package/dist/src/agentdocs.js +250 -8
- 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 +400 -24
- package/dist/src/components.js.map +1 -1
- package/dist/src/doctor.js +37 -3
- package/dist/src/doctor.js.map +1 -1
- package/dist/src/envtypes.gen.js +2929 -271
- 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/preview.js +114 -0
- package/dist/src/preview.js.map +1 -0
- package/dist/src/route.js +4 -2
- package/dist/src/route.js.map +1 -1
- package/dist/src/site.js +164 -22
- package/dist/src/site.js.map +1 -1
- package/dist/src/siteinit.js +381 -74
- package/dist/src/siteinit.js.map +1 -1
- package/dist/src/sitekinds.js +56 -13
- 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 +6 -2
- 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/dist/src/structure.js +114 -0
- package/dist/src/structure.js.map +1 -0
- package/dist/src/templates.js +93 -0
- package/dist/src/templates.js.map +1 -0
- package/dist/src/themediff.js +49 -0
- package/dist/src/themediff.js.map +1 -0
- package/package.json +1 -1
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
|
|
@@ -61,27 +63,163 @@ export class ComponentRefused extends DevError {
|
|
|
61
63
|
}
|
|
62
64
|
const HERO = `import { defineProps, type Props } from '@sbuilder/sdk/component';
|
|
63
65
|
|
|
64
|
-
// The props the editor shows for this component.
|
|
65
|
-
//
|
|
66
|
-
// var(--<name>, <default>).
|
|
66
|
+
// The props the editor shows for this component. Content props (text, link, enum…) change the
|
|
67
|
+
// markup and are the same on every breakpoint. A length or a colour is a \`css\` prop: the page sets
|
|
68
|
+
// it per breakpoint as the custom property --<name>, and the markup reads var(--<name>, <default>).
|
|
69
|
+
// A css prop's name is lowercase kebab-case.
|
|
67
70
|
export const schema = defineProps({
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
eyebrow: { kind: 'text', label: 'Eyebrow', default: 'New season' },
|
|
72
|
+
title: { kind: 'text', label: 'Title', default: 'Made by hand, made to last' },
|
|
73
|
+
// h1 when this hero opens the page; h2 when the page already has its h1.
|
|
74
|
+
level: { kind: 'enum', label: 'Title level', options: ['h1', 'h2'], default: 'h1' },
|
|
75
|
+
subtitle: {
|
|
76
|
+
kind: 'text',
|
|
77
|
+
label: 'Subtitle',
|
|
78
|
+
default: 'Small-batch goods from our workshop, packed with care and shipped in two days.',
|
|
79
|
+
},
|
|
80
|
+
cta: { kind: 'text', label: 'Button', default: 'Shop the collection' },
|
|
81
|
+
ctaLink: { kind: 'link', label: 'Button link', default: '/' },
|
|
82
|
+
secondary: { kind: 'text', label: 'Second button (blank hides it)', default: 'Our story' },
|
|
83
|
+
secondaryLink: { kind: 'link', label: 'Second button link', default: '/' },
|
|
84
|
+
tone: { kind: 'enum', label: 'Tone', options: ['light', 'dark'], default: 'light' },
|
|
85
|
+
align: { kind: 'enum', label: 'Align', options: ['center', 'start'], default: 'center' },
|
|
86
|
+
'accent-color': { kind: 'css', label: 'Button colour', default: '#171717' },
|
|
87
|
+
'accent-text': { kind: 'css', label: 'Button text colour', default: '#ffffff' },
|
|
88
|
+
'max-width': { kind: 'css', label: 'Text width', default: '720px' },
|
|
89
|
+
'padding-block': { kind: 'css', label: 'Space above and below', default: '96px' },
|
|
73
90
|
});
|
|
74
91
|
|
|
75
|
-
|
|
92
|
+
// Plain CSS, scoped by class. Rendered once per hero; no network, no fonts of its own (it inherits
|
|
93
|
+
// the site's).
|
|
94
|
+
const CSS = \`
|
|
95
|
+
.sb-hero{--fg:#171717;--muted:#525252;--bg:#fafafa;background:var(--bg);color:var(--fg);padding:var(--padding-block,96px) 24px}
|
|
96
|
+
.sb-hero-dark{--fg:#fafafa;--muted:#a3a3a3;--bg:#0a0a0a}
|
|
97
|
+
.sb-hero-in{max-width:var(--max-width,720px);margin:0 auto;display:grid;gap:20px}
|
|
98
|
+
.sb-hero-center .sb-hero-in{text-align:center;justify-items:center}
|
|
99
|
+
.sb-hero-eyebrow{margin:0;font-size:13px;font-weight:600;letter-spacing:.08em;text-transform:uppercase;color:var(--muted)}
|
|
100
|
+
.sb-hero-title{margin:0;font-size:clamp(36px,6vw,60px);line-height:1.05;letter-spacing:-.02em}
|
|
101
|
+
.sb-hero-sub{margin:0;max-width:60ch;font-size:18px;line-height:1.6;color:var(--muted)}
|
|
102
|
+
.sb-hero-actions{display:flex;flex-wrap:wrap;gap:12px;margin-top:8px}
|
|
103
|
+
.sb-hero-center .sb-hero-actions{justify-content:center}
|
|
104
|
+
.sb-hero a{display:inline-flex;align-items:center;min-height:44px;padding:0 22px;border-radius:999px;font-weight:600;text-decoration:none}
|
|
105
|
+
.sb-hero-primary{background:var(--accent-color,#171717);color:var(--accent-text,#ffffff)}
|
|
106
|
+
.sb-hero-dark .sb-hero-primary{background:var(--accent-color,#fafafa);color:var(--accent-text,#0a0a0a)}
|
|
107
|
+
.sb-hero-secondary{color:inherit;box-shadow:inset 0 0 0 1px currentColor}
|
|
108
|
+
.sb-hero a:focus-visible{outline:2px solid currentColor;outline-offset:3px}
|
|
109
|
+
\`;
|
|
110
|
+
|
|
111
|
+
export default function Hero(p: Props<typeof schema>) {
|
|
112
|
+
const Title = p.level;
|
|
76
113
|
return (
|
|
77
|
-
<section class={\`hero hero-\${tone}\`}
|
|
78
|
-
<
|
|
79
|
-
<
|
|
80
|
-
|
|
114
|
+
<section class={\`sb-hero sb-hero-\${p.tone} sb-hero-\${p.align}\`}>
|
|
115
|
+
<style dangerouslySetInnerHTML={{ __html: CSS }} />
|
|
116
|
+
<div class="sb-hero-in">
|
|
117
|
+
{p.eyebrow && <p class="sb-hero-eyebrow">{p.eyebrow}</p>}
|
|
118
|
+
<Title class="sb-hero-title">{p.title}</Title>
|
|
119
|
+
{p.subtitle && <p class="sb-hero-sub">{p.subtitle}</p>}
|
|
120
|
+
<div class="sb-hero-actions">
|
|
121
|
+
<a class="sb-hero-primary" href={p.ctaLink}>
|
|
122
|
+
{p.cta}
|
|
123
|
+
</a>
|
|
124
|
+
{p.secondary && (
|
|
125
|
+
<a class="sb-hero-secondary" href={p.secondaryLink}>
|
|
126
|
+
{p.secondary}
|
|
127
|
+
</a>
|
|
128
|
+
)}
|
|
129
|
+
</div>
|
|
130
|
+
</div>
|
|
81
131
|
</section>
|
|
82
132
|
);
|
|
83
133
|
}
|
|
84
134
|
`;
|
|
135
|
+
/**
|
|
136
|
+
* The second example: an FAQ accordion that HYDRATES. On the server (and with JavaScript off) every
|
|
137
|
+
* answer is open, so nothing is unreachable; once the published page hydrates it, useEffect turns
|
|
138
|
+
* it into an accordion. Deterministic: no clock, no randomness, no network at render.
|
|
139
|
+
*/
|
|
140
|
+
const FAQ = `import { useEffect, useState } from 'preact/hooks';
|
|
141
|
+
import { defineProps, type Props } from '@sbuilder/sdk/component';
|
|
142
|
+
|
|
143
|
+
// \`anchor\` builds the element ids: give each FAQ on one page its own.
|
|
144
|
+
export const schema = defineProps({
|
|
145
|
+
heading: { kind: 'text', label: 'Heading', default: 'Frequently asked questions' },
|
|
146
|
+
anchor: { kind: 'text', label: 'Anchor (unique on the page)', default: 'faq' },
|
|
147
|
+
q1: { kind: 'text', label: 'Question 1', default: 'How long does delivery take?' },
|
|
148
|
+
a1: { kind: 'richtext', label: 'Answer 1', default: '<p>Two to three days in the city, up to five elsewhere.</p>' },
|
|
149
|
+
q2: { kind: 'text', label: 'Question 2', default: 'Can I return an item?' },
|
|
150
|
+
a2: { kind: 'richtext', label: 'Answer 2', default: '<p>Yes — within 14 days, unused and in its box.</p>' },
|
|
151
|
+
q3: { kind: 'text', label: 'Question 3', default: 'How do I pay?' },
|
|
152
|
+
a3: { kind: 'richtext', label: 'Answer 3', default: '<p>By card, bank transfer or cash on delivery.</p>' },
|
|
153
|
+
q4: { kind: 'text', label: 'Question 4 (blank hides it)', default: '' },
|
|
154
|
+
a4: { kind: 'richtext', label: 'Answer 4', default: '' },
|
|
155
|
+
'accent-color': { kind: 'css', label: 'Accent colour', default: '#171717' },
|
|
156
|
+
'max-width': { kind: 'css', label: 'Width', default: '720px' },
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const CSS = \`
|
|
160
|
+
.sb-faq{max-width:var(--max-width,720px);margin:0 auto;padding:64px 24px}
|
|
161
|
+
.sb-faq h2{margin:0 0 24px;font-size:clamp(28px,4vw,36px);letter-spacing:-.01em}
|
|
162
|
+
.sb-faq-item{border-top:1px solid rgba(0,0,0,.12)}
|
|
163
|
+
.sb-faq-item:last-child{border-bottom:1px solid rgba(0,0,0,.12)}
|
|
164
|
+
.sb-faq h3{margin:0;font-size:inherit}
|
|
165
|
+
.sb-faq button{all:unset;box-sizing:border-box;display:flex;width:100%;justify-content:space-between;gap:16px;padding:18px 0;font-size:17px;font-weight:600;cursor:pointer}
|
|
166
|
+
.sb-faq button:focus-visible{outline:2px solid var(--accent-color,#171717);outline-offset:2px}
|
|
167
|
+
.sb-faq-mark{color:var(--accent-color,#171717)}
|
|
168
|
+
.sb-faq-a{padding:0 0 18px;line-height:1.6;opacity:.8}
|
|
169
|
+
.sb-faq-a[hidden]{display:none}
|
|
170
|
+
\`;
|
|
171
|
+
|
|
172
|
+
export default function Faq(p: Props<typeof schema>) {
|
|
173
|
+
const items = [
|
|
174
|
+
[p.q1, p.a1],
|
|
175
|
+
[p.q2, p.a2],
|
|
176
|
+
[p.q3, p.a3],
|
|
177
|
+
[p.q4, p.a4],
|
|
178
|
+
].filter(([q]) => q);
|
|
179
|
+
// Not ready on the server: every answer is shown. In the browser, after hydration, it collapses.
|
|
180
|
+
const [ready, setReady] = useState(false);
|
|
181
|
+
const [open, setOpen] = useState(0);
|
|
182
|
+
useEffect(() => setReady(true), []);
|
|
183
|
+
return (
|
|
184
|
+
<section class="sb-faq" id={p.anchor} aria-labelledby={\`\${p.anchor}-title\`}>
|
|
185
|
+
<style dangerouslySetInnerHTML={{ __html: CSS }} />
|
|
186
|
+
<h2 id={\`\${p.anchor}-title\`}>{p.heading}</h2>
|
|
187
|
+
{items.map(([q, a], i) => {
|
|
188
|
+
const id = \`\${p.anchor}-\${i + 1}\`;
|
|
189
|
+
const shown = !ready || open === i;
|
|
190
|
+
return (
|
|
191
|
+
<div class="sb-faq-item" key={id}>
|
|
192
|
+
<h3>
|
|
193
|
+
<button
|
|
194
|
+
type="button"
|
|
195
|
+
id={\`\${id}-q\`}
|
|
196
|
+
aria-expanded={shown}
|
|
197
|
+
aria-controls={\`\${id}-a\`}
|
|
198
|
+
onClick={() => setOpen(open === i ? -1 : i)}
|
|
199
|
+
>
|
|
200
|
+
{q}
|
|
201
|
+
<span class="sb-faq-mark" aria-hidden="true">
|
|
202
|
+
{shown ? '−' : '+'}
|
|
203
|
+
</span>
|
|
204
|
+
</button>
|
|
205
|
+
</h3>
|
|
206
|
+
<div
|
|
207
|
+
class="sb-faq-a"
|
|
208
|
+
id={\`\${id}-a\`}
|
|
209
|
+
role="region"
|
|
210
|
+
aria-labelledby={\`\${id}-q\`}
|
|
211
|
+
hidden={!shown}
|
|
212
|
+
dangerouslySetInnerHTML={{ __html: a }}
|
|
213
|
+
/>
|
|
214
|
+
</div>
|
|
215
|
+
);
|
|
216
|
+
})}
|
|
217
|
+
</section>
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
`;
|
|
221
|
+
/** The example sources init writes: one byte-for-byte equal to these is an untouched starter. */
|
|
222
|
+
export const STARTERS = new Set([HERO, FAQ]);
|
|
85
223
|
const TSCONFIG = `{
|
|
86
224
|
"compilerOptions": {
|
|
87
225
|
"target": "ES2020",
|
|
@@ -107,7 +245,7 @@ const refuseLink = async (dir, file) => {
|
|
|
107
245
|
if (l)
|
|
108
246
|
throw new ComponentRefused(`${l} is a symbolic link — nothing was read or written. Replace it with the real file.`);
|
|
109
247
|
};
|
|
110
|
-
/** `sb site components init`: devDependencies, components/Hero.tsx, components/tsconfig.json. */
|
|
248
|
+
/** `sb site components init`: devDependencies, components/Hero.tsx + Faq.tsx, components/tsconfig.json. */
|
|
111
249
|
export async function componentsInit(dir, log) {
|
|
112
250
|
const out = { wrote: [], kept: [] };
|
|
113
251
|
for (const f of ['package.json', COMPONENTS_DIR, `${COMPONENTS_DIR}/Hero.tsx`])
|
|
@@ -149,13 +287,20 @@ export async function componentsInit(dir, log) {
|
|
|
149
287
|
await mkdir(join(dir, COMPONENTS_DIR), { recursive: true });
|
|
150
288
|
for (const [file, body] of [
|
|
151
289
|
[`${COMPONENTS_DIR}/Hero.tsx`, HERO],
|
|
290
|
+
[`${COMPONENTS_DIR}/Faq.tsx`, FAQ],
|
|
152
291
|
[`${COMPONENTS_DIR}/tsconfig.json`, TSCONFIG],
|
|
292
|
+
// What an AI agent reads before writing one (agentdocs.ts) — the same file sb site init writes.
|
|
293
|
+
[COMPONENTS_SKILL_PATH, COMPONENTS_SKILL],
|
|
153
294
|
]) {
|
|
154
295
|
await refuseLink(dir, file);
|
|
296
|
+
await mkdir(dirname(join(dir, file)), { recursive: true });
|
|
155
297
|
try {
|
|
156
298
|
await writeFile(join(dir, file), body, { encoding: 'utf8', flag: 'wx' });
|
|
157
299
|
out.wrote.push(file);
|
|
158
300
|
log(` + ${file}`);
|
|
301
|
+
// Recorded in a site repo, so `sb site agents --refresh` may update it later.
|
|
302
|
+
if (file === COMPONENTS_SKILL_PATH && existsSync(join(dir, SITE_MANIFEST)))
|
|
303
|
+
await recordAgentFiles(dir, { [file]: { sha256: sha256(body) } });
|
|
159
304
|
}
|
|
160
305
|
catch (err) {
|
|
161
306
|
if (err.code !== 'EEXIST')
|
|
@@ -164,8 +309,8 @@ export async function componentsInit(dir, log) {
|
|
|
164
309
|
log(` · ${file} exists — kept`);
|
|
165
310
|
}
|
|
166
311
|
}
|
|
167
|
-
log('Next: npm install, then edit components/Hero.tsx and run sb site push — it
|
|
168
|
-
'component before the pages. Then drop it on a page from the editor (Code component).');
|
|
312
|
+
log('Next: npm install, then edit components/Hero.tsx or components/Faq.tsx and run sb site push — it\n' +
|
|
313
|
+
'bundles and uploads each component before the pages. Then drop it on a page from the editor (Code component).');
|
|
169
314
|
return out;
|
|
170
315
|
}
|
|
171
316
|
function loadEsbuild(dir) {
|
|
@@ -331,6 +476,11 @@ export async function listComponents(dir, note) {
|
|
|
331
476
|
}
|
|
332
477
|
return out;
|
|
333
478
|
}
|
|
479
|
+
/** The two refusals a component upload waits out once (status + code), and how it waits — injectable for tests. */
|
|
480
|
+
const RENDER_BUSY = new Set(['429 site_render_busy', '503 renderer_busy']);
|
|
481
|
+
export const uploadTiming = {
|
|
482
|
+
sleep: (ms) => new Promise((r) => setTimeout(r, ms)),
|
|
483
|
+
};
|
|
334
484
|
/** Builds and uploads each component; the version the platform assigned is printed. */
|
|
335
485
|
export async function pushComponents(ctx, o) {
|
|
336
486
|
const out = { sent: 0, failed: 0 };
|
|
@@ -344,6 +494,18 @@ export async function pushComponents(ctx, o) {
|
|
|
344
494
|
ctx.warn(` ! ${err.message}`);
|
|
345
495
|
return { sent: 0, failed: 1 };
|
|
346
496
|
}
|
|
497
|
+
// An untouched starter from `sb site components init` that no page names is not uploaded: the
|
|
498
|
+
// examples are there to copy, and pushing them would give every repo two components it never used.
|
|
499
|
+
const used = new Set((await designFiles(ctx.dir)).flatMap((d) => namesIn(d.doc)));
|
|
500
|
+
const skipped = [];
|
|
501
|
+
for (const f of files) {
|
|
502
|
+
const text = await readFile(join(ctx.dir, COMPONENTS_DIR, f), 'utf8').catch(() => '');
|
|
503
|
+
if (STARTERS.has(text) && !used.has(f.replace(/\.tsx$/, '')))
|
|
504
|
+
skipped.push(f);
|
|
505
|
+
}
|
|
506
|
+
for (const f of skipped)
|
|
507
|
+
ctx.log(` · ${COMPONENTS_DIR}/${f}: the untouched example from sb site components init, on no page — not uploaded (edit it or place it to upload it; delete it if you do not want it).`);
|
|
508
|
+
files = files.filter((f) => !skipped.includes(f));
|
|
347
509
|
if (!files.length)
|
|
348
510
|
return out;
|
|
349
511
|
if (!ctx.v1 && !o.dryRun) {
|
|
@@ -369,14 +531,25 @@ export async function pushComponents(ctx, o) {
|
|
|
369
531
|
out.sent++;
|
|
370
532
|
continue;
|
|
371
533
|
}
|
|
372
|
-
const
|
|
534
|
+
const upload = () => send(ctx, `${ctx.base}/code-components`, 'POST', {
|
|
373
535
|
name: built.name,
|
|
374
536
|
propsSchema: built.schema,
|
|
375
537
|
ssrBundle: built.ssr,
|
|
376
538
|
clientBundle: built.client,
|
|
377
539
|
});
|
|
378
|
-
|
|
379
|
-
|
|
540
|
+
let res = await upload();
|
|
541
|
+
let err = res.ok ? undefined : await errorOf(res);
|
|
542
|
+
// A slow test render cools the site's renders for a few seconds, so the NEXT upload of the same
|
|
543
|
+
// push can meet a busy renderer. That one case waits (Retry-After, at most 15 s) and tries ONCE
|
|
544
|
+
// more; anything else, or a second refusal, fails as before.
|
|
545
|
+
if (err && RENDER_BUSY.has(`${res.status} ${err.code}`)) {
|
|
546
|
+
const secs = Math.min(15, Math.max(0, Number(res.headers.get('retry-after')) || 5));
|
|
547
|
+
ctx.log(` … ${file}: the platform's renderer is busy (${err.code}) — waiting ${secs}s, then one retry.`);
|
|
548
|
+
await uploadTiming.sleep(secs * 1000);
|
|
549
|
+
res = await upload();
|
|
550
|
+
err = res.ok ? undefined : await errorOf(res);
|
|
551
|
+
}
|
|
552
|
+
if (err) {
|
|
380
553
|
const wait = res.headers.get('retry-after');
|
|
381
554
|
ctx.warn(` ! ${file}: refused (${refusal(err)})${res.status === 429 && wait ? ` — try again in ${wait}s` : ''}`);
|
|
382
555
|
out.failed++;
|
|
@@ -396,6 +569,15 @@ const KINDS = [
|
|
|
396
569
|
['design/overlays', 'overlay'],
|
|
397
570
|
];
|
|
398
571
|
const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
572
|
+
/** Every component a document names (`<CodeComponent component="…">`), pinned to a version or not. */
|
|
573
|
+
function namesIn(doc) {
|
|
574
|
+
return Object.values(doc.nodes ?? {}).flatMap((n) => isObj(n.data) &&
|
|
575
|
+
n.data.type === 'code-component' &&
|
|
576
|
+
isObj(n.specials) &&
|
|
577
|
+
typeof n.specials.component === 'string'
|
|
578
|
+
? [n.specials.component]
|
|
579
|
+
: []);
|
|
580
|
+
}
|
|
399
581
|
/** Every code-component node of a document: its pinned name and version. */
|
|
400
582
|
export function pinsOf(doc) {
|
|
401
583
|
const out = [];
|
|
@@ -420,8 +602,9 @@ async function designFiles(dir) {
|
|
|
420
602
|
if (await linkOn(dir, file))
|
|
421
603
|
continue;
|
|
422
604
|
try {
|
|
423
|
-
const
|
|
424
|
-
|
|
605
|
+
const text = await readFile(join(dir, file), 'utf8');
|
|
606
|
+
const { document, meta, derived } = parseFile(text, kind);
|
|
607
|
+
out.push({ file, kind, doc: document, meta, derived, text });
|
|
425
608
|
}
|
|
426
609
|
catch {
|
|
427
610
|
// said by the files check; an upgrade must not touch a file it cannot read
|
|
@@ -464,6 +647,7 @@ export async function upgrade(o) {
|
|
|
464
647
|
const schema = target.propsSchema ?? {};
|
|
465
648
|
let changed = 0;
|
|
466
649
|
let files = 0;
|
|
650
|
+
let clashed = 0;
|
|
467
651
|
for (const f of await designFiles(ctx.dir)) {
|
|
468
652
|
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
653
|
if (!nodes.length)
|
|
@@ -481,9 +665,17 @@ export async function upgrade(o) {
|
|
|
481
665
|
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
666
|
}
|
|
483
667
|
files++;
|
|
668
|
+
// The rewrite prints the document as parsed, so a hand-typed html= goes with it: say so.
|
|
669
|
+
for (const d of f.derived)
|
|
670
|
+
ctx.warn(o.dryRun
|
|
671
|
+
? derivedWarning(f.file, d).replace('your edit is ignored', 'the rewrite would drop it')
|
|
672
|
+
: derivedWarning(f.file, d));
|
|
484
673
|
if (!o.dryRun) {
|
|
485
674
|
await refuseLink(ctx.dir, f.file);
|
|
486
|
-
await
|
|
675
|
+
if (!(await putIfStill(join(ctx.dir, f.file), f.text, printFile(f.kind, f.doc, f.meta)))) {
|
|
676
|
+
ctx.warn(` ! ${f.file}: changed while upgrading — kept yours, not rewritten. Run it again.`);
|
|
677
|
+
clashed++;
|
|
678
|
+
}
|
|
487
679
|
}
|
|
488
680
|
}
|
|
489
681
|
if (!changed) {
|
|
@@ -492,8 +684,192 @@ export async function upgrade(o) {
|
|
|
492
684
|
}
|
|
493
685
|
ctx.log(`${changed} pin(s) in ${files} file(s) ${o.dryRun ? 'would move' : 'moved'} to ${o.name} version ${target.version}. ` +
|
|
494
686
|
'html is derived: the platform re-renders it when you push. Review the diff, then sb site push (a draft).');
|
|
687
|
+
return clashed ? 1 : 0;
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* `sb site components preview <Name> [--props <json|@file>] [--json]`: builds the component here,
|
|
691
|
+
* exactly as push does, and asks the platform to render it once (POST
|
|
692
|
+
* /api/v1/code-components/preview) — the HTML a page save would store, with nothing uploaded and no
|
|
693
|
+
* page touched. A refusal (the sandbox's own error, a bad prop) is printed with its code and the
|
|
694
|
+
* exit is 1, so an agent can read it and fix the component. A busy platform is said with its
|
|
695
|
+
* Retry-After and never retried here.
|
|
696
|
+
*/
|
|
697
|
+
export async function preview(o) {
|
|
698
|
+
if (!NAME.test(o.name))
|
|
699
|
+
throw new ComponentRefused(`"${o.name}" is not a component name.`);
|
|
700
|
+
let props;
|
|
701
|
+
if (o.props !== undefined) {
|
|
702
|
+
const raw = o.props.startsWith('@')
|
|
703
|
+
? await readFile(resolve(o.props.slice(1)), 'utf8').catch(() => {
|
|
704
|
+
throw new ComponentRefused(`--props ${o.props}: the file could not be read.`);
|
|
705
|
+
})
|
|
706
|
+
: o.props;
|
|
707
|
+
let v;
|
|
708
|
+
try {
|
|
709
|
+
v = JSON.parse(raw);
|
|
710
|
+
}
|
|
711
|
+
catch (err) {
|
|
712
|
+
throw new ComponentRefused(`--props is not JSON (${err instanceof Error ? err.message : String(err)}). Write an object: --props '{"title":"Hi"}'`);
|
|
713
|
+
}
|
|
714
|
+
if (!isObj(v))
|
|
715
|
+
throw new ComponentRefused(`--props takes a JSON object: --props '{"title":"Hi"}'`);
|
|
716
|
+
props = v;
|
|
717
|
+
}
|
|
718
|
+
const file = `${COMPONENTS_DIR}/${o.name}.tsx`;
|
|
719
|
+
await refuseLink(o.dir, COMPONENTS_DIR);
|
|
720
|
+
await refuseLink(o.dir, file);
|
|
721
|
+
if (!existsSync(join(o.dir, file)))
|
|
722
|
+
throw new ComponentRefused(`${file} is not here — write it first (sb site components init starts one).`);
|
|
723
|
+
const built = await buildComponent(o.dir, `${o.name}.tsx`);
|
|
724
|
+
const size = `${(Buffer.byteLength(built.ssr) / 1024).toFixed(1)} KiB SSR`;
|
|
725
|
+
const log = o.log ?? console.log;
|
|
726
|
+
if (o.dryRun) {
|
|
727
|
+
log(` · ${file} builds (${size}); dry run — nothing sent, nothing rendered.`);
|
|
728
|
+
return 0;
|
|
729
|
+
}
|
|
730
|
+
const ctx = await open(o);
|
|
731
|
+
if (!ctx.v1)
|
|
732
|
+
throw new ComponentRefused('Previewing needs an API key (wbk_… with pages.write) in SB_TOKEN or .env — a browser sign-in cannot.');
|
|
733
|
+
const res = await send(ctx, `${ctx.base}/code-components/preview`, 'POST', {
|
|
734
|
+
ssrBundle: built.ssr,
|
|
735
|
+
propsSchema: built.schema,
|
|
736
|
+
...(props ? { props } : {}),
|
|
737
|
+
});
|
|
738
|
+
if (res.ok) {
|
|
739
|
+
const html = (await res.json()).html ?? '';
|
|
740
|
+
ctx.log(o.json ? JSON.stringify({ html }) : html);
|
|
741
|
+
return 0;
|
|
742
|
+
}
|
|
743
|
+
const err = await errorOf(res);
|
|
744
|
+
const wait = res.headers.get('retry-after');
|
|
745
|
+
ctx.warn(res.status === 429 || res.status === 503
|
|
746
|
+
? ` ! ${file}: the platform is busy (${refusal(err)})${wait ? ` — try again in ${wait}s` : ''}. Not retried.`
|
|
747
|
+
: ` ! ${file}: ${refusal(err)}`);
|
|
748
|
+
return 1;
|
|
749
|
+
}
|
|
750
|
+
const kib = (n) => `${(n / 1024).toFixed(1)} KiB`;
|
|
751
|
+
/**
|
|
752
|
+
* `sb site components list [--json]`: each component on the platform with its versions, sizes and
|
|
753
|
+
* dates, plus the versions THIS repo's files pin. Which pages pin a version on the platform (drafts
|
|
754
|
+
* and live pages) is not something the API lists; `sb site components delete` names them on refusal.
|
|
755
|
+
*/
|
|
756
|
+
export async function listRemote(o) {
|
|
757
|
+
const ctx = await open(o);
|
|
758
|
+
const read = async (url, what) => {
|
|
759
|
+
const res = await send(ctx, url, 'GET');
|
|
760
|
+
if (!res.ok)
|
|
761
|
+
throw new ComponentRefused(`Reading ${what} failed: ${refusal(await errorOf(res))}`);
|
|
762
|
+
return (await res.json());
|
|
763
|
+
};
|
|
764
|
+
const latest = (await read(`${ctx.base}/code-components`, 'the code components')).codeComponents ?? [];
|
|
765
|
+
const pins = await pinsInRepo(ctx.dir);
|
|
766
|
+
const out = [];
|
|
767
|
+
for (const c of [...latest].sort((a, b) => a.name.localeCompare(b.name))) {
|
|
768
|
+
// A component holds at most 200 live versions, which is also the page ceiling: one request.
|
|
769
|
+
const all = await read(`${ctx.base}/code-components/${encodeURIComponent(c.name)}/versions?limit=200`, c.name);
|
|
770
|
+
const here = new Map();
|
|
771
|
+
for (const p of pins.filter((p) => p.name === c.name))
|
|
772
|
+
here.set(p.version, (here.get(p.version) ?? new Set()).add(p.file));
|
|
773
|
+
out.push({
|
|
774
|
+
name: c.name,
|
|
775
|
+
latest: c.version,
|
|
776
|
+
count: all.total ?? all.codeComponents?.length ?? 0,
|
|
777
|
+
versions: (all.codeComponents ?? []).map((v) => ({
|
|
778
|
+
version: v.version,
|
|
779
|
+
ssrSize: v.ssrSize,
|
|
780
|
+
clientSize: v.clientSize,
|
|
781
|
+
createdAt: v.createdAt,
|
|
782
|
+
})),
|
|
783
|
+
pinnedHere: [...here.keys()]
|
|
784
|
+
.sort((a, b) => a - b)
|
|
785
|
+
.map((version) => ({ version, files: [...here.get(version)].sort() })),
|
|
786
|
+
});
|
|
787
|
+
}
|
|
788
|
+
if (o.json) {
|
|
789
|
+
ctx.log(JSON.stringify({ codeComponents: out }, null, 2));
|
|
790
|
+
return 0;
|
|
791
|
+
}
|
|
792
|
+
if (!out.length) {
|
|
793
|
+
ctx.log('No code components on the platform yet. sb site components init, then sb site push.');
|
|
794
|
+
return 0;
|
|
795
|
+
}
|
|
796
|
+
for (const c of out) {
|
|
797
|
+
ctx.log(`${c.name} latest version ${c.latest}, ${c.count} version(s)`);
|
|
798
|
+
for (const v of c.versions) {
|
|
799
|
+
const pin = c.pinnedHere.find((p) => p.version === v.version);
|
|
800
|
+
ctx.log(` v${v.version} ${kib(v.ssrSize)} SSR, ${kib(v.clientSize)} client ${v.createdAt}` +
|
|
801
|
+
(pin ? ` pinned here: ${pin.files.join(', ')}` : ''));
|
|
802
|
+
}
|
|
803
|
+
for (const p of c.pinnedHere)
|
|
804
|
+
if (!c.versions.some((v) => v.version === p.version))
|
|
805
|
+
ctx.warn(` ! v${p.version} is pinned by ${p.files.join(', ')} and is not on the platform (sb site components upgrade ${c.name})`);
|
|
806
|
+
}
|
|
495
807
|
return 0;
|
|
496
808
|
}
|
|
809
|
+
/**
|
|
810
|
+
* `sb site components delete <Name> <version> [--yes]`: deletes one version on the platform. Asked
|
|
811
|
+
* first on a terminal; off one, `--yes` is required. Refused (409 version_pinned) while any draft or
|
|
812
|
+
* live page, shared section or overlay pins it — the pages the platform names are printed and the
|
|
813
|
+
* exit is non-zero. A deleted number is never reused: the next upload gets a new one.
|
|
814
|
+
*/
|
|
815
|
+
export async function deleteRemote(o) {
|
|
816
|
+
if (!NAME.test(o.name))
|
|
817
|
+
throw new ComponentRefused(`"${o.name}" is not a component name.`);
|
|
818
|
+
if (!Number.isInteger(o.version) || o.version < 1)
|
|
819
|
+
throw new ComponentRefused('Name the version to delete: sb site components delete Hero 3');
|
|
820
|
+
const ctx = await open(o);
|
|
821
|
+
if (!ctx.v1)
|
|
822
|
+
throw new ComponentRefused('Deleting a version needs an API key (wbk_… with pages.write) in SB_TOKEN or .env — a browser sign-in cannot.');
|
|
823
|
+
const what = `${o.name} version ${o.version}`;
|
|
824
|
+
const versionURL = `${ctx.base}/code-components/${encodeURIComponent(o.name)}/versions/${o.version}`;
|
|
825
|
+
for (const p of (await pinsInRepo(ctx.dir)).filter((p) => p.name === o.name && p.version === o.version))
|
|
826
|
+
ctx.warn(` ! ${p.file}: node ${p.node} pins ${what} — once it is deleted, pushing this file renders that node empty.`);
|
|
827
|
+
if (o.dryRun) {
|
|
828
|
+
// A read, so a missing version is said as missing; whether a page pins it only the real
|
|
829
|
+
// DELETE can tell (the platform checks drafts and live pages then).
|
|
830
|
+
const got = await send(ctx, versionURL, 'GET');
|
|
831
|
+
if (got.status === 404) {
|
|
832
|
+
ctx.warn(` ! the platform has no ${what}; nothing would be deleted.`);
|
|
833
|
+
return 1;
|
|
834
|
+
}
|
|
835
|
+
if (!got.ok) {
|
|
836
|
+
ctx.warn(` ! reading ${what} failed (${refusal(await errorOf(got))}) — nothing sent.`);
|
|
837
|
+
return 1;
|
|
838
|
+
}
|
|
839
|
+
ctx.log(` ✗ ${what} would be deleted (dry run) — nothing sent. Only the real delete can tell whether a page still pins it.`);
|
|
840
|
+
return 0;
|
|
841
|
+
}
|
|
842
|
+
if (!o.yes) {
|
|
843
|
+
if (!o.confirm) {
|
|
844
|
+
ctx.warn(` ! off a terminal, deleting ${what} needs --yes; nothing deleted.`);
|
|
845
|
+
return 1;
|
|
846
|
+
}
|
|
847
|
+
if (!(await o.confirm(`Delete ${what} from the platform? Its number is never reused. [y/N] `))) {
|
|
848
|
+
ctx.log('Nothing deleted.');
|
|
849
|
+
return 0;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
const res = await send(ctx, versionURL, 'DELETE');
|
|
853
|
+
if (res.ok) {
|
|
854
|
+
ctx.log(` ✗ ${what} deleted. Its number is never reused: the next upload gets a new one.`);
|
|
855
|
+
return 0;
|
|
856
|
+
}
|
|
857
|
+
const err = await errorOf(res);
|
|
858
|
+
if (res.status === 409 && err.code === 'version_pinned') {
|
|
859
|
+
const pages = err.pages ?? [];
|
|
860
|
+
ctx.warn(` ! ${what} is still used — not deleted. Move these off it first (sb site components upgrade ${o.name}, then sb site push):`);
|
|
861
|
+
// A shared section or overlay pin may come without a page id: no empty "()" for it.
|
|
862
|
+
for (const p of pages)
|
|
863
|
+
ctx.warn(` - ${p.name || p.pageId || '?'}${p.pageId ? ` (${p.pageId})` : ''}`);
|
|
864
|
+
if (!pages.length)
|
|
865
|
+
ctx.warn(` ${err.error}`);
|
|
866
|
+
return 1;
|
|
867
|
+
}
|
|
868
|
+
ctx.warn(res.status === 404
|
|
869
|
+
? ` ! the platform has no ${what}; nothing deleted (sb site components list shows what it has).`
|
|
870
|
+
: ` ! ${what} was not deleted (${refusal(err)})`);
|
|
871
|
+
return 1;
|
|
872
|
+
}
|
|
497
873
|
// ── doctor ────────────────────────────────────────────────────────────────────────────────────
|
|
498
874
|
const make = (id, title) => (status, detail, fix = null) => ({
|
|
499
875
|
id,
|