@sbuilder/cli 0.9.1 → 0.11.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 +247 -6
- package/README.vi.md +241 -7
- package/dist/bin/sb.js +141 -6
- package/dist/bin/sb.js.map +1 -1
- package/dist/src/agentdocs.js +12 -3
- package/dist/src/agentdocs.js.map +1 -1
- package/dist/src/doctor.js +516 -0
- package/dist/src/doctor.js.map +1 -0
- package/dist/src/envtypes.gen.js +1748 -0
- package/dist/src/envtypes.gen.js.map +1 -0
- package/dist/src/host.js +414 -22
- package/dist/src/host.js.map +1 -1
- package/dist/src/hostlink.js +161 -0
- package/dist/src/hostlink.js.map +1 -0
- package/dist/src/login.js +1 -1
- package/dist/src/login.js.map +1 -1
- package/dist/src/next.js +82 -6
- package/dist/src/next.js.map +1 -1
- package/dist/src/repo.js +24 -2
- package/dist/src/repo.js.map +1 -1
- package/dist/src/roottsconfig.js +154 -0
- package/dist/src/roottsconfig.js.map +1 -0
- package/dist/src/route.js +16 -1
- package/dist/src/route.js.map +1 -1
- package/dist/src/site.js +615 -71
- package/dist/src/site.js.map +1 -1
- package/dist/src/sitedev.js +99 -0
- package/dist/src/sitedev.js.map +1 -0
- package/dist/src/siteinit.js +76 -0
- package/dist/src/siteinit.js.map +1 -1
- package/dist/src/sitekinds.js +1048 -0
- package/dist/src/sitekinds.js.map +1 -0
- package/dist/src/sitepaths.js +12 -0
- package/dist/src/sitepaths.js.map +1 -0
- package/dist/src/sitestatus.js +104 -0
- package/dist/src/sitestatus.js.map +1 -0
- package/dist/src/source/meta.js +118 -0
- package/dist/src/source/meta.js.map +1 -0
- package/dist/src/source/parse.js +21 -2
- package/dist/src/source/parse.js.map +1 -1
- package/dist/src/starter.js +61 -0
- package/dist/src/starter.js.map +1 -0
- package/dist/src/vercel.js +379 -0
- package/dist/src/vercel.js.map +1 -0
- package/package.json +9 -1
- package/dist/src/export.js +0 -167
- package/dist/src/export.js.map +0 -1
package/dist/src/site.js
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
-
import { lstat, mkdir, readdir, readFile, rename, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { lstat, mkdir, readdir, readFile, rename, stat, writeFile } from 'node:fs/promises';
|
|
3
3
|
import { dirname, join } from 'node:path';
|
|
4
4
|
import { authedFetch, SB_TOKEN_REFUSED } from './auth.js';
|
|
5
5
|
import { sbJsonSite } from './code.js';
|
|
6
6
|
import { APP_BLOCK_EDITED, appBlockSeals, editedAppBlocks } from './appblocks.js';
|
|
7
7
|
import { DevError, messageOf } from './dev.js';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
8
|
+
import { parseFile, printFile } from './source/meta.js';
|
|
9
|
+
import { pullKinds, pushKinds } from './sitekinds.js';
|
|
10
|
+
import { staleTranslations } from './sitestatus.js';
|
|
11
|
+
import { SAFE_FILE, SAFE_KIND_FILE } from './sitepaths.js';
|
|
12
|
+
export { SAFE_FILE, SAFE_KIND_FILE };
|
|
13
|
+
import { SourceError } from './source/parse.js';
|
|
14
|
+
import { tagFor } from './source/print.js';
|
|
15
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
10
16
|
/**
|
|
11
17
|
* `sb site pull` / `sb site push` — a site's page DRAFTS as `.page.tsx` files in a repo.
|
|
12
18
|
*
|
|
@@ -22,9 +28,15 @@ import { literal, printPage, tagFor } from './source/print.js';
|
|
|
22
28
|
* a page someone edited in the editor since. Neither side is ever overwritten
|
|
23
29
|
* silently — a clash is named, and `--force` is the deliberate way past it.
|
|
24
30
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
31
|
+
* `export const page = { name, slug, type, isHomepage, settings }` in a page file is
|
|
32
|
+
* AUTHORITATIVE: a file with one and no manifest entry creates the page, and a changed
|
|
33
|
+
* field renames or re-types it. Deletion never happens by itself: a tracked file that is
|
|
34
|
+
* gone is reported, and only `--prune` (plus `--yes` off a terminal) deletes the page.
|
|
35
|
+
* A page deleted on the platform keeps its file. Deleting something a merchant still
|
|
36
|
+
* depends on is a decision, not a sync side effect.
|
|
37
|
+
*
|
|
38
|
+
* After a save the file is rewritten to what the platform stored (ids it minted, the
|
|
39
|
+
* facts it settled on), so the next pull has nothing to change.
|
|
28
40
|
*/
|
|
29
41
|
export const SITE_MANIFEST = '.sb-site.json';
|
|
30
42
|
export const PAGES_DIR = 'design/pages';
|
|
@@ -33,20 +45,32 @@ export class SiteRefused extends DevError {
|
|
|
33
45
|
export const SITE_UNAUTHORIZED = 'Not signed in, or the token was refused (expired or revoked). Run `sb login`, or set SB_TOKEN ' +
|
|
34
46
|
'to an API key (wbk_…) with pages.read / pages.write from Settings -> API keys.';
|
|
35
47
|
export const SITE_NO_SITE = 'Which site? Pass --site <id>, or add "siteId" to sb.json. (An API key (wbk_…) in SB_TOKEN implies its site.)';
|
|
36
|
-
const
|
|
48
|
+
const factsOf = (p) => ({
|
|
49
|
+
name: p.name,
|
|
50
|
+
slug: p.slug ?? '',
|
|
51
|
+
type: p.type,
|
|
52
|
+
isHomepage: p.isHomepage === true,
|
|
53
|
+
defaultTemplate: p.isDefaultTemplate === true,
|
|
54
|
+
settings: p.settings && typeof p.settings === 'object' ? p.settings : {},
|
|
55
|
+
});
|
|
56
|
+
/** The text of `export const page`: empty settings and a false default are left out. */
|
|
57
|
+
const metaOf = (id, path, f) => ({
|
|
58
|
+
id,
|
|
59
|
+
path,
|
|
60
|
+
name: f.name,
|
|
61
|
+
slug: f.slug,
|
|
62
|
+
type: f.type,
|
|
63
|
+
isHomepage: f.isHomepage,
|
|
64
|
+
...(f.defaultTemplate ? { defaultTemplate: true } : {}),
|
|
65
|
+
...(Object.keys(f.settings).length ? { settings: f.settings } : {}),
|
|
66
|
+
});
|
|
67
|
+
export const sha256 = (s) => createHash('sha256').update(s, 'utf8').digest('hex');
|
|
37
68
|
/**
|
|
38
69
|
* A page file: the document as `.page.tsx` (cli/src/source, SKILL sbuilder-site-source),
|
|
39
|
-
* plus the page's own facts
|
|
40
|
-
* moving a page happens in the editor; the parser ignores this export.
|
|
70
|
+
* plus the page's own facts, which push reads back (`export const page`).
|
|
41
71
|
*/
|
|
42
|
-
const fileText = (
|
|
43
|
-
|
|
44
|
-
id: p.id,
|
|
45
|
-
name: p.name,
|
|
46
|
-
path: p.path,
|
|
47
|
-
type: p.type,
|
|
48
|
-
})};\n`;
|
|
49
|
-
async function readText(path) {
|
|
72
|
+
const fileText = (id, path, f, doc) => printFile('page', doc, metaOf(id, path, f));
|
|
73
|
+
export async function readText(path) {
|
|
50
74
|
try {
|
|
51
75
|
return await readFile(path, 'utf8');
|
|
52
76
|
}
|
|
@@ -56,7 +80,7 @@ async function readText(path) {
|
|
|
56
80
|
throw err;
|
|
57
81
|
}
|
|
58
82
|
}
|
|
59
|
-
async function put(path, text) {
|
|
83
|
+
export async function put(path, text) {
|
|
60
84
|
await mkdir(dirname(path), { recursive: true });
|
|
61
85
|
const tmp = `${path}.${process.pid}.tmp`;
|
|
62
86
|
await writeFile(tmp, text, 'utf8');
|
|
@@ -80,7 +104,27 @@ export async function readManifest(dir) {
|
|
|
80
104
|
throw new SiteRefused(`${SITE_MANIFEST} names ${JSON.stringify(e?.file)}, which is not a file in ${PAGES_DIR}/ — nothing was read or sent.`);
|
|
81
105
|
}
|
|
82
106
|
}
|
|
83
|
-
|
|
107
|
+
const named = [
|
|
108
|
+
['globals', SAFE_KIND_FILE.globals, 'design/globals/'],
|
|
109
|
+
['overlays', SAFE_KIND_FILE.overlays, 'design/overlays/'],
|
|
110
|
+
['translations', SAFE_KIND_FILE.translations, 'design/translations/<locale>.json'],
|
|
111
|
+
];
|
|
112
|
+
for (const [k, re, where] of named) {
|
|
113
|
+
for (const e of Object.values(m[k] ?? {})) {
|
|
114
|
+
if (!re.test(String(e?.file)))
|
|
115
|
+
throw new SiteRefused(`${SITE_MANIFEST} names ${JSON.stringify(e?.file)}, which is not a file in ${where} — nothing was read or sent.`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (m.theme && !SAFE_KIND_FILE.theme.test(String(m.theme.file)))
|
|
119
|
+
throw new SiteRefused(`${SITE_MANIFEST} names ${JSON.stringify(m.theme.file)}, which is not design/theme.json — nothing was read or sent.`);
|
|
120
|
+
return {
|
|
121
|
+
siteId: m.siteId ?? null,
|
|
122
|
+
pages: m.pages ?? {},
|
|
123
|
+
...(m.globals ? { globals: m.globals } : {}),
|
|
124
|
+
...(m.overlays ? { overlays: m.overlays } : {}),
|
|
125
|
+
...(m.theme ? { theme: m.theme } : {}),
|
|
126
|
+
...(m.translations ? { translations: m.translations } : {}),
|
|
127
|
+
};
|
|
84
128
|
}
|
|
85
129
|
/**
|
|
86
130
|
* A symbolic link anywhere on a page file's path, or undefined. The name check
|
|
@@ -96,10 +140,8 @@ export async function linkOn(dir, file) {
|
|
|
96
140
|
}
|
|
97
141
|
return undefined;
|
|
98
142
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const saveManifest = (ctx) => put(join(ctx.dir, SITE_MANIFEST), JSON.stringify(ctx.manifest, null, 2) + '\n');
|
|
102
|
-
async function open(o) {
|
|
143
|
+
export const saveManifest = (ctx) => put(join(ctx.dir, SITE_MANIFEST), JSON.stringify(ctx.manifest, null, 2) + '\n');
|
|
144
|
+
export async function open(o) {
|
|
103
145
|
const cred = o.token;
|
|
104
146
|
if (!cred)
|
|
105
147
|
throw new SiteRefused(SITE_UNAUTHORIZED);
|
|
@@ -128,14 +170,19 @@ async function open(o) {
|
|
|
128
170
|
manifest,
|
|
129
171
|
log,
|
|
130
172
|
warn: o.warn ?? log,
|
|
173
|
+
o,
|
|
131
174
|
};
|
|
132
175
|
}
|
|
133
|
-
async function send(ctx, url, method, body) {
|
|
176
|
+
export async function send(ctx, url, method, body, headers) {
|
|
134
177
|
let res;
|
|
135
178
|
try {
|
|
136
179
|
res = await authedFetch(url, body === undefined
|
|
137
|
-
? { method }
|
|
138
|
-
: {
|
|
180
|
+
? { method, ...(headers ? { headers } : {}) }
|
|
181
|
+
: {
|
|
182
|
+
method,
|
|
183
|
+
headers: { 'Content-Type': 'application/json', ...headers },
|
|
184
|
+
body: JSON.stringify(body),
|
|
185
|
+
}, ctx.cred);
|
|
139
186
|
}
|
|
140
187
|
catch (err) {
|
|
141
188
|
if (err instanceof DevError)
|
|
@@ -146,7 +193,7 @@ async function send(ctx, url, method, body) {
|
|
|
146
193
|
throw new SiteRefused(SITE_UNAUTHORIZED);
|
|
147
194
|
return res;
|
|
148
195
|
}
|
|
149
|
-
async function errorOf(res) {
|
|
196
|
+
export async function errorOf(res) {
|
|
150
197
|
try {
|
|
151
198
|
return (await res.json());
|
|
152
199
|
}
|
|
@@ -210,7 +257,10 @@ export async function pull(o) {
|
|
|
210
257
|
if (!draft)
|
|
211
258
|
continue; // a page with no draft yet has nothing to hold
|
|
212
259
|
const entry = ctx.manifest.pages[p.id];
|
|
213
|
-
|
|
260
|
+
const facts = factsOf(p);
|
|
261
|
+
// A page synced before facts were recorded has no base for them: it is NOT "unchanged" — the
|
|
262
|
+
// file is rewritten from the platform (or kept and named if you edited it), which records them.
|
|
263
|
+
if (entry && entry.rev === draft.rev && entry.page && isDeepStrictEqual(entry.page, facts)) {
|
|
214
264
|
// The draft has not moved, but the page may have been re-pathed in the editor.
|
|
215
265
|
if (entry.path !== p.path && !o.dryRun) {
|
|
216
266
|
entry.path = p.path;
|
|
@@ -231,12 +281,15 @@ export async function pull(o) {
|
|
|
231
281
|
clashes++;
|
|
232
282
|
continue;
|
|
233
283
|
}
|
|
234
|
-
if (!entry &&
|
|
284
|
+
if (!entry &&
|
|
285
|
+
local !== undefined &&
|
|
286
|
+
local !== fileText(p.id, p.path, facts, draft.document) &&
|
|
287
|
+
!o.force) {
|
|
235
288
|
ctx.warn(` ! ${file}: already exists and is not tracked — kept it. Move it aside, or --force.`);
|
|
236
289
|
clashes++;
|
|
237
290
|
continue;
|
|
238
291
|
}
|
|
239
|
-
const text = fileText(p, draft);
|
|
292
|
+
const text = fileText(p.id, p.path, facts, draft.document);
|
|
240
293
|
if (!o.dryRun) {
|
|
241
294
|
await put(join(ctx.dir, file), text);
|
|
242
295
|
ctx.manifest.pages[p.id] = {
|
|
@@ -245,6 +298,7 @@ export async function pull(o) {
|
|
|
245
298
|
sha256: sha256(text),
|
|
246
299
|
schemaVersion: draft.schemaVersion,
|
|
247
300
|
path: p.path,
|
|
301
|
+
page: facts,
|
|
248
302
|
appBlocks: appBlockSeals(draft.document),
|
|
249
303
|
};
|
|
250
304
|
await saveManifest(ctx);
|
|
@@ -263,76 +317,335 @@ export async function pull(o) {
|
|
|
263
317
|
if (!o.dryRun)
|
|
264
318
|
await saveManifest(ctx);
|
|
265
319
|
ctx.log(`${wrote} page(s) ${o.dryRun ? 'would be ' : ''}pulled${clashes ? `, ${clashes} kept` : ''}.`);
|
|
266
|
-
|
|
320
|
+
const more = await pullKinds(ctx);
|
|
321
|
+
for (const line of await staleTranslations(ctx.dir))
|
|
322
|
+
ctx.warn(` ~ STALE ${line}`);
|
|
323
|
+
if (more.wrote || more.clashes)
|
|
324
|
+
ctx.log(`${more.wrote} other file(s) ${o.dryRun ? 'would be ' : ''}pulled${more.clashes ? `, ${more.clashes} kept` : ''}.`);
|
|
325
|
+
return clashes || more.clashes ? 1 : 0;
|
|
267
326
|
}
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
327
|
+
/** The platform's answer to a page write: `{ page: {…} }` on both surfaces. */
|
|
328
|
+
const pageOf = async (res) => (await res.json()).page;
|
|
329
|
+
export const refusal = (err) => `${err.code ? `${err.code}: ` : ''}${err.error}`;
|
|
330
|
+
const ABSENT = Symbol('absent');
|
|
331
|
+
const isPlain = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
332
|
+
const sameV = (a, b) => a === ABSENT || b === ABSENT ? a === b : isDeepStrictEqual(a, b);
|
|
333
|
+
const at = (o, k) => (k in o ? o[k] : ABSENT);
|
|
334
|
+
/**
|
|
335
|
+
* The three-way merge of the page's `settings` (its SEO blob), PER KEY, recursively over plain
|
|
336
|
+
* objects — arrays and scalars are leaves, and a key added or removed is a change of that key.
|
|
337
|
+
* A SEO title edited in the editor and a description edited in the file therefore both survive.
|
|
338
|
+
*
|
|
339
|
+
* merged what to send: ours where only ours changed, theirs where only theirs changed
|
|
340
|
+
* held what the FILE will hold afterwards: the same, but a key only the editor changed keeps its
|
|
341
|
+
* base value (it reaches the file only through the rewrite — see the base-advance rule)
|
|
342
|
+
* conflicts `settings.seo.title`-style paths changed to different values on both sides
|
|
343
|
+
*/
|
|
344
|
+
export function mergeSettings(base, ours, theirs, force) {
|
|
345
|
+
const conflicts = [];
|
|
346
|
+
const three = (b, o, t, path) => {
|
|
347
|
+
if (sameV(o, b))
|
|
348
|
+
return [t, b]; // ours did not change it: the platform's value stands
|
|
349
|
+
if (sameV(t, b) || sameV(o, t))
|
|
350
|
+
return [o, o]; // only ours changed, or both made the same change
|
|
351
|
+
if (isPlain(o) && isPlain(t) && (isPlain(b) || b === ABSENT)) {
|
|
352
|
+
const bb = isPlain(b) ? b : {};
|
|
353
|
+
const m = {};
|
|
354
|
+
const h = {};
|
|
355
|
+
for (const k of new Set([...Object.keys(bb), ...Object.keys(o), ...Object.keys(t)])) {
|
|
356
|
+
const [mv, hv] = three(at(bb, k), at(o, k), at(t, k), `${path}.${k}`);
|
|
357
|
+
if (mv !== ABSENT)
|
|
358
|
+
m[k] = mv;
|
|
359
|
+
if (hv !== ABSENT)
|
|
360
|
+
h[k] = hv;
|
|
361
|
+
}
|
|
362
|
+
return [m, h];
|
|
279
363
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
364
|
+
if (!force)
|
|
365
|
+
conflicts.push(path);
|
|
366
|
+
return force ? [o, o] : [t, b];
|
|
367
|
+
};
|
|
368
|
+
const [merged, held] = three(base, ours, theirs, 'settings');
|
|
369
|
+
return { merged, held, conflicts };
|
|
370
|
+
}
|
|
371
|
+
const FACT_KEYS = ['name', 'slug', 'type', 'isHomepage', 'settings', 'defaultTemplate'];
|
|
372
|
+
/**
|
|
373
|
+
* Three-way, per fact, like the document: `base` is what was last synced, `meta` is the file
|
|
374
|
+
* (ours), `theirs` is the platform now. Only ours changed → send it; only theirs changed → leave
|
|
375
|
+
* it (it is recorded and the file takes it); both changed to the same value → nothing; both
|
|
376
|
+
* changed differently → a conflict, unless `force` picks the file.
|
|
377
|
+
*
|
|
378
|
+
* A key the file does not state is not a change (printing omits an empty `settings` and a false
|
|
379
|
+
* `defaultTemplate`); a default template can be set but not unset — make another page it.
|
|
380
|
+
*/
|
|
381
|
+
export function mergeFacts(meta, base, theirs, force, warn) {
|
|
382
|
+
const send = {};
|
|
383
|
+
const conflicts = [];
|
|
384
|
+
const held = {};
|
|
385
|
+
if (!meta)
|
|
386
|
+
return { send, conflicts, held };
|
|
387
|
+
if (meta.defaultTemplate === false && base.defaultTemplate)
|
|
388
|
+
warn(' ! defaultTemplate cannot be switched off — make another page of this type the default.');
|
|
389
|
+
for (const k of FACT_KEYS) {
|
|
390
|
+
if (k === 'settings') {
|
|
391
|
+
// One fact per KEY inside it, not one fact for the whole blob.
|
|
392
|
+
if (!('settings' in meta) || !isPlain(meta.settings))
|
|
393
|
+
continue;
|
|
394
|
+
const m = mergeSettings(base.settings, meta.settings, theirs.settings, force);
|
|
395
|
+
conflicts.push(...m.conflicts);
|
|
396
|
+
held.settings = m.held;
|
|
397
|
+
if (!isDeepStrictEqual(m.merged, theirs.settings))
|
|
398
|
+
send.settings = m.merged;
|
|
284
399
|
continue;
|
|
285
400
|
}
|
|
286
|
-
const
|
|
287
|
-
|
|
401
|
+
const ours = meta[k];
|
|
402
|
+
const changed = k === 'defaultTemplate'
|
|
403
|
+
? ours === true && !base[k]
|
|
404
|
+
: k in meta && !isDeepStrictEqual(ours, base[k]);
|
|
405
|
+
if (!changed || isDeepStrictEqual(ours, theirs[k]))
|
|
288
406
|
continue;
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
407
|
+
if (force || isDeepStrictEqual(theirs[k], base[k]))
|
|
408
|
+
send[k] = ours;
|
|
409
|
+
else
|
|
410
|
+
conflicts.push(k);
|
|
411
|
+
}
|
|
412
|
+
return { send, conflicts, held };
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* The file's facts when nothing records the last sync: any fact it states differently from the
|
|
416
|
+
* platform is a conflict (--force sends it). Equal facts are fine; absent ones are left alone.
|
|
417
|
+
*/
|
|
418
|
+
export function unrecordedFacts(meta, now, force) {
|
|
419
|
+
const send = {};
|
|
420
|
+
const conflicts = [];
|
|
421
|
+
for (const k of FACT_KEYS) {
|
|
422
|
+
if (!meta || !(k in meta))
|
|
423
|
+
continue;
|
|
424
|
+
if (k === 'defaultTemplate' ? meta[k] !== true || now[k] : isDeepStrictEqual(meta[k], now[k]))
|
|
299
425
|
continue;
|
|
426
|
+
if (force)
|
|
427
|
+
send[k] = meta[k];
|
|
428
|
+
else
|
|
429
|
+
conflicts.push(k);
|
|
430
|
+
}
|
|
431
|
+
return { send, conflicts };
|
|
432
|
+
}
|
|
433
|
+
/**
|
|
434
|
+
* Rewrites `file` to `back` only if it still holds `seen` — the text this push read. A save made
|
|
435
|
+
* while the push was in flight (routine under `sb site dev`) is never overwritten; it is said, and
|
|
436
|
+
* it goes up on the next push. True when the file holds `back` now.
|
|
437
|
+
*/
|
|
438
|
+
export async function writeBack(ctx, file, seen, back) {
|
|
439
|
+
if (back === seen)
|
|
440
|
+
return true;
|
|
441
|
+
if ((await readText(join(ctx.dir, file))) !== seen) {
|
|
442
|
+
ctx.warn(` ! ${file}: changed while it was being pushed — not rewritten; the newer save goes up on the next push (elements in it without an id get new ids then).`);
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
await put(join(ctx.dir, file), back);
|
|
446
|
+
return true;
|
|
447
|
+
}
|
|
448
|
+
/** What the platform settled on is not always what was asked (a slug is de-duplicated): say so. */
|
|
449
|
+
function verifyFacts(ctx, file, asked, got) {
|
|
450
|
+
const now = factsOf(got);
|
|
451
|
+
for (const [k, want] of Object.entries(asked))
|
|
452
|
+
if (!isDeepStrictEqual(now[k], want))
|
|
453
|
+
ctx.warn(` ! ${file}: asked ${k} ${JSON.stringify(want)}, the platform has ${JSON.stringify(now[k])}.`);
|
|
454
|
+
}
|
|
455
|
+
/** PATCH the changed facts (and the default template, which the session surface sets by PUT). */
|
|
456
|
+
async function sendFacts(ctx, id, file, changes,
|
|
457
|
+
/** The platform's facts as the three-way read them: sent as `base` (v1 only). */
|
|
458
|
+
seen,
|
|
459
|
+
/** On a --force retry: what to send against the page as it is NOW (the three-way, redone). */
|
|
460
|
+
refresh) {
|
|
461
|
+
const url = `${ctx.base}/pages/${encodeURIComponent(id)}`;
|
|
462
|
+
/**
|
|
463
|
+
* The v1 PATCH is fenced: `base` carries the value of each field being changed as this push read
|
|
464
|
+
* it, and the platform refuses (409 source_stale, naming the field) if it moved since — so an
|
|
465
|
+
* editor save landing between the read and the write is never overwritten. The session surface
|
|
466
|
+
* has no such fence.
|
|
467
|
+
*/
|
|
468
|
+
const patchOf = (ch, b) => {
|
|
469
|
+
const { defaultTemplate, ...patch } = ch;
|
|
470
|
+
const body = ctx.v1 && defaultTemplate ? { ...patch, defaultTemplate } : patch;
|
|
471
|
+
return ctx.v1 && b
|
|
472
|
+
? {
|
|
473
|
+
...body,
|
|
474
|
+
base: Object.fromEntries(['name', 'slug', 'type', 'isHomepage', 'settings']
|
|
475
|
+
.filter((f) => f in patch)
|
|
476
|
+
.map((f) => [f, b[f]])),
|
|
477
|
+
}
|
|
478
|
+
: body;
|
|
479
|
+
};
|
|
480
|
+
let sent = changes;
|
|
481
|
+
const { defaultTemplate } = changes;
|
|
482
|
+
const calls = [];
|
|
483
|
+
if (Object.keys(patchOf(changes)).length)
|
|
484
|
+
calls.push(['PATCH', url, patchOf(changes, seen)]);
|
|
485
|
+
// Only the session surface sets the default by its own route; v1 takes it in the PATCH.
|
|
486
|
+
if (!ctx.v1 && defaultTemplate)
|
|
487
|
+
calls.push(['PUT', `${url}/default-template`, undefined]);
|
|
488
|
+
let got;
|
|
489
|
+
for (const [method, to, payload] of calls) {
|
|
490
|
+
let res = await send(ctx, to, method, payload);
|
|
491
|
+
if (res.status === 409 && method === 'PATCH' && ctx.v1 && seen && ctx.o.force) {
|
|
492
|
+
// --force: re-read, REDO the merge against the page as it is now (so an editor save to a key
|
|
493
|
+
// the file did not touch survives), and send once more.
|
|
494
|
+
const fresh = await send(ctx, url, 'GET');
|
|
495
|
+
if (fresh.ok) {
|
|
496
|
+
const page = await pageOf(fresh);
|
|
497
|
+
sent = refresh ? refresh(factsOf(page)) : changes;
|
|
498
|
+
const body = patchOf(sent, factsOf(page));
|
|
499
|
+
if (Object.keys(body).length)
|
|
500
|
+
res = await send(ctx, to, method, body);
|
|
501
|
+
else
|
|
502
|
+
res = fresh; // nothing of the file's is left to send
|
|
503
|
+
}
|
|
300
504
|
}
|
|
505
|
+
if (!res.ok) {
|
|
506
|
+
const err = await errorOf(res);
|
|
507
|
+
ctx.warn(err.code === 'source_stale' && err.details?.field
|
|
508
|
+
? ` ! ${file}: ${err.details.field} changed in the editor while pushing — nothing sent. Pull to take the editor's, or sb site push --force to overwrite it.`
|
|
509
|
+
: ` ! ${file}: the page's facts were refused (${refusal(err)}) — nothing sent.`);
|
|
510
|
+
return undefined;
|
|
511
|
+
}
|
|
512
|
+
got = await pageOf(res);
|
|
513
|
+
}
|
|
514
|
+
if (got)
|
|
515
|
+
verifyFacts(ctx, file, sent, got);
|
|
516
|
+
return got;
|
|
517
|
+
}
|
|
518
|
+
export async function push(o) {
|
|
519
|
+
const ctx = await open(o);
|
|
520
|
+
const tracked = new Set(Object.values(ctx.manifest.pages).map((e) => e.file));
|
|
521
|
+
let failed = 0;
|
|
522
|
+
let sent = 0;
|
|
523
|
+
let listed;
|
|
524
|
+
const server = async () => (listed ??= await listPages(ctx));
|
|
525
|
+
/** Saves one page: its facts first (a refusal stops the file), then its document. */
|
|
526
|
+
async function savePage(id, entry, text, document, meta, created) {
|
|
301
527
|
const edited = editedAppBlocks(document, entry.appBlocks);
|
|
302
528
|
if (edited.length) {
|
|
303
529
|
// Refused here, not on the server: the platform would save the page and quietly
|
|
304
530
|
// drop the edit, which reads as success. --force does not change that outcome.
|
|
305
531
|
ctx.warn(APP_BLOCK_EDITED(entry.file, edited));
|
|
306
532
|
failed++;
|
|
307
|
-
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
// A page this push created or adopted (rev < 0) takes the platform's revision ONCE, now, and
|
|
536
|
+
// keeps it: a retry after a failed save then 409s if the editor moved meanwhile, instead of
|
|
537
|
+
// re-reading the current rev and overwriting the editor's edit.
|
|
538
|
+
if (!o.dryRun && entry.rev < 0) {
|
|
539
|
+
const draft = await getDraft(ctx, id);
|
|
540
|
+
entry.rev = draft?.rev ?? 0;
|
|
541
|
+
entry.schemaVersion = draft?.schemaVersion;
|
|
542
|
+
await saveManifest(ctx);
|
|
543
|
+
}
|
|
544
|
+
// Three-way per fact (mergeFacts): an edit made in the editor is neither undone by a file that
|
|
545
|
+
// never mentioned it nor overwritten by one that changed the same fact differently.
|
|
546
|
+
let theirs;
|
|
547
|
+
let settled;
|
|
548
|
+
let patched = false;
|
|
549
|
+
if (!created) {
|
|
550
|
+
const res = await send(ctx, `${ctx.base}/pages/${encodeURIComponent(id)}`, 'GET');
|
|
551
|
+
if (!res.ok && res.status !== 404) {
|
|
552
|
+
ctx.warn(` ! ${entry.file}: reading the page failed (${refusal(await errorOf(res))}) — nothing sent.`);
|
|
553
|
+
failed++;
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
theirs = res.ok ? await pageOf(res) : undefined;
|
|
557
|
+
}
|
|
558
|
+
let changes = {};
|
|
559
|
+
let held = {};
|
|
560
|
+
if (theirs) {
|
|
561
|
+
const now = factsOf(theirs);
|
|
562
|
+
let conflicts;
|
|
563
|
+
if (entry.page)
|
|
564
|
+
({ send: changes, conflicts, held } = mergeFacts(meta, entry.page, now, o.force, ctx.warn));
|
|
565
|
+
else {
|
|
566
|
+
// No record of the last sync (a manifest from before facts were kept): there is no base, so
|
|
567
|
+
// a fact the file states differently from the platform cannot be told from an editor edit
|
|
568
|
+
// the file never saw. Name it; never guess.
|
|
569
|
+
({ send: changes, conflicts } = unrecordedFacts(meta, now, o.force));
|
|
570
|
+
for (const k of conflicts)
|
|
571
|
+
ctx.warn(` ! ${entry.file}: ${k} differs from the platform and this repo has no record of the last sync — ` +
|
|
572
|
+
'sb site pull to take the platform’s, or sb site push --force to send the file’s. Nothing sent for this page.');
|
|
573
|
+
}
|
|
574
|
+
if (entry.page)
|
|
575
|
+
for (const k of conflicts)
|
|
576
|
+
ctx.warn(` ! ${entry.file}: ${k} changed here AND in the editor — kept both; ` +
|
|
577
|
+
"pull to take the editor's, or sb site push --force. Nothing sent for this page.");
|
|
578
|
+
if (conflicts.length) {
|
|
579
|
+
failed++;
|
|
580
|
+
o.report?.(entry.file, 'refused');
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
308
583
|
}
|
|
309
584
|
if (o.dryRun) {
|
|
310
585
|
ctx.log(` ↑ ${entry.file} (dry run)`);
|
|
311
586
|
sent++;
|
|
312
|
-
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
if (theirs) {
|
|
590
|
+
const now = factsOf(theirs);
|
|
591
|
+
patched = Object.keys(changes).length > 0;
|
|
592
|
+
const got = patched
|
|
593
|
+
? await sendFacts(ctx, id, entry.file, changes, now, (fresh) => mergeFacts(meta, entry.page ?? now, fresh, true, () => { }).send)
|
|
594
|
+
: theirs;
|
|
595
|
+
if (!got) {
|
|
596
|
+
failed++;
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
// A fact's recorded base may only move to a value the FILE holds: it is what the next push
|
|
600
|
+
// compares the file with, so one that ran ahead of the file would revert the editor's change
|
|
601
|
+
// if the document save below failed. Sent facts hold the file's value; a fact the file
|
|
602
|
+
// already states as the platform has it is the same. The rest wait for the rewrite.
|
|
603
|
+
const base = { ...(entry.page ?? now) };
|
|
604
|
+
for (const k of FACT_KEYS) {
|
|
605
|
+
if (k in changes)
|
|
606
|
+
base[k] = changes[k];
|
|
607
|
+
else if (meta && k in meta && isDeepStrictEqual(meta[k], now[k]))
|
|
608
|
+
base[k] = now[k];
|
|
609
|
+
}
|
|
610
|
+
// `settings` is held key by key: what the file holds, not the merged blob (which carries
|
|
611
|
+
// the editor's keys the file has not seen yet).
|
|
612
|
+
if (held.settings !== undefined)
|
|
613
|
+
base.settings = held.settings;
|
|
614
|
+
entry.page = base;
|
|
615
|
+
await saveManifest(ctx);
|
|
616
|
+
settled = got;
|
|
313
617
|
}
|
|
314
618
|
const body = (baseRev) => ({
|
|
315
619
|
document,
|
|
316
620
|
schemaVersion: entry.schemaVersion ?? document.schema_version,
|
|
317
621
|
baseRev,
|
|
318
622
|
});
|
|
319
|
-
|
|
623
|
+
const url = docURL(ctx, id);
|
|
624
|
+
let res = await send(ctx, url, 'PUT', body(entry.rev));
|
|
320
625
|
if (res.status === 409 && o.force) {
|
|
321
626
|
const err = await errorOf(res);
|
|
322
627
|
const cur = Number(err.details?.rev);
|
|
323
628
|
if (err.code === 'source_stale' && cur > 0)
|
|
324
|
-
res = await send(ctx,
|
|
629
|
+
res = await send(ctx, url, 'PUT', body(cur));
|
|
325
630
|
}
|
|
326
631
|
if (!res.ok) {
|
|
327
632
|
const err = await errorOf(res);
|
|
633
|
+
const saved = patched ? "the page's facts were saved, the document was not: " : '';
|
|
328
634
|
ctx.warn(err.code === 'source_stale'
|
|
329
|
-
? ` ! ${entry.file}:
|
|
635
|
+
? ` ! ${entry.file}: ${saved}` +
|
|
636
|
+
(patched
|
|
637
|
+
? 'edited in the editor since your last pull — the document was not sent. '
|
|
638
|
+
: 'edited in the editor since your last pull — nothing sent. ') +
|
|
330
639
|
'Pull and merge, or sb site push --force to overwrite the editor’s draft.'
|
|
331
|
-
: ` ! ${entry.file}:
|
|
640
|
+
: ` ! ${entry.file}: ${saved}refused (${refusal(err)})`);
|
|
332
641
|
failed++;
|
|
333
|
-
|
|
642
|
+
o.report?.(entry.file, err.code === 'source_stale' ? 'stale' : 'refused');
|
|
643
|
+
return;
|
|
334
644
|
}
|
|
335
645
|
const draft = await draftOf(res, ctx.v1);
|
|
646
|
+
// `path` is derived from the slug, information only: it may follow the platform at once.
|
|
647
|
+
if (settled)
|
|
648
|
+
entry.path = settled.path;
|
|
336
649
|
for (const w of draft.warnings ?? []) {
|
|
337
650
|
const subject = Object.entries(w)
|
|
338
651
|
.filter(([k, v]) => k !== 'code' && typeof v === 'string' && v)
|
|
@@ -340,27 +653,258 @@ export async function push(o) {
|
|
|
340
653
|
.join(', ');
|
|
341
654
|
ctx.warn(` ! ${entry.file}: the platform says ${w.code}${subject ? ` (${subject})` : ''}`);
|
|
342
655
|
}
|
|
656
|
+
// The file becomes what the platform STORED — minted ids, the facts it settled on — so the
|
|
657
|
+
// next pull finds nothing to change. (A file the platform stored as sent prints as itself.)
|
|
658
|
+
const saved = draft.document ?? document;
|
|
659
|
+
const facts = settled ? factsOf(settled) : entry.page;
|
|
660
|
+
const out = facts
|
|
661
|
+
? fileText(id, entry.path ?? '', facts, saved)
|
|
662
|
+
: printFile('page', saved, undefined);
|
|
663
|
+
const wrote = await writeBack(ctx, entry.file, text, out);
|
|
664
|
+
// The recorded facts are what the FILE holds. The platform's facts reach the file only through
|
|
665
|
+
// the rewrite: when it was declined (a newer save landed meanwhile) the base keeps just what
|
|
666
|
+
// this push sent and what the file already stated, so an editor-only change is not mistaken
|
|
667
|
+
// for the file's own on the next push and reverted.
|
|
668
|
+
if (wrote && settled)
|
|
669
|
+
entry.page = facts;
|
|
343
670
|
ctx.manifest.pages[id] = {
|
|
344
671
|
...entry,
|
|
345
|
-
appBlocks: appBlockSeals(
|
|
672
|
+
appBlocks: appBlockSeals(saved),
|
|
346
673
|
rev: draft.rev,
|
|
347
|
-
|
|
674
|
+
// What the file holds: the rewrite, or — when it was saved over meanwhile — the text we sent,
|
|
675
|
+
// so the newer save differs from it and goes up next.
|
|
676
|
+
sha256: sha256(wrote ? out : text),
|
|
348
677
|
schemaVersion: draft.schemaVersion,
|
|
349
678
|
};
|
|
350
679
|
await saveManifest(ctx);
|
|
351
680
|
ctx.log(` ↑ ${entry.file}`);
|
|
352
681
|
sent++;
|
|
682
|
+
o.report?.(entry.file, 'sent');
|
|
683
|
+
}
|
|
684
|
+
const missing = [];
|
|
685
|
+
for (const [id, entry] of Object.entries(ctx.manifest.pages)) {
|
|
686
|
+
if (o.only && !o.only.has(entry.file))
|
|
687
|
+
continue;
|
|
688
|
+
const link = await linkOn(ctx.dir, entry.file);
|
|
689
|
+
if (link) {
|
|
690
|
+
ctx.warn(` ! ${entry.file}: ${link} is a symbolic link — not followed, nothing sent.`);
|
|
691
|
+
failed++;
|
|
692
|
+
continue;
|
|
693
|
+
}
|
|
694
|
+
const text = await readText(join(ctx.dir, entry.file));
|
|
695
|
+
if (text === undefined) {
|
|
696
|
+
missing.push([id, entry]);
|
|
697
|
+
continue;
|
|
698
|
+
}
|
|
699
|
+
if (sha256(text) === entry.sha256)
|
|
700
|
+
continue;
|
|
701
|
+
let parsed;
|
|
702
|
+
try {
|
|
703
|
+
parsed = parseFile(text, 'page');
|
|
704
|
+
}
|
|
705
|
+
catch (err) {
|
|
706
|
+
// file:line:col — clickable in a terminal and in an editor's problem matcher.
|
|
707
|
+
ctx.warn(err instanceof SourceError
|
|
708
|
+
? ` ! ${entry.file}:${err.message} — nothing sent.`
|
|
709
|
+
: ` ! ${entry.file}: ${messageOf(err)} — nothing sent.`);
|
|
710
|
+
failed++;
|
|
711
|
+
o.report?.(entry.file, 'refused');
|
|
712
|
+
continue;
|
|
713
|
+
}
|
|
714
|
+
await savePage(id, entry, text, parsed.document, parsed.meta, false);
|
|
353
715
|
}
|
|
354
716
|
const local = await readdir(join(ctx.dir, PAGES_DIR)).catch(() => []);
|
|
355
|
-
for (const f of local) {
|
|
717
|
+
for (const f of local.sort()) {
|
|
356
718
|
const file = `${PAGES_DIR}/${f}`;
|
|
357
|
-
if (f.endsWith('.page.tsx') && !
|
|
358
|
-
|
|
719
|
+
if (!f.endsWith('.page.tsx') || tracked.has(file) || (o.only && !o.only.has(file)))
|
|
720
|
+
continue;
|
|
721
|
+
// Untracked: a file with `export const page` is a NEW page; one without is only reported.
|
|
722
|
+
const link = SAFE_FILE.test(file) ? await linkOn(ctx.dir, file) : file;
|
|
723
|
+
if (link) {
|
|
724
|
+
ctx.warn(` ! ${file}: ${link} is not a plain page file — not read, nothing sent.`);
|
|
725
|
+
failed++;
|
|
726
|
+
continue;
|
|
727
|
+
}
|
|
728
|
+
const text = (await readText(join(ctx.dir, file)));
|
|
729
|
+
let parsed;
|
|
730
|
+
try {
|
|
731
|
+
parsed = parseFile(text, 'page');
|
|
732
|
+
}
|
|
733
|
+
catch (err) {
|
|
734
|
+
ctx.warn(err instanceof SourceError
|
|
735
|
+
? ` ! ${file}:${err.message} — nothing sent.`
|
|
736
|
+
: ` ! ${file}: ${messageOf(err)} — nothing sent.`);
|
|
737
|
+
failed++;
|
|
738
|
+
continue;
|
|
739
|
+
}
|
|
740
|
+
const meta = parsed.meta;
|
|
741
|
+
if (!meta) {
|
|
742
|
+
ctx.warn(` ? ${file}: not a tracked page — create the page in the editor, then sb site pull ` +
|
|
743
|
+
'(or add `export const page = { name: "…" }` to create it from this file).');
|
|
744
|
+
continue;
|
|
745
|
+
}
|
|
746
|
+
if (typeof meta.name !== 'string' || !meta.name.trim()) {
|
|
747
|
+
ctx.warn(` ! ${file}: \`export const page\` needs a name to create the page — nothing sent.`);
|
|
748
|
+
failed++;
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
const known = typeof meta.id === 'string' ? (await server()).find((p) => p.id === meta.id) : undefined;
|
|
752
|
+
if (typeof meta.id === 'string' && !known) {
|
|
753
|
+
// An id the platform does not have: a page deleted there, or a file copied from another site.
|
|
754
|
+
// Creating "a new one" would silently break the identity the file claims.
|
|
755
|
+
ctx.warn(` ! ${file}: names page ${JSON.stringify(meta.id)}, which this site does not have — ` +
|
|
756
|
+
'remove `id` from `export const page` to create a new page. Nothing sent.');
|
|
757
|
+
failed++;
|
|
758
|
+
continue;
|
|
759
|
+
}
|
|
760
|
+
if (known) {
|
|
761
|
+
// Its id names a page that exists but is not tracked here (a lost manifest, a copied file):
|
|
762
|
+
// sending would overwrite that page's draft with no fence, so only --force does.
|
|
763
|
+
if (!o.force || ctx.manifest.pages[known.id]) {
|
|
764
|
+
ctx.warn(` ! ${file}: names page ${known.id}, which exists but is not tracked from this file — ` +
|
|
765
|
+
'sb site pull to take it, or --force to overwrite its draft. Nothing sent.');
|
|
766
|
+
failed++;
|
|
767
|
+
continue;
|
|
768
|
+
}
|
|
769
|
+
const entry = {
|
|
770
|
+
file,
|
|
771
|
+
rev: -1,
|
|
772
|
+
sha256: '',
|
|
773
|
+
path: known.path,
|
|
774
|
+
page: factsOf(known),
|
|
775
|
+
};
|
|
776
|
+
if (!o.dryRun)
|
|
777
|
+
ctx.manifest.pages[known.id] = entry;
|
|
778
|
+
await savePage(known.id, entry, text, parsed.document, meta, false);
|
|
779
|
+
continue;
|
|
780
|
+
}
|
|
781
|
+
if (o.dryRun) {
|
|
782
|
+
ctx.log(` + ${file} (dry run: would create page ${JSON.stringify(meta.name)})`);
|
|
783
|
+
sent++;
|
|
784
|
+
continue;
|
|
785
|
+
}
|
|
786
|
+
const asked = { name: meta.name };
|
|
787
|
+
for (const k of ['slug', 'type'])
|
|
788
|
+
if (typeof meta[k] === 'string')
|
|
789
|
+
asked[k] = meta[k];
|
|
790
|
+
if (meta.isHomepage === true)
|
|
791
|
+
asked.isHomepage = true;
|
|
792
|
+
if (meta.settings && Object.keys(meta.settings).length)
|
|
793
|
+
asked.settings = meta.settings;
|
|
794
|
+
const res = await send(ctx, `${ctx.base}/pages`, 'POST', asked);
|
|
795
|
+
if (!res.ok) {
|
|
796
|
+
ctx.warn(` ! ${file}: the page was refused (${refusal(await errorOf(res))}) — nothing created.`);
|
|
797
|
+
failed++;
|
|
798
|
+
continue;
|
|
799
|
+
}
|
|
800
|
+
const made = await pageOf(res);
|
|
801
|
+
// Recorded BEFORE the document goes up: if that fails the page exists, and the next push
|
|
802
|
+
// must retry this file rather than create a second page.
|
|
803
|
+
const entry = {
|
|
804
|
+
file,
|
|
805
|
+
rev: -1,
|
|
806
|
+
sha256: '',
|
|
807
|
+
path: made.path,
|
|
808
|
+
page: factsOf(made),
|
|
809
|
+
};
|
|
810
|
+
ctx.manifest.pages[made.id] = entry;
|
|
811
|
+
await saveManifest(ctx);
|
|
812
|
+
verifyFacts(ctx, file, asked, made);
|
|
813
|
+
if (meta.defaultTemplate === true) {
|
|
814
|
+
const got = await sendFacts(ctx, made.id, file, { defaultTemplate: true });
|
|
815
|
+
if (got)
|
|
816
|
+
entry.page = factsOf(got);
|
|
817
|
+
}
|
|
818
|
+
ctx.log(` + ${file} (page ${made.id} created)`);
|
|
819
|
+
await savePage(made.id, entry, text, parsed.document, meta, true);
|
|
820
|
+
}
|
|
821
|
+
if (missing.length && o.only)
|
|
822
|
+
for (const [, e] of missing)
|
|
823
|
+
ctx.warn(` ! ${e.file}: missing — not deleted (sb site dev never prunes).`);
|
|
824
|
+
if (missing.length && !o.only) {
|
|
825
|
+
if (!o.prune) {
|
|
826
|
+
for (const [id, e] of missing) {
|
|
827
|
+
ctx.warn(` ! ${e.file}: missing — the page ${JSON.stringify(e.page?.name ?? id)} is kept on the platform; ` +
|
|
828
|
+
'sb site push --prune deletes it.');
|
|
829
|
+
failed++;
|
|
830
|
+
}
|
|
359
831
|
}
|
|
832
|
+
else if (!(await prune(ctx, o, missing)))
|
|
833
|
+
failed++;
|
|
360
834
|
}
|
|
361
835
|
ctx.log(`${sent} page draft(s) ${o.dryRun ? 'would be ' : ''}saved. Publish with sb site publish when ready.`);
|
|
362
|
-
|
|
836
|
+
const more = await pushKinds(ctx, gatePrune(ctx, o));
|
|
837
|
+
if (more.sent)
|
|
838
|
+
ctx.log(`${more.sent} other file(s) ${o.dryRun ? 'would be ' : ''}saved.`);
|
|
839
|
+
return failed || more.failed ? 1 : 0;
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* `push --prune`: delete the pages whose tracked file is gone. The list is printed first; off a
|
|
843
|
+
* terminal `--yes` is required, on one the answer is asked. False when something was refused.
|
|
844
|
+
*/
|
|
845
|
+
async function prune(ctx, o, missing) {
|
|
846
|
+
const go = await gatePrune(ctx, o)(PAGES_DIR, `${missing.length} page(s)`, missing.map(([id, e]) => ` - ${e.file} (${JSON.stringify(e.page?.name ?? id)}, ${id})`));
|
|
847
|
+
if (go !== 'delete')
|
|
848
|
+
return go === 'stop';
|
|
849
|
+
let ok = true;
|
|
850
|
+
for (const [id, e] of missing) {
|
|
851
|
+
// The platform's draft must still be the one this file stood for: a page edited in the editor
|
|
852
|
+
// since the last sync is not deleted on the strength of a file that never saw the edit.
|
|
853
|
+
if (e.rev < 0) {
|
|
854
|
+
ctx.warn(` ! ${e.file}: this page was created from the file but never saved from it — not deleted (the draft is the platform's). Push it first, or delete the page in the editor.`);
|
|
855
|
+
ok = false;
|
|
856
|
+
continue;
|
|
857
|
+
}
|
|
858
|
+
const draft = await getDraft(ctx, id);
|
|
859
|
+
if (draft && draft.rev !== e.rev) {
|
|
860
|
+
ctx.warn(` ! ${e.file}: the page was edited in the editor since your last pull — not deleted. Pull, then decide.`);
|
|
861
|
+
ok = false;
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
const res = await send(ctx, `${ctx.base}/pages/${encodeURIComponent(id)}`, 'DELETE', undefined, { 'If-Match': String(e.rev) });
|
|
865
|
+
if (res.ok || res.status === 404) {
|
|
866
|
+
delete ctx.manifest.pages[id];
|
|
867
|
+
await saveManifest(ctx);
|
|
868
|
+
ctx.log(` ✗ ${e.file} (page deleted)`);
|
|
869
|
+
}
|
|
870
|
+
else {
|
|
871
|
+
const err = await errorOf(res);
|
|
872
|
+
ctx.warn(err.code === 'source_stale'
|
|
873
|
+
? ` ! ${e.file}: the page was edited in the editor since your last pull — not deleted. Pull, then decide.`
|
|
874
|
+
: ` ! ${e.file}: the page was not deleted (${refusal(err)})`);
|
|
875
|
+
ok = false;
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
return ok;
|
|
363
879
|
}
|
|
880
|
+
/**
|
|
881
|
+
* The ONE gate every `--prune` passes, whatever it deletes: the directory must exist (a checkout
|
|
882
|
+
* with none makes everything look deleted), the list is printed BEFORE any DELETE, and off a
|
|
883
|
+
* terminal `--yes` is required. 'stop' = declined or a dry run (not a failure), 'fail' = refused.
|
|
884
|
+
*/
|
|
885
|
+
export const gatePrune = (ctx, o) => async (dir, what, lines) => {
|
|
886
|
+
const there = await stat(join(ctx.dir, dir)).then((s) => s.isDirectory(), () => false);
|
|
887
|
+
if (!there) {
|
|
888
|
+
ctx.warn(` ! ${dir}/ is not here, so everything in it would look deleted — nothing deleted.`);
|
|
889
|
+
return 'fail';
|
|
890
|
+
}
|
|
891
|
+
ctx.log(`--prune ${o.dryRun ? 'would delete' : 'deletes'} ${what} from the platform:`);
|
|
892
|
+
for (const l of lines)
|
|
893
|
+
ctx.log(l);
|
|
894
|
+
if (o.dryRun)
|
|
895
|
+
return 'stop';
|
|
896
|
+
if (!o.yes) {
|
|
897
|
+
if (!o.confirm) {
|
|
898
|
+
ctx.warn(' ! off a terminal --prune also needs --yes; nothing deleted.');
|
|
899
|
+
return 'fail';
|
|
900
|
+
}
|
|
901
|
+
if (!(await o.confirm(`Delete ${what} from the platform? [y/N] `))) {
|
|
902
|
+
ctx.log('Nothing deleted.');
|
|
903
|
+
return 'stop';
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
return 'delete';
|
|
907
|
+
};
|
|
364
908
|
export const PUBLISH_WHAT = 'Which pages? Name their files (sb site publish design/pages/index.page.tsx) or pass --all. ' +
|
|
365
909
|
'Publishing makes them live; nothing is published by default.';
|
|
366
910
|
/**
|