@tideworthy/tidysites 0.1.17 → 0.1.19

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/bin/tidysites.mjs CHANGED
@@ -133,11 +133,12 @@ function describeError(e) {
133
133
  return msg;
134
134
  }
135
135
 
136
- async function tool(cfg, name, args) {
136
+ async function tool(cfg, name, args, { throws = false } = {}) {
137
137
  try {
138
138
  return await callTool(cfg, name, args);
139
139
  } catch (e) {
140
- if (e instanceof ApiError) fail(describeError(e));
140
+ // A caller that can recover (a menu to create first) takes the error; every other call ends the command with it.
141
+ if (e instanceof ApiError && !throws) fail(describeError(e));
141
142
  throw e;
142
143
  }
143
144
  }
@@ -367,24 +368,27 @@ async function push(ctx) {
367
368
  }
368
369
  try {
369
370
  try {
370
- await tool(ctx.cfg, 'call_website_site_tool', { site_id: ctx.cfg.site, tool: 'menu_set_items', arguments: { name, items } });
371
+ await tool(ctx.cfg, 'call_website_site_tool', { site_id: ctx.cfg.site, tool: 'menu_set_items', arguments: { name, items } }, { throws: true });
371
372
  } catch (e) {
372
373
  // A menu the website does not have yet (a phone drawer, a footer column) is created from the file, then filled.
373
374
  if (!/not.?found/i.test(e.message ?? '')) throw e;
374
- await tool(ctx.cfg, 'call_website_site_tool', { site_id: ctx.cfg.site, tool: 'menu_create', arguments: { name, label: m.label || name } });
375
- await tool(ctx.cfg, 'call_website_site_tool', { site_id: ctx.cfg.site, tool: 'menu_set_items', arguments: { name, items } });
375
+ if (!/^[a-z][a-z0-9_]*$/.test(name)) throw new Error(`a new menu's name is lowercase letters, digits and underscores (${name} has something else)`);
376
+ await tool(ctx.cfg, 'call_website_site_tool', { site_id: ctx.cfg.site, tool: 'menu_create', arguments: { name, label: m.label || name } }, { throws: true });
377
+ await tool(ctx.cfg, 'call_website_site_tool', { site_id: ctx.cfg.site, tool: 'menu_set_items', arguments: { name, items } }, { throws: true });
376
378
  say(`Menu ${name}: created.`);
377
379
  }
378
380
  say(`Menu ${name}: ${items.length} item${items.length === 1 ? '' : 's'} written.`);
379
381
  } catch (e) {
380
- menuProblems.push(`menus/${name}.json: ${e.message}`);
382
+ menuProblems.push(`menus/${name}.json: ${e instanceof ApiError ? describeError(e) : e.message}`);
381
383
  }
382
384
  }
385
+ // Refresh what went (the entries, and the menus that were written) before any menu failure is reported, so the snapshot
386
+ // knows the website's new versions and the next push is not refused as "changed on the website".
387
+ const written = menus.filter((n) => !menuProblems.some((p) => p.startsWith(`menus/${n}.json`)));
388
+ const pushed = new Set([...keys, ...written.map((m) => `menus/${m}`)]);
389
+ if (touched.length) await pull(ctx, touched, { overwrite: pushed, menus: written.length > 0 });
390
+ else if (written.length) await pullMenus(ctx);
383
391
  if (menuProblems.length) fail(menuProblems.join('\n'));
384
- // Refresh only what went, and take the website's version of exactly those entries; every other file stays as it is.
385
- const pushed = new Set([...keys, ...menus.map((m) => `menus/${m}`)]);
386
- if (touched.length) await pull(ctx, touched, { overwrite: pushed, menus: menus.length > 0 });
387
- else if (menus.length) await pullMenus(ctx);
388
392
  }
389
393
 
390
394
  const MEDIA_TYPES = { png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif', webp: 'image/webp', avif: 'image/avif', svg: 'image/svg+xml', ico: 'image/x-icon', mp4: 'video/mp4', m4v: 'video/mp4', webm: 'video/webm', mov: 'video/quicktime', woff2: 'font/woff2', woff: 'font/woff', ttf: 'font/ttf', otf: 'font/otf', pdf: 'application/pdf' };
package/lib/folder.mjs CHANGED
@@ -145,9 +145,20 @@ export function pushDocument(local, snapshot, { keys, removeKeys = [], settings
145
145
  // so the names the snapshot had and the file no longer has go as null.
146
146
  const data = { ...e.data };
147
147
  if (was?.data) for (const k of Object.keys(was.data)) if (!(k in data) && !emptyValue(was.data[k])) data[k] = null;
148
- (content[collection] ??= []).push({ id: e.id, slug, status: e.status, data });
148
+ // A new entry's seed key is its slug, so "$ref:<slug>" from another file resolves to it once it is created.
149
+ (content[collection] ??= []).push({ id: e.id || slug, slug, status: e.status, data });
149
150
  if (was?.updated_at) expect[key] = was.updated_at;
150
151
  }
152
+ // "$ref:<slug>" (or "$ref:<collection>/<slug>") to an entry the folder already knows the website's id for becomes that id; a reference to an entry created by this same push stays for the website to resolve.
153
+ const ids = new Map();
154
+ for (const [k, e] of Object.entries(local)) if (e?.id) { const [, slug] = splitKey(k); ids.set(slug, e.id); ids.set(k, e.id); }
155
+ const resolveRefs = (v) => {
156
+ if (typeof v === 'string' && v.startsWith('$ref:')) { const target = v.slice(5).trim(); const id = ids.get(target) ?? ids.get(target.split('/').pop()); return id ?? `$ref:${target.split('/').pop()}`; }
157
+ if (Array.isArray(v)) return v.map(resolveRefs);
158
+ if (v && typeof v === 'object') return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, resolveRefs(x)]));
159
+ return v;
160
+ };
161
+ for (const entries of Object.values(content)) for (const e of entries) e.data = resolveRefs(e.data);
151
162
  const remove = {};
152
163
  for (const key of removeKeys) {
153
164
  const [collection, slug] = splitKey(key);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tideworthy/tidysites",
3
- "version": "0.1.17",
3
+ "version": "0.1.19",
4
4
  "description": "Pull a Tidysites website to files, edit, validate against its content model, push it back, screenshot the result.",
5
5
  "type": "module",
6
6
  "bin": {