@sbuilder/cli 0.16.0 → 0.18.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 (43) hide show
  1. package/README.md +79 -5
  2. package/README.vi.md +77 -4
  3. package/dist/bin/sb.js +127 -20
  4. package/dist/bin/sb.js.map +1 -1
  5. package/dist/src/adopt.js +184 -0
  6. package/dist/src/adopt.js.map +1 -0
  7. package/dist/src/agentdocs.js +18 -0
  8. package/dist/src/agentdocs.js.map +1 -1
  9. package/dist/src/completion.js +80 -0
  10. package/dist/src/completion.js.map +1 -0
  11. package/dist/src/components.js +9 -1
  12. package/dist/src/components.js.map +1 -1
  13. package/dist/src/envtypes.gen.js +9 -5
  14. package/dist/src/envtypes.gen.js.map +1 -1
  15. package/dist/src/login.js +52 -44
  16. package/dist/src/login.js.map +1 -1
  17. package/dist/src/nextshims.js +180 -0
  18. package/dist/src/nextshims.js.map +1 -0
  19. package/dist/src/preview.js +12 -3
  20. package/dist/src/preview.js.map +1 -1
  21. package/dist/src/route.js +2 -0
  22. package/dist/src/route.js.map +1 -1
  23. package/dist/src/site.js +6 -0
  24. package/dist/src/site.js.map +1 -1
  25. package/dist/src/sitedev.js +11 -2
  26. package/dist/src/sitedev.js.map +1 -1
  27. package/dist/src/sitefunctions.js +206 -0
  28. package/dist/src/sitefunctions.js.map +1 -0
  29. package/dist/src/sitekinds.js +149 -51
  30. package/dist/src/sitekinds.js.map +1 -1
  31. package/dist/src/sitemedia.js +115 -0
  32. package/dist/src/sitemedia.js.map +1 -0
  33. package/dist/src/sitepaths.js +5 -0
  34. package/dist/src/sitepaths.js.map +1 -1
  35. package/dist/src/sitestatus.js +15 -0
  36. package/dist/src/sitestatus.js.map +1 -1
  37. package/dist/src/templates.js +5 -1
  38. package/dist/src/templates.js.map +1 -1
  39. package/dist/src/ui.js +35 -0
  40. package/dist/src/ui.js.map +1 -1
  41. package/dist/src/update.js +79 -0
  42. package/dist/src/update.js.map +1 -0
  43. package/package.json +1 -1
@@ -0,0 +1,206 @@
1
+ import { readdir } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { CHANGED_WHILE_PULLING, errorOf, putIfStill, readText, refusal, saveManifest, send, sha256, } from './site.js';
4
+ import { listFailed, none, unsafe } from './sitekinds.js';
5
+ import { SAFE_KIND_FILE } from './sitepaths.js';
6
+ const DIR = 'functions';
7
+ const ext = (l) => (l === 'javascript' ? 'js' : 'ts');
8
+ const fileOf = (f) => `${DIR}/${f.slug}.${ext(f.language)}`;
9
+ /** functions/<slug>.<ts|js> → its slug and language, or undefined for anything else. */
10
+ function parseName(file) {
11
+ const m = SAFE_KIND_FILE.functions.exec(file);
12
+ return m ? { slug: m[1], language: m[2] === 'js' ? 'javascript' : 'typescript' } : undefined;
13
+ }
14
+ async function usesFunctions(ctx) {
15
+ return Object.keys(ctx.manifest.functions ?? {}).length > 0 || (await localFiles(ctx)).length > 0;
16
+ }
17
+ async function listFns(ctx) {
18
+ const res = await send(ctx, `${ctx.base}/functions`, 'GET');
19
+ return res.ok ? (await res.json()).functions : res;
20
+ }
21
+ const compileNote = (file, f) => f.compileError
22
+ ? ` ! ${file}: the platform could not compile it — ${f.compileError.split('\n')[0]} (the last good build keeps serving)`
23
+ : '';
24
+ export async function pullFunctions(ctx) {
25
+ const out = none();
26
+ const fns = await listFns(ctx);
27
+ if (!Array.isArray(fns)) {
28
+ // A key without the scope, or a platform without the route: said out loud — but only to a repo
29
+ // that uses functions. Everyone else would read a warning about something they never asked for.
30
+ if (await usesFunctions(ctx))
31
+ await listFailed(ctx, 'backend functions', fns, 'functions.read');
32
+ return out;
33
+ }
34
+ // Not created for a site with no functions: a no-op pull must not add a key to .sb-site.json.
35
+ const have = ctx.manifest.functions ?? {};
36
+ const seen = new Set();
37
+ for (const f of fns) {
38
+ seen.add(f.slug);
39
+ const entry = have[f.slug];
40
+ const file = entry?.file ?? fileOf(f);
41
+ const note = compileNote(file, f);
42
+ if (entry && entry.updatedAt === f.updatedAt) {
43
+ entry.enabled = f.enabled;
44
+ continue;
45
+ }
46
+ const why = await unsafe(ctx, SAFE_KIND_FILE.functions, file);
47
+ if (why) {
48
+ ctx.warn(` ! ${file}: ${why} — not followed, nothing written.`);
49
+ out.clashes++;
50
+ continue;
51
+ }
52
+ const local = await readText(join(ctx.dir, file));
53
+ if (entry && local !== undefined && sha256(local) !== entry.sha256 && !ctx.o.force) {
54
+ ctx.warn(` ! ${file}: changed here AND in the editor — kept yours. sb site pull --force takes the editor's.`);
55
+ out.clashes++;
56
+ continue;
57
+ }
58
+ if (!entry && local !== undefined && local !== f.content && !ctx.o.force) {
59
+ ctx.warn(` ! ${file}: already exists and is not tracked — kept it. Move it aside, or --force.`);
60
+ out.clashes++;
61
+ continue;
62
+ }
63
+ if (!ctx.o.dryRun) {
64
+ if (!(await putIfStill(join(ctx.dir, file), local, f.content))) {
65
+ ctx.warn(CHANGED_WHILE_PULLING(file));
66
+ out.clashes++;
67
+ continue;
68
+ }
69
+ (ctx.manifest.functions ??= have)[f.slug] = {
70
+ file,
71
+ id: f.id,
72
+ updatedAt: f.updatedAt,
73
+ sha256: sha256(f.content),
74
+ enabled: f.enabled,
75
+ };
76
+ await saveManifest(ctx);
77
+ }
78
+ ctx.log(` ↓ ${file}${f.enabled ? '' : ' (disabled on the platform)'}`);
79
+ if (note)
80
+ ctx.warn(note);
81
+ out.wrote++;
82
+ }
83
+ for (const [slug, entry] of Object.entries(have)) {
84
+ if (seen.has(slug))
85
+ continue;
86
+ ctx.warn(` ! ${entry.file}: deleted in the editor — your file is kept. Delete it, or push --functions to create it again.`);
87
+ if (!ctx.o.dryRun) {
88
+ delete have[slug];
89
+ await saveManifest(ctx);
90
+ }
91
+ }
92
+ return out;
93
+ }
94
+ export const FUNCTIONS_WITHHELD = (files) => ` ! ${files.join(', ')}: changed, but a function save is LIVE at /_wb/fn/… at once — nothing sent. ` +
95
+ 'Run sb site push --functions to send ' +
96
+ (files.length === 1 ? 'it.' : 'them.');
97
+ async function localFiles(ctx) {
98
+ const names = await readdir(join(ctx.dir, DIR)).catch(() => []);
99
+ return names.map((n) => `${DIR}/${n}`).filter((f) => parseName(f));
100
+ }
101
+ export async function pushFunctions(ctx) {
102
+ const out = none();
103
+ const o = ctx.o;
104
+ const have = ctx.manifest.functions ?? {};
105
+ const files = new Set(await localFiles(ctx));
106
+ for (const e of Object.values(have))
107
+ files.add(e.file);
108
+ const changed = [];
109
+ for (const file of [...files].sort()) {
110
+ if (o.only && !o.only.has(file))
111
+ continue;
112
+ const name = parseName(file);
113
+ const entry = name && have[name.slug];
114
+ const text = await readText(join(ctx.dir, file));
115
+ if (text === undefined) {
116
+ if (entry)
117
+ ctx.warn(` ! ${file}: missing — the function keeps serving (a function is never deleted from here; delete it in the editor).`);
118
+ continue;
119
+ }
120
+ if (!name)
121
+ continue;
122
+ if (entry && entry.file === file && sha256(text) === entry.sha256)
123
+ continue;
124
+ const why = await unsafe(ctx, SAFE_KIND_FILE.functions, file);
125
+ if (why) {
126
+ ctx.warn(` ! ${file}: ${why} — not read, nothing sent.`);
127
+ out.failed++;
128
+ continue;
129
+ }
130
+ changed.push({ file, text, ...name });
131
+ }
132
+ if (!changed.length)
133
+ return out;
134
+ if (!o.functions) {
135
+ ctx.warn(FUNCTIONS_WITHHELD(changed.map((c) => c.file)));
136
+ return out; // withheld on purpose, said out loud: not a failure
137
+ }
138
+ if (o.dryRun) {
139
+ for (const c of changed)
140
+ ctx.log(` ↑ ${c.file} (dry run — live at /_wb/fn/${c.slug} once sent)`);
141
+ out.sent += changed.length;
142
+ return out;
143
+ }
144
+ const platform = await listFns(ctx);
145
+ if (!Array.isArray(platform)) {
146
+ ctx.warn(` ! functions: reading them failed (${refusal(await errorOf(platform))}) — nothing sent.`);
147
+ out.failed += changed.length;
148
+ return out;
149
+ }
150
+ const bySlug = new Map(platform.map((f) => [f.slug, f]));
151
+ const store = (ctx.manifest.functions ??= {});
152
+ for (const c of changed) {
153
+ const entry = store[c.slug];
154
+ const there = bySlug.get(c.slug);
155
+ if (!entry && there && !o.force) {
156
+ ctx.warn(` ! ${c.file}: the platform already has /_wb/fn/${c.slug}, which this repo never pulled — sb site pull, or --force. Nothing sent.`);
157
+ out.failed++;
158
+ o.report?.(c.file, 'refused');
159
+ continue;
160
+ }
161
+ const body = (expected) => ({
162
+ slug: c.slug,
163
+ language: c.language,
164
+ content: c.text,
165
+ enabled: there?.enabled ?? entry?.enabled ?? true,
166
+ ...(expected ? { expectedUpdatedAt: expected } : {}),
167
+ });
168
+ let res = there
169
+ ? await send(ctx, `${ctx.base}/functions/${there.id}`, 'PUT', body(entry?.updatedAt ?? there.updatedAt))
170
+ : await send(ctx, `${ctx.base}/functions`, 'POST', body());
171
+ if (res.status === 409 && o.force && there)
172
+ res = await send(ctx, `${ctx.base}/functions/${there.id}`, 'PUT', body(there.updatedAt));
173
+ if (!res.ok) {
174
+ const err = await errorOf(res);
175
+ const stale = res.status === 409;
176
+ ctx.warn(stale
177
+ ? ` ! ${c.file}: edited in the editor since your last pull — nothing sent. Pull and merge, or push --functions --force to overwrite it.`
178
+ : ` ! ${c.file}: refused (${refusal(err)})`);
179
+ out.failed++;
180
+ o.report?.(c.file, stale ? 'stale' : 'refused');
181
+ continue;
182
+ }
183
+ const saved = (await res.json()).function;
184
+ store[c.slug] = {
185
+ file: c.file,
186
+ id: saved.id,
187
+ updatedAt: saved.updatedAt,
188
+ sha256: sha256(c.text),
189
+ enabled: saved.enabled,
190
+ };
191
+ await saveManifest(ctx);
192
+ const note = compileNote(c.file, saved);
193
+ if (note) {
194
+ // Saved — but what serves is still the last good build: a failure, so the exit says so.
195
+ ctx.warn(note);
196
+ out.failed++;
197
+ o.report?.(c.file, 'refused');
198
+ continue;
199
+ }
200
+ ctx.log(` ↑ ${c.file} (live at /_wb/fn/${c.slug}${saved.enabled ? '' : ' once enabled in the editor'})`);
201
+ out.sent++;
202
+ o.report?.(c.file, 'sent');
203
+ }
204
+ return out;
205
+ }
206
+ //# sourceMappingURL=s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C,CAAC,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;YAC9B,SAAS;QACX,CAAC;QACD,GAAG,CAAC,GAAG,CACL,OAAO,CAAC,CAAC,IAAI,qBAAqB,CAAC,CAAC,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,6BAA6B,GAAG,CACjG,CAAC;QACF,GAAG,CAAC,IAAI,EAAE,CAAC;QACX,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IAC7B,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
@@ -10,6 +10,8 @@ import { SAFE_KIND_FILE } from './sitepaths.js';
10
10
  import { DEFAULT_THEME } from './envtypes.gen.js';
11
11
  import { themeDiff } from './themediff.js';
12
12
  import { refuseStructure } from './structure.js';
13
+ import { pullFunctions, pushFunctions } from './sitefunctions.js';
14
+ import { pushMedia } from './sitemedia.js';
13
15
  /**
14
16
  * The rest of a site as source: shared sections (design/globals), overlays
15
17
  * (design/overlays), the theme (design/theme.json) and translations
@@ -29,7 +31,7 @@ import { refuseStructure } from './structure.js';
29
31
  * These read /api/v1 with the repo's own key (a session login does not reach them).
30
32
  */
31
33
  const PAGE = 200;
32
- const none = () => ({ clashes: 0, wrote: 0, sent: 0, failed: 0 });
34
+ export const none = () => ({ clashes: 0, wrote: 0, sent: 0, failed: 0 });
33
35
  // ─── shared helpers ─────────────────────────────────────────────────────────────
34
36
  /** `a-name.page.tsx`-style stem from a display name, unique among `taken`. */
35
37
  function fileFor(dir, ext, name, id, taken) {
@@ -46,14 +48,14 @@ function fileFor(dir, ext, name, id, taken) {
46
48
  return file;
47
49
  }
48
50
  /** A file that may be read: a safe name and no symlink on its path. Says why not, or undefined. */
49
- async function unsafe(ctx, re, file) {
51
+ export async function unsafe(ctx, re, file) {
50
52
  if (!re.test(file))
51
53
  return `${file} is not a plain file name`;
52
54
  const link = await linkOn(ctx.dir, file);
53
55
  return link ? `${link} is a symbolic link` : undefined;
54
56
  }
55
57
  /** What a failed list means: skip the kind with a line, never a crash and never silently. */
56
- async function listFailed(ctx, what, res, scope) {
58
+ export async function listFailed(ctx, what, res, scope) {
57
59
  const why = res.status === 403
58
60
  ? `this key cannot read ${what} (it needs ${scope})`
59
61
  : res.status === 404 || res.status === 501
@@ -505,29 +507,102 @@ async function pruneDocKind(ctx, k, gate, missing) {
505
507
  }
506
508
  return ok;
507
509
  }
508
- // ─── the theme ──────────────────────────────────────────────────────────────────
509
- const THEME_FILE = 'design/theme.json';
510
- async function readTheme(ctx) {
511
- const res = await send(ctx, `${ctx.base}/theme`, 'GET');
512
- return res.ok
513
- ? (await res.json()).theme
514
- : res;
510
+ const short = (v) => {
511
+ const t = JSON.stringify(v) ?? 'nothing';
512
+ return t.length > 60 ? t.slice(0, 57) + '…' : t;
513
+ };
514
+ /** What changed in a settings document, key by key (a theme has its own token-level diff). */
515
+ export function settingsDiff(before, after) {
516
+ const b = (before ?? {});
517
+ const a = (after ?? {});
518
+ const out = [];
519
+ for (const k of [...new Set([...Object.keys(b), ...Object.keys(a)])].sort())
520
+ if (!isDeepStrictEqual(b[k], a[k]))
521
+ out.push(!(k in b)
522
+ ? `+ ${k}: ${short(a[k])}`
523
+ : !(k in a)
524
+ ? `- ${k}`
525
+ : `~ ${k}: ${short(b[k])} → ${short(a[k])}`);
526
+ return out;
515
527
  }
516
- async function pullTheme(ctx) {
528
+ const THEME = {
529
+ file: 'design/theme.json',
530
+ re: SAFE_KIND_FILE.theme,
531
+ route: 'theme',
532
+ key: 'theme',
533
+ manifestKey: 'theme',
534
+ flag: 'theme',
535
+ readScope: 'theme.read',
536
+ fallback: DEFAULT_THEME,
537
+ t: {
538
+ the: 'the theme',
539
+ live: 'a theme save is LIVE for every visitor at once',
540
+ has: 'a theme',
541
+ liveNow: 'the theme is live now',
542
+ kept: 'the theme is kept on the platform (a theme is never deleted from here)',
543
+ defaultNote: 'the default theme: this site has not saved one yet',
544
+ neverSaved: 'the theme was never saved',
545
+ },
546
+ diff: (b, a) => themeDiff(b ?? DEFAULT_THEME, a),
547
+ };
548
+ const SETTINGS = {
549
+ file: 'design/settings.json',
550
+ re: SAFE_KIND_FILE.settings,
551
+ route: 'site-settings',
552
+ key: 'siteSettings',
553
+ manifestKey: 'settings',
554
+ flag: 'settings',
555
+ readScope: 'member.read',
556
+ fallback: {},
557
+ optional: true,
558
+ t: {
559
+ the: 'the settings',
560
+ live: 'a settings save is LIVE for every visitor at once (currency, markets, store details)',
561
+ has: 'settings',
562
+ liveNow: 'the settings are live now',
563
+ kept: 'the settings are kept on the platform (settings are never deleted from here)',
564
+ defaultNote: 'empty: this site has not saved settings yet',
565
+ neverSaved: 'the settings were never saved',
566
+ noWrite: "this repo's key may not save settings: it needs site.update, which a key made by sb site init leaves out on purpose (it also changes domains, payment and tax). Change them in the editor, or put a key that holds site.update (Settings → API keys) in SB_TOKEN for this one push",
567
+ },
568
+ diff: settingsDiff,
569
+ };
570
+ async function readDoc(ctx, d) {
571
+ const res = await send(ctx, `${ctx.base}/${d.route}`, 'GET');
572
+ if (!res.ok)
573
+ return res;
574
+ const got = (await res.json())[d.key];
575
+ // A 200 without the key is a platform that does not serve this kind: said as such, never a crash.
576
+ return got ?? new Response(JSON.stringify({ error: 'not served here' }), { status: 404 });
577
+ }
578
+ async function pullDoc(ctx, d) {
517
579
  const out = none();
518
- const t = await readTheme(ctx);
580
+ const t = await readDoc(ctx, d);
519
581
  if ('status' in t) {
520
- await listFailed(ctx, 'the theme', t, 'theme.read');
582
+ const unused = d.optional &&
583
+ (t.status === 404 || t.status === 501) &&
584
+ !ctx.manifest[d.manifestKey] &&
585
+ (await readText(join(ctx.dir, d.file))) === undefined;
586
+ if (!unused)
587
+ await listFailed(ctx, d.t.the, t, d.readScope);
521
588
  return out;
522
589
  }
523
- // Nothing saved yet: the site renders with the schema's default theme, so that is the file — an
524
- // agent (or anyone) then sees the shape and the token ids. Unchanged, push never sends it.
525
- const document = t.document ?? DEFAULT_THEME;
526
- const entry = ctx.manifest.theme;
590
+ // Nothing saved yet: the fallback is the file — an agent (or anyone) then sees the shape and the
591
+ // ids. Unchanged, push never sends it.
592
+ const document = t.document ?? d.fallback;
593
+ const entry = ctx.manifest[d.manifestKey];
527
594
  if (entry && entry.updatedAt === t.updatedAt)
528
595
  return out;
529
- const file = entry?.file ?? THEME_FILE;
530
- const why = await unsafe(ctx, SAFE_KIND_FILE.theme, file);
596
+ // "Never saved" for a document this repo synced a saved version of cannot be true (neither can be
597
+ // deleted): it is a platform that failed to read it. Writing the fallback over the file would
598
+ // replace real settings with an empty object, quietly — keep the file and say so instead.
599
+ if (t.document === null && entry && entry.updatedAt !== '') {
600
+ ctx.warn(` ! ${entry.file}: the platform answered that ${d.t.neverSaved}, but this repo synced it — kept your file, nothing written. Pull again later.`);
601
+ out.clashes++;
602
+ return out;
603
+ }
604
+ const file = entry?.file ?? d.file;
605
+ const why = await unsafe(ctx, d.re, file);
531
606
  if (why) {
532
607
  ctx.warn(` ! ${file}: ${why} — not followed, nothing written.`);
533
608
  out.clashes++;
@@ -535,7 +610,7 @@ async function pullTheme(ctx) {
535
610
  }
536
611
  const text = printJson(document);
537
612
  const local = await readText(join(ctx.dir, file));
538
- // No saved theme and a theme.json of the owner's own: theirs to push with --theme, not a clash.
613
+ // Nothing saved and a file of the owner's own: theirs to push with the flag, not a clash.
539
614
  if (t.document === null && !entry && local !== undefined)
540
615
  return out;
541
616
  if (entry && local !== undefined && sha256(local) !== entry.sha256 && !ctx.o.force) {
@@ -554,22 +629,21 @@ async function pullTheme(ctx) {
554
629
  out.clashes++;
555
630
  return out;
556
631
  }
557
- ctx.manifest.theme = { file, updatedAt: t.updatedAt, sha256: sha256(text) };
632
+ ctx.manifest[d.manifestKey] = { file, updatedAt: t.updatedAt, sha256: sha256(text) };
558
633
  await saveManifest(ctx);
559
634
  }
560
- ctx.log(t.document === null
561
- ? ` ↓ ${file} (the default theme: this site has not saved one yet)`
562
- : ` ↓ ${file}`);
635
+ ctx.log(t.document === null ? ` ↓ ${file} (${d.t.defaultNote})` : ` ↓ ${file}`);
563
636
  out.wrote++;
564
637
  return out;
565
638
  }
566
- export const THEME_WITHHELD = (file) => ` ! ${file}: changed, but a theme save is LIVE for every visitor at once — nothing sent. ` +
567
- 'Run sb site push --theme to send it.';
568
- async function pushTheme(ctx) {
639
+ export const THEME_WITHHELD = (file) => withheld(THEME, file);
640
+ const withheld = (d, file) => ` ! ${file}: changed, but ${d.t.live} — nothing sent. ` +
641
+ `Run sb site push --${d.flag} to send it.`;
642
+ async function pushDoc(ctx, d) {
569
643
  const out = none();
570
644
  const o = ctx.o;
571
- const entry = ctx.manifest.theme;
572
- const file = entry?.file ?? THEME_FILE;
645
+ const entry = ctx.manifest[d.manifestKey];
646
+ const file = entry?.file ?? d.file;
573
647
  if (o.only && !o.only.has(file))
574
648
  return out;
575
649
  const text = await readText(join(ctx.dir, file));
@@ -577,14 +651,14 @@ async function pushTheme(ctx) {
577
651
  if (entry && o.only)
578
652
  ctx.warn(` ! ${file}: missing — not deleted (sb site dev never prunes).`);
579
653
  else if (entry) {
580
- ctx.warn(` ! ${file}: missing — the theme is kept on the platform (a theme is never deleted from here).`);
654
+ ctx.warn(` ! ${file}: missing — ${d.t.kept}.`);
581
655
  out.failed++;
582
656
  }
583
657
  return out;
584
658
  }
585
659
  if (entry && sha256(text) === entry.sha256)
586
660
  return out;
587
- const why = await unsafe(ctx, SAFE_KIND_FILE.theme, file);
661
+ const why = await unsafe(ctx, d.re, file);
588
662
  if (why) {
589
663
  ctx.warn(` ! ${file}: ${why} — not read, nothing sent.`);
590
664
  out.failed++;
@@ -594,7 +668,7 @@ async function pushTheme(ctx) {
594
668
  try {
595
669
  doc = parseJson(text);
596
670
  if (typeof doc !== 'object' || doc === null || Array.isArray(doc))
597
- throw new Error('the theme must be a JSON object');
671
+ throw new Error(`${d.t.the} must be a JSON object`);
598
672
  }
599
673
  catch (err) {
600
674
  ctx.warn(` ! ${file}: ${messageOf(err)} — nothing sent.`);
@@ -602,32 +676,32 @@ async function pushTheme(ctx) {
602
676
  o.report?.(file, 'refused');
603
677
  return out;
604
678
  }
605
- if (!o.theme) {
606
- ctx.warn(THEME_WITHHELD(file));
679
+ if (!o[d.flag]) {
680
+ ctx.warn(withheld(d, file));
607
681
  return out; // withheld on purpose, said out loud: not a failure
608
682
  }
609
- const now = await readTheme(ctx);
683
+ const now = await readDoc(ctx, d);
610
684
  if (o.dryRun) {
611
- // What would go LIVE, token by token, against the theme the platform holds now.
612
- const changes = 'status' in now ? [] : themeDiff(now.document ?? DEFAULT_THEME, doc);
613
- ctx.log(` ↑ ${file} (dry run — a theme save is LIVE at once${'status' in now ? '; the platform theme could not be read to compare' : changes.length ? '' : '; it matches the platform theme'})`);
685
+ // What would go LIVE, against what the platform holds now.
686
+ const changes = 'status' in now ? [] : d.diff(now.document ?? d.fallback, doc);
687
+ ctx.log(` ↑ ${file} (dry run — ${d.t.live.split(' for every')[0]} at once${'status' in now ? `; ${d.t.the} on the platform could not be read to compare` : changes.length ? '' : `; it matches ${d.t.the} on the platform`})`);
614
688
  for (const c of changes)
615
689
  ctx.log(` ${c}`);
616
690
  out.sent++;
617
691
  return out;
618
692
  }
619
693
  if ('status' in now) {
620
- ctx.warn(` ! ${file}: reading the theme failed (${refusal(await errorOf(now))}) — nothing sent.`);
694
+ ctx.warn(` ! ${file}: reading ${d.t.the} failed (${refusal(await errorOf(now))}) — nothing sent.`);
621
695
  out.failed++;
622
696
  return out;
623
697
  }
624
- // No record of a sync, but the platform has a theme: sending would replace it unseen.
698
+ // No record of a sync, but the platform has one: sending would replace it unseen.
625
699
  if (!entry && now.updatedAt !== '' && !o.force) {
626
- ctx.warn(` ! ${file}: the platform already has a theme this repo never pulled — sb site pull, or --force. Nothing sent.`);
700
+ ctx.warn(` ! ${file}: the platform already has ${d.t.has} this repo never pulled — sb site pull, or --force. Nothing sent.`);
627
701
  out.failed++;
628
702
  return out;
629
703
  }
630
- const send1 = (expected) => send(ctx, `${ctx.base}/theme`, 'PUT', { document: doc, expectedUpdatedAt: expected });
704
+ const send1 = (expected) => send(ctx, `${ctx.base}/${d.route}`, 'PUT', { document: doc, expectedUpdatedAt: expected });
631
705
  let res = await send1(entry?.updatedAt ?? '');
632
706
  if (res.status === 409 && o.force) {
633
707
  const err = await errorOf(res);
@@ -638,24 +712,34 @@ async function pushTheme(ctx) {
638
712
  const err = await errorOf(res);
639
713
  const stale = err.code === 'source_stale';
640
714
  ctx.warn(stale
641
- ? ` ! ${file}: edited in the editor since your last pull — nothing sent. Pull and merge, or sb site push --theme --force to overwrite it.`
642
- : ` ! ${file}: refused (${refusal(err)})`);
715
+ ? ` ! ${file}: edited in the editor since your last pull — nothing sent. Pull and merge, or sb site push --${d.flag} --force to overwrite it.`
716
+ : err.code === 'insufficient_scope' && d.t.noWrite
717
+ ? ` ! ${file}: ${d.t.noWrite} — nothing sent.`
718
+ : ` ! ${file}: refused (${refusal(err)})`);
643
719
  out.failed++;
644
720
  o.report?.(file, stale ? 'stale' : 'refused');
645
721
  return out;
646
722
  }
647
- const saved = (await res.json()).theme;
723
+ const saved = (await res.json())[d.key];
648
724
  const back = printJson(saved.document ?? doc);
649
725
  const wrote = await writeBack(ctx, file, text, back);
650
- ctx.manifest.theme = { file, updatedAt: saved.updatedAt, sha256: sha256(wrote ? back : text) };
726
+ ctx.manifest[d.manifestKey] = {
727
+ file,
728
+ updatedAt: saved.updatedAt,
729
+ sha256: sha256(wrote ? back : text),
730
+ };
651
731
  await saveManifest(ctx);
652
- ctx.log(` ↑ ${file} (the theme is live now)`);
653
- for (const c of themeDiff(now.document ?? DEFAULT_THEME, doc))
732
+ ctx.log(` ↑ ${file} (${d.t.liveNow})`);
733
+ for (const c of d.diff(now.document ?? d.fallback, doc))
654
734
  ctx.log(` ${c}`);
655
735
  out.sent++;
656
736
  o.report?.(file, 'sent');
657
737
  return out;
658
738
  }
739
+ const pullTheme = (ctx) => pullDoc(ctx, THEME);
740
+ const pushTheme = (ctx) => pushDoc(ctx, THEME);
741
+ const pullSettings = (ctx) => pullDoc(ctx, SETTINGS);
742
+ const pushSettings = (ctx) => pushDoc(ctx, SETTINGS);
659
743
  // ─── translations ───────────────────────────────────────────────────────────────
660
744
  const SOURCES = ['human', 'machine'];
661
745
  const HASH = /^[0-9a-f]{8}$/;
@@ -1043,14 +1127,22 @@ async function pushTranslations(ctx, gate) {
1043
1127
  /** Is any of these kinds on disk or in the manifest? A session login skips them — out loud. */
1044
1128
  async function present(ctx) {
1045
1129
  const m = ctx.manifest;
1046
- if (m.globals || m.overlays || m.theme || m.translations)
1130
+ if (m.globals || m.overlays || m.theme || m.settings || m.translations || m.functions || m.media)
1047
1131
  return true;
1048
- for (const p of ['design/globals', 'design/overlays', 'design/theme.json', 'design/translations'])
1132
+ for (const p of [
1133
+ 'design/globals',
1134
+ 'design/overlays',
1135
+ 'design/theme.json',
1136
+ 'design/settings.json',
1137
+ 'design/translations',
1138
+ 'functions',
1139
+ 'media',
1140
+ ])
1049
1141
  if (await stat(join(ctx.dir, p)).then(() => true, () => false))
1050
1142
  return true;
1051
1143
  return false;
1052
1144
  }
1053
- const NEEDS_KEY = ' · globals, overlays, the theme and translations sync only with an API key (a repo made by sb site init has one in .env) — skipped.';
1145
+ const NEEDS_KEY = ' · globals, overlays, the theme, settings, translations, functions and media sync only with an API key (a repo made by sb site init has one in .env) — skipped.';
1054
1146
  export async function pullKinds(ctx) {
1055
1147
  if (!ctx.v1) {
1056
1148
  if (await present(ctx))
@@ -1062,7 +1154,9 @@ export async function pullKinds(ctx) {
1062
1154
  await pullDocKind(ctx, KINDS.globals),
1063
1155
  await pullDocKind(ctx, KINDS.overlays),
1064
1156
  await pullTheme(ctx),
1157
+ await pullSettings(ctx),
1065
1158
  await pullTranslations(ctx),
1159
+ await pullFunctions(ctx),
1066
1160
  ]) {
1067
1161
  all.clashes += r.clashes;
1068
1162
  all.wrote += r.wrote;
@@ -1078,10 +1172,14 @@ export async function pushKinds(ctx, gate) {
1078
1172
  const all = none();
1079
1173
  const prune = (k) => (missing) => pruneDocKind(ctx, k, gate, missing);
1080
1174
  for (const r of [
1175
+ // First: an upload changes no page, and a page pushed in the same run may already use its URL.
1176
+ await pushMedia(ctx),
1081
1177
  await pushDocKind(ctx, KINDS.globals, prune(KINDS.globals)),
1082
1178
  await pushDocKind(ctx, KINDS.overlays, prune(KINDS.overlays)),
1083
1179
  await pushTheme(ctx),
1180
+ await pushSettings(ctx),
1084
1181
  await pushTranslations(ctx, gate),
1182
+ await pushFunctions(ctx),
1085
1183
  ]) {
1086
1184
  all.sent += r.sent;
1087
1185
  all.failed += r.failed;