@unbase-app/layer 0.0.0-stage → 2.0.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/LICENSE +21 -0
- package/README.md +400 -2
- package/bin/unbase-layer.mjs +28 -0
- package/cli/artifact-style.mjs +89 -0
- package/cli/fetch.mjs +142 -0
- package/cli/lib.mjs +160 -0
- package/cli/live.mjs +35 -0
- package/cli/overview-artifact.mjs +142 -0
- package/cli/plan-artifact.mjs +337 -0
- package/cli/post.mjs +309 -0
- package/cli/redispatch.mjs +99 -0
- package/cli/screenshot.mjs +189 -0
- package/cli/status.mjs +50 -0
- package/cli/store.mjs +143 -0
- package/dist/client/start.d.ts +4 -0
- package/dist/client/start.js +12888 -0
- package/package.json +119 -4
- package/skills/build-comment/SKILL.md +61 -0
- package/skills/get-comments/SKILL.md +127 -0
- package/skills/process-comments/SKILL.md +115 -0
- package/skills/propose-comment/SKILL.md +79 -0
- package/src/astro/component-marker.mjs +161 -0
- package/src/astro/content-map-extra.mjs +192 -0
- package/src/astro/content-map.mjs +584 -0
- package/src/astro/deploy-context.mjs +66 -0
- package/src/astro/index.d.ts +97 -0
- package/src/astro/index.mjs +277 -0
- package/src/astro/pages.mjs +192 -0
- package/src/astro/version.mjs +13 -0
- package/src/edit/index.d.mts +178 -0
- package/src/edit/index.mjs +1002 -0
- package/src/page/index.d.mts +71 -0
- package/src/page/index.mjs +216 -0
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Builds the plan page of /get-comments from the working directory of a round.
|
|
4
|
+
//
|
|
5
|
+
// npx unbase-layer plan-artifact --dir reviews/comments/2026-10-07
|
|
6
|
+
//
|
|
7
|
+
// Reads plan.json and comments.json, embeds the screenshots (png) as downscaled
|
|
8
|
+
// webp and writes <dir>/plan.html. The skill publishes it with the Artifact
|
|
9
|
+
// tool, with capabilities { db: {}, user: {} }: the choices go into the
|
|
10
|
+
// collection "choices" (one document per comment number: { choice, note,
|
|
11
|
+
// extra, by, at }), where /process-comments reads them with ArtifactData.
|
|
12
|
+
//
|
|
13
|
+
// plan.json:
|
|
14
|
+
// {
|
|
15
|
+
// "date": "2026-10-07",
|
|
16
|
+
// "dir": "reviews/comments/2026-10-07",
|
|
17
|
+
// "header": { "title", "lead", "choices": ["#5 logo 32 or 36px", …] }, optional; "#nr" links to the card
|
|
18
|
+
// "packages": [ { "branch", "title", "route": "direct" | "concept", "comments": [8, 9], "after": [] } ],
|
|
19
|
+
// "waiting": [14], comments without a branch yet (waiting for an answer)
|
|
20
|
+
// "completed": [5, 6], wrapped up in this round
|
|
21
|
+
// "artifact": "<url after publishing>", so /process-comments finds the choices
|
|
22
|
+
// "comments": {
|
|
23
|
+
// "8": {
|
|
24
|
+
// "kind": "clear" | "variants" | "question" | "covered" | "nothing",
|
|
25
|
+
// "reading": "…", "plan": "…", "files": ["src/…"],
|
|
26
|
+
// "evidence": "…", "conflict": "…", "note": "…", "question": "…", all optional
|
|
27
|
+
// "before": { "desktop": "8-before-desktop.png", "mobile": "8-before-mobile.png" },
|
|
28
|
+
// "beforeHover": "8-before-hover.png", the before with --hover
|
|
29
|
+
// "variants": [ { "id": "A", "title": "…", "diff": "8-A.diff", "after": { "desktop": "…", "mobile": "…" } } ],
|
|
30
|
+
// "recommended": "A", variant id, or null
|
|
31
|
+
// "options": [[value, label], …], otherwise: clear → ok/skip, variants → A/B/C/skip
|
|
32
|
+
// "extra": { "name", "legend", "options": [[value, label], …], "recommended" }, a second choice
|
|
33
|
+
// "zoom": [x, y, w, h] crop for the variants (px of the desktop screenshot); without it text only
|
|
34
|
+
// }
|
|
35
|
+
// }
|
|
36
|
+
// }
|
|
37
|
+
// A text variant may be written as "part 1 | part 2" when the sentence has two
|
|
38
|
+
// tones: part 2 is set in grey.
|
|
39
|
+
// ============================================================================
|
|
40
|
+
|
|
41
|
+
import { readFileSync, writeFileSync, existsSync } from "node:fs";
|
|
42
|
+
import { join } from "node:path";
|
|
43
|
+
import { args } from "./lib.mjs";
|
|
44
|
+
import { BASE_CSS, HEAD, image, e, when } from "./artifact-style.mjs";
|
|
45
|
+
|
|
46
|
+
const o = args();
|
|
47
|
+
if (!o.dir) {
|
|
48
|
+
console.error("Usage: --dir reviews/comments/<date>");
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
const D = o.dir;
|
|
52
|
+
const plan = JSON.parse(readFileSync(join(D, "plan.json"), "utf8"));
|
|
53
|
+
const comments = new Map(JSON.parse(readFileSync(join(D, "comments.json"), "utf8")).comments.map((x) => [x.nr, x]));
|
|
54
|
+
const exists = (f) => f && existsSync(join(D, f));
|
|
55
|
+
|
|
56
|
+
const KIND = {
|
|
57
|
+
clear: ["Clear", "chip--ok"],
|
|
58
|
+
variants: ["Choose a variant", "chip--choose"],
|
|
59
|
+
question: ["Question", "chip--ask"],
|
|
60
|
+
covered: ["Covered by another", "chip--with"],
|
|
61
|
+
nothing: ["Nothing to do", "chip--with"],
|
|
62
|
+
};
|
|
63
|
+
const ROUTE = { direct: ["Direct", "A correction to an existing component or page"], concept: ["Concept", "First as a concept next to the current version"] };
|
|
64
|
+
|
|
65
|
+
function options(p) {
|
|
66
|
+
if (p.options) return p.options;
|
|
67
|
+
if (p.kind === "variants") return [...p.variants.map((v) => [v.id, `${v.id} · ${v.title.includes(" | ") ? sentence(...v.title.split(" | ")) : v.title}`]), ["skip", "Skip"]];
|
|
68
|
+
return [["ok", "Agreed"], ["skip", "Skip"]];
|
|
69
|
+
}
|
|
70
|
+
const recommended = (p) => p.recommended ?? (p.kind === "variants" || p.options ? null : "ok");
|
|
71
|
+
// "#5" → link to that card; not inside an HTML entity (').
|
|
72
|
+
const linkNrs = (t) => e(t).replace(/(?<!&)#(\d+)/g, '<a href="#comment-$1">#$1</a>');
|
|
73
|
+
/** "part 1 | part 2" of a text variant as one sentence: a space, except before punctuation. */
|
|
74
|
+
const sentence = (a, b) => (/^[,.:;!?]/.test(b) ? a + b : a + " " + b);
|
|
75
|
+
|
|
76
|
+
async function figure(file, label, mobile = false) {
|
|
77
|
+
if (!exists(file)) return "";
|
|
78
|
+
const src = await image(join(D, file), { width: mobile ? 390 : 1280 });
|
|
79
|
+
return `<figure class="shot${mobile ? " shot--mobile" : ""}"><button type="button" class="shot__button" aria-label="View ${e(label)} large"><img src="${src}" alt="${e(label)}" loading="lazy"></button><figcaption>${e(label)}</figcaption></figure>`;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function card(nr) {
|
|
83
|
+
const p = plan.comments[String(nr)];
|
|
84
|
+
const x = comments.get(nr);
|
|
85
|
+
const [kind, chip] = KIND[p.kind] ?? [p.kind, "chip--with"];
|
|
86
|
+
const comp = x.component.name || "section on the page";
|
|
87
|
+
const replies = x.replies
|
|
88
|
+
.map((r) => `<p class="reply"><strong>${e(r.author)}</strong> <span>${when(r.createdAt)}</span><br>${e(r.text)}</p>`)
|
|
89
|
+
.join("");
|
|
90
|
+
const rows = [["What I read", p.reading], ["What I'll do", p.plan]];
|
|
91
|
+
for (const [k, l] of [["evidence", "Why this reading"], ["conflict", "Conflicts with the design rules"], ["note", "Note"], ["question", "Open question"]]) if (p[k]) rows.push([l, p[k]]);
|
|
92
|
+
if (p.files?.length) rows.push(["Files", p.files.join(", ")]);
|
|
93
|
+
const dl = rows.map(([l, v]) => `<div class="row${/Conflicts|Open question/.test(l) ? " row--alert" : ""}"><dt>${e(l)}</dt><dd>${linkNrs(v)}</dd></div>`).join("");
|
|
94
|
+
|
|
95
|
+
const viewport = `${x.viewport.width}×${x.viewport.height}`;
|
|
96
|
+
const shots = (await figure(p.before?.desktop, `Before · desktop ${viewport}, with the pin`)) + (await figure(p.before?.mobile, "Before · mobile 390", true));
|
|
97
|
+
|
|
98
|
+
let variants = "";
|
|
99
|
+
if (p.variants?.length) {
|
|
100
|
+
const zoom = p.zoom ?? null;
|
|
101
|
+
const items = [];
|
|
102
|
+
const beforeZoom = zoom && (p.beforeHover ?? p.before?.desktop);
|
|
103
|
+
if (zoom && exists(beforeZoom)) items.push(`<div class="variant"><span class="variant__id">Now</span><img src="${await image(join(D, beforeZoom), { zoom, quality: 85 })}" alt="Now"></div>`);
|
|
104
|
+
for (const v of p.variants) {
|
|
105
|
+
const rec = v.id === p.recommended;
|
|
106
|
+
const img = zoom && exists(v.after?.desktop) ? `<img src="${await image(join(D, v.after.desktop), { zoom, quality: 85 })}" alt="Variant ${e(v.id)}">` : "";
|
|
107
|
+
const [a, b] = v.title.split(" | ");
|
|
108
|
+
const text = b !== undefined
|
|
109
|
+
? `<span class="statement">${e(a)}<span class="statement__rest">${e(/^[,.:;!?]/.test(b) ? b : " " + b)}</span></span>`
|
|
110
|
+
: `<span class="variant__title">${e(v.title)}</span>`;
|
|
111
|
+
items.push(`<div class="variant${rec ? " variant--recommended" : ""}"><span class="variant__id">${e(v.id)}${rec ? " · recommended" : ""}</span>${img}${text}</div>`);
|
|
112
|
+
}
|
|
113
|
+
variants = `<div class="variants${zoom ? " variants--image" : ""}">${items.join("")}</div>`;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const rec = recommended(p);
|
|
117
|
+
const radios = options(p)
|
|
118
|
+
.map(([w, l]) => `<label class="option"><input type="radio" name="c${nr}" value="${e(w)}" id="c${nr}-${e(w)}"><span>${e(l)}${w === rec ? " <em>recommended</em>" : ""}</span></label>`)
|
|
119
|
+
.join("");
|
|
120
|
+
const extra = p.extra
|
|
121
|
+
? `<fieldset class="choice__group"><legend>${e(p.extra.legend)}</legend>${p.extra.options.map(([w, l]) => `<label class="option"><input type="radio" name="x${nr}" value="${e(w)}" id="x${nr}-${e(w)}"><span>${e(l)}</span></label>`).join("")}</fieldset>`
|
|
122
|
+
: "";
|
|
123
|
+
|
|
124
|
+
return `
|
|
125
|
+
<article class="card" id="comment-${nr}" data-nr="${nr}" data-recommended="${e(rec ?? "")}" data-extra-recommended="${e(p.extra?.recommended ?? "")}">
|
|
126
|
+
<header class="card__head">
|
|
127
|
+
<span class="pin" aria-label="Comment ${nr}">${nr}</span>
|
|
128
|
+
<span class="card__where"><a href="${e(x.page.url ?? "#")}">${e(x.page.path ?? "")}</a> · <code>${e(comp)}</code>${x.author.team ? "" : ' · <strong class="outside">from outside the team</strong>'}</span>
|
|
129
|
+
<span class="chip ${chip}">${e(kind)}</span>
|
|
130
|
+
</header>
|
|
131
|
+
<blockquote class="quote"><p>${e(x.text)}</p><footer>${e(x.author.name)} · ${when(x.createdAt)}</footer></blockquote>
|
|
132
|
+
${replies ? `<div class="replies">${replies}</div>` : ""}
|
|
133
|
+
<dl class="explain">${dl}</dl>
|
|
134
|
+
<div class="shots">${shots}</div>
|
|
135
|
+
${variants}
|
|
136
|
+
<form class="choice" data-nr="${nr}" onsubmit="return false">
|
|
137
|
+
<fieldset class="choice__group"><legend>Your choice</legend>${radios}</fieldset>
|
|
138
|
+
${extra}
|
|
139
|
+
<label class="choice__note" for="n${nr}">Note <span>(optional)</span></label>
|
|
140
|
+
<textarea id="n${nr}" rows="1"></textarea>
|
|
141
|
+
<p class="choice__status" aria-live="polite"></p>
|
|
142
|
+
</form>
|
|
143
|
+
</article>`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const sections = [];
|
|
147
|
+
for (const [i, pk] of plan.packages.entries()) {
|
|
148
|
+
const [r, rt] = ROUTE[pk.route] ?? [pk.route, ""];
|
|
149
|
+
const cards = [];
|
|
150
|
+
for (const nr of pk.comments) cards.push(await card(nr));
|
|
151
|
+
sections.push(`
|
|
152
|
+
<section class="package" id="package-${i + 1}">
|
|
153
|
+
<header class="package__head">
|
|
154
|
+
<p class="package__nr">Package ${i + 1} of ${plan.packages.length}</p>
|
|
155
|
+
<h2>${e(pk.title)}</h2>
|
|
156
|
+
<p class="package__meta"><code>${e(pk.branch)}</code><span class="chip chip--route" title="${e(rt)}">${e(r)}</span><span>${pk.comments.map((n) => `#${n}`).join(", ")}</span>${pk.after?.length ? `<span>after package ${e(pk.after.join(", "))}</span>` : ""}</p>
|
|
157
|
+
</header>
|
|
158
|
+
${cards.join("")}
|
|
159
|
+
</section>`);
|
|
160
|
+
}
|
|
161
|
+
if (plan.waiting?.length) {
|
|
162
|
+
const cards = [];
|
|
163
|
+
for (const nr of plan.waiting) cards.push(await card(nr));
|
|
164
|
+
sections.push(`
|
|
165
|
+
<section class="package" id="waiting">
|
|
166
|
+
<header class="package__head"><p class="package__nr">No branch yet</p><h2>Waiting for an answer</h2></header>
|
|
167
|
+
${cards.join("")}
|
|
168
|
+
</section>`);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const all = Object.keys(plan.comments).map(Number).sort((a, b) => a - b);
|
|
172
|
+
const toChoose = all.filter((n) => ["variants", "question"].includes(plan.comments[String(n)].kind));
|
|
173
|
+
const header = plan.header ?? {};
|
|
174
|
+
const title = header.title ?? `Comment round ${plan.date}`;
|
|
175
|
+
|
|
176
|
+
const CSS = `${BASE_CSS}
|
|
177
|
+
.replies { display: grid; gap: 8px; }
|
|
178
|
+
.reply { margin: 0; font-size: 14px; padding: 10px 12px; border-radius: 8px; background: var(--fill); }
|
|
179
|
+
.reply strong { color: var(--ink); } .reply span { color: var(--quiet); font-size: 13px; }
|
|
180
|
+
.outside { color: var(--ask); }
|
|
181
|
+
.explain { margin: 0; display: grid; gap: 10px; }
|
|
182
|
+
.row { display: grid; grid-template-columns: 170px minmax(0, 1fr); gap: 4px 16px; }
|
|
183
|
+
.row dt { font-weight: 600; color: var(--ink); font-size: 14px; }
|
|
184
|
+
.row dd { margin: 0; min-width: 0; }
|
|
185
|
+
.row--alert dt { color: var(--red); }
|
|
186
|
+
.shots { display: grid; grid-template-columns: minmax(0, 1fr) 132px; gap: 12px; align-items: start; }
|
|
187
|
+
.shot { margin: 0; display: grid; gap: 6px; min-width: 0; }
|
|
188
|
+
.shot figcaption { font-size: 12px; color: var(--quiet); }
|
|
189
|
+
.shot__button { padding: 0; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--fill); cursor: zoom-in; display: block; }
|
|
190
|
+
.shot__button:focus-visible { outline: 2px solid var(--red); outline-offset: 2px; }
|
|
191
|
+
.shot img { display: block; width: 100%; height: auto; }
|
|
192
|
+
.variants { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
|
|
193
|
+
.variants--image { grid-template-columns: 1fr; }
|
|
194
|
+
.variant { border: 1px solid var(--line); border-radius: 10px; padding: 12px; display: grid; gap: 8px; min-width: 0; }
|
|
195
|
+
.variant--recommended { border-color: var(--red); }
|
|
196
|
+
.variant__id { font: 600 12px/1 var(--body); letter-spacing: .06em; text-transform: uppercase; color: var(--quiet); }
|
|
197
|
+
.variant--recommended .variant__id { color: var(--red); }
|
|
198
|
+
.variant img { display: block; width: 100%; height: auto; border-radius: 6px; }
|
|
199
|
+
.variant__title { color: var(--ink); font-weight: 500; }
|
|
200
|
+
.statement { font: 600 18px/1.3 var(--heading); color: var(--ink); }
|
|
201
|
+
.statement__rest { color: var(--quiet); }
|
|
202
|
+
.panel__actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
|
|
203
|
+
.line { font: 13px/1.5 var(--mono); background: var(--fill); border-radius: 8px; padding: 10px 12px; color: var(--ink); overflow-wrap: anywhere; min-width: 0; }
|
|
204
|
+
.choice { border-top: 1px solid var(--line); padding-top: 14px; display: grid; gap: 10px; }
|
|
205
|
+
.choice__group { border: 0; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
|
|
206
|
+
.choice__group legend { font-weight: 600; color: var(--ink); font-size: 14px; margin-bottom: 8px; width: 100%; }
|
|
207
|
+
.option { position: relative; }
|
|
208
|
+
.option input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
|
|
209
|
+
.option span { display: inline-block; padding: 8px 14px; border: 1px solid var(--line); border-radius: 999px; font-size: 14px; color: var(--ink); cursor: pointer; }
|
|
210
|
+
.option input:focus-visible + span { outline: 2px solid var(--red); outline-offset: 2px; }
|
|
211
|
+
.option em { font-style: normal; font-size: 12px; color: var(--red); margin-left: 4px; }
|
|
212
|
+
.option input:checked + span { background: var(--ink); border-color: var(--ink); color: var(--card); }
|
|
213
|
+
.option input:checked + span em { color: inherit; opacity: .8; }
|
|
214
|
+
.choice__note { font-weight: 600; color: var(--ink); font-size: 14px; }
|
|
215
|
+
.choice__note span { font-weight: 400; color: var(--quiet); }
|
|
216
|
+
.choice textarea { font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; resize: vertical; width: 100%; }
|
|
217
|
+
.choice__status { margin: 0; font-size: 13px; color: var(--quiet); min-height: 1.2em; }
|
|
218
|
+
dialog { border: 0; padding: 0; background: transparent; max-width: min(96vw, 1400px); }
|
|
219
|
+
dialog::backdrop { background: rgba(15,27,36,.75); }
|
|
220
|
+
dialog img { display: block; max-width: 100%; max-height: 90vh; border-radius: 8px; }
|
|
221
|
+
dialog button { position: absolute; top: 8px; right: 8px; }
|
|
222
|
+
@media (max-width: 640px) { .row { grid-template-columns: 1fr; } .shots { grid-template-columns: minmax(0, 1fr) 96px; } }
|
|
223
|
+
@media (prefers-reduced-motion: no-preference) { .option span { transition: background-color .15s, color .15s; } }
|
|
224
|
+
`;
|
|
225
|
+
|
|
226
|
+
const SCRIPT = `
|
|
227
|
+
(() => {
|
|
228
|
+
const NRS = ${JSON.stringify(all)};
|
|
229
|
+
const state = {};
|
|
230
|
+
let db = null, uid = null;
|
|
231
|
+
const explain = (t) => (document.getElementById("choices-explain").textContent = t);
|
|
232
|
+
const line = () => {
|
|
233
|
+
const parts = NRS.filter((n) => state[n]?.choice).map((n) => n + " " + state[n].choice + (state[n].extra ? "/" + state[n].extra : ""));
|
|
234
|
+
document.getElementById("line").textContent = parts.length ? parts.join(" · ") : "No choices yet";
|
|
235
|
+
const open = NRS.filter((n) => !state[n]?.choice).length;
|
|
236
|
+
document.getElementById("count").textContent = open ? open + " still open" : "All chosen";
|
|
237
|
+
};
|
|
238
|
+
const show = (n) => {
|
|
239
|
+
const s = state[n] || {}, form = document.querySelector('form.choice[data-nr="' + n + '"]');
|
|
240
|
+
if (!form) return;
|
|
241
|
+
form.querySelectorAll('input[name="c' + n + '"]').forEach((i) => (i.checked = i.value === s.choice));
|
|
242
|
+
form.querySelectorAll('input[name="x' + n + '"]').forEach((i) => (i.checked = i.value === s.extra));
|
|
243
|
+
const ta = form.querySelector("textarea");
|
|
244
|
+
if (document.activeElement !== ta) ta.value = s.note || "";
|
|
245
|
+
};
|
|
246
|
+
const status = (n, t) => { const el = document.querySelector('form.choice[data-nr="' + n + '"] .choice__status'); if (el) el.textContent = t; };
|
|
247
|
+
const busy = {};
|
|
248
|
+
async function save(n) {
|
|
249
|
+
line();
|
|
250
|
+
if (!db) { status(n, "Only in this view; copy the line at the top."); return; }
|
|
251
|
+
if (busy[n]) { busy[n].again = true; return; }
|
|
252
|
+
busy[n] = { again: false };
|
|
253
|
+
try {
|
|
254
|
+
do { busy[n].again = false; await db.collection("choices").doc(String(n)).set({ ...state[n], by: uid, at: new Date().toISOString() }); } while (busy[n].again);
|
|
255
|
+
status(n, "Saved");
|
|
256
|
+
} catch { status(n, "Saving failed; copy the line at the top."); }
|
|
257
|
+
finally { delete busy[n]; }
|
|
258
|
+
}
|
|
259
|
+
document.querySelectorAll("form.choice").forEach((form) => {
|
|
260
|
+
const n = Number(form.dataset.nr);
|
|
261
|
+
form.addEventListener("change", (ev) => {
|
|
262
|
+
const t = ev.target;
|
|
263
|
+
if (t.name === "c" + n) state[n] = { ...(state[n] || {}), choice: t.value };
|
|
264
|
+
else if (t.name === "x" + n) state[n] = { ...(state[n] || {}), extra: t.value };
|
|
265
|
+
else return;
|
|
266
|
+
save(n);
|
|
267
|
+
});
|
|
268
|
+
const ta = form.querySelector("textarea");
|
|
269
|
+
ta.addEventListener("change", () => { state[n] = { ...(state[n] || {}), note: ta.value.trim() }; save(n); });
|
|
270
|
+
});
|
|
271
|
+
document.getElementById("all-recommended").addEventListener("click", async () => {
|
|
272
|
+
for (const k of document.querySelectorAll(".card[data-recommended]")) {
|
|
273
|
+
const n = Number(k.dataset.nr), rec = k.dataset.recommended, xr = k.dataset.extraRecommended;
|
|
274
|
+
if (!rec || state[n]?.choice) continue;
|
|
275
|
+
state[n] = { ...(state[n] || {}), choice: rec, ...(xr && !state[n]?.extra ? { extra: xr } : {}) };
|
|
276
|
+
show(n); await save(n);
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
document.getElementById("copy").addEventListener("click", async (ev) => {
|
|
280
|
+
const el = document.getElementById("line");
|
|
281
|
+
try { await navigator.clipboard.writeText(el.textContent); ev.target.textContent = "Copied"; }
|
|
282
|
+
catch { const r = document.createRange(); r.selectNodeContents(el); const s = getSelection(); s.removeAllRanges(); s.addRange(r); }
|
|
283
|
+
});
|
|
284
|
+
const dlg = document.getElementById("large");
|
|
285
|
+
document.querySelectorAll(".shot__button").forEach((k) => k.addEventListener("click", () => {
|
|
286
|
+
const i = k.querySelector("img"); dlg.querySelector("img").src = i.src; dlg.querySelector("img").alt = i.alt; dlg.showModal();
|
|
287
|
+
}));
|
|
288
|
+
document.getElementById("large-close").addEventListener("click", () => dlg.close());
|
|
289
|
+
dlg.addEventListener("click", (ev) => ev.target === dlg && dlg.close());
|
|
290
|
+
line();
|
|
291
|
+
(async () => {
|
|
292
|
+
const c = window.claude;
|
|
293
|
+
if (!c?.use) return explain("Saving isn't possible here; copy the line and paste it in the chat.");
|
|
294
|
+
const [d, u] = await Promise.all([c.use("db"), c.use("user")]);
|
|
295
|
+
if (!d) return explain("Saving isn't possible here; copy the line and paste it in the chat.");
|
|
296
|
+
db = d; uid = u ? await u.id() : null;
|
|
297
|
+
db.collection("choices").onSnapshot((snap) => {
|
|
298
|
+
for (const doc of snap.docs) {
|
|
299
|
+
const n = Number(doc.id), b = doc.data() || {};
|
|
300
|
+
if (busy[n]) continue;
|
|
301
|
+
state[n] = { choice: b.choice, note: b.note, extra: b.extra };
|
|
302
|
+
show(n);
|
|
303
|
+
}
|
|
304
|
+
line();
|
|
305
|
+
}, () => explain("The choices could not load; copy the line when you choose."));
|
|
306
|
+
})();
|
|
307
|
+
})();`;
|
|
308
|
+
|
|
309
|
+
const html = `<title>${e(title)}</title>
|
|
310
|
+
${HEAD}
|
|
311
|
+
<style>
|
|
312
|
+
/* Layout: one reading column; a section per package, a card per comment with
|
|
313
|
+
the quote, the reading, before images, variants and the choice. */
|
|
314
|
+
${CSS}</style>
|
|
315
|
+
<main>
|
|
316
|
+
<header class="head">
|
|
317
|
+
<p class="eyebrow">Comment mode · /get-comments</p>
|
|
318
|
+
<h1>${e(title)}</h1>
|
|
319
|
+
${header.lead ? `<p>${linkNrs(header.lead)}</p>` : ""}
|
|
320
|
+
<p class="counts"><span><strong>${all.length}</strong> comments</span><span><strong>${plan.packages.length}</strong> packages</span><span><strong>${toChoose.length}</strong> need a choice</span><span><strong>${plan.waiting?.length ?? 0}</strong> waiting</span>${plan.completed?.length ? `<span><strong>${plan.completed.length}</strong> completed</span>` : ""}</p>
|
|
321
|
+
</header>
|
|
322
|
+
<section class="panel" aria-labelledby="choices-head">
|
|
323
|
+
<h2 id="choices-head">Your choices</h2>
|
|
324
|
+
<p class="quiet" id="choices-explain">Your choices are saved as soon as you click them.</p>
|
|
325
|
+
${header.choices?.length ? `<ul>${header.choices.map((k) => `<li>${linkNrs(k)}</li>`).join("")}</ul>` : ""}
|
|
326
|
+
<div class="panel__actions"><button type="button" class="button" id="all-recommended">Take the recommended choices</button><span class="quiet" id="count"></span></div>
|
|
327
|
+
<div class="line" id="line" aria-live="polite">No choices yet</div>
|
|
328
|
+
<div class="panel__actions"><button type="button" class="button button--quiet" id="copy">Copy this line</button><span class="quiet">Paste it in the chat if saving doesn't work here.</span></div>
|
|
329
|
+
</section>
|
|
330
|
+
${sections.join("")}
|
|
331
|
+
</main>
|
|
332
|
+
<dialog id="large"><img alt=""><button type="button" class="button" id="large-close">Close</button></dialog>
|
|
333
|
+
<script>${SCRIPT}</script>
|
|
334
|
+
`;
|
|
335
|
+
const out = join(D, "plan.html");
|
|
336
|
+
writeFileSync(out, html);
|
|
337
|
+
console.log(`${out} · ${Math.round(html.length / 1024)} KB`);
|
package/cli/post.mjs
ADDED
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// Posts Operator replies under pins, with before/after images.
|
|
4
|
+
//
|
|
5
|
+
// npx unbase-layer post --json replies.json --dry shows what would happen
|
|
6
|
+
// npx unbase-layer post --json replies.json writes (production!)
|
|
7
|
+
// npx unbase-layer post --check can we post at all?
|
|
8
|
+
// npx unbase-layer post --proposal work/proposal.json --only-nr 8 --team-only [--dry]
|
|
9
|
+
//
|
|
10
|
+
// replies.json is one reply or a list:
|
|
11
|
+
// {
|
|
12
|
+
// "pin": 8, the #number of the comment
|
|
13
|
+
// "text": "Changed: …", what goes under the pin
|
|
14
|
+
// "teamOnly": false, true: only the team sees it (PR link)
|
|
15
|
+
// "resolve": true, optional: mark the pin as resolved
|
|
16
|
+
// "images": [ optional: before/after per screen
|
|
17
|
+
// { "label": "Desktop · 2560", "before": "before.png", "after": "after.png" },
|
|
18
|
+
// { "label": "Mobile · 390", "before": "before-m.png", "after": "after-m.png" }
|
|
19
|
+
// ]
|
|
20
|
+
// }
|
|
21
|
+
//
|
|
22
|
+
// --proposal is the proposal of the propose-comment skill (one comment in the
|
|
23
|
+
// shape of plan.json of /get-comments, plus "nr"). It becomes one reply that
|
|
24
|
+
// starts with "Proposal:" (or "Question:"), with the attachment kind
|
|
25
|
+
// "variants": now and every variant per screen, a crop around the pin
|
|
26
|
+
// (from "zoom"), and the choice buttons. This is also the validator of the CI
|
|
27
|
+
// workflow: it refuses a proposal for another pin than --only-nr, a reply
|
|
28
|
+
// that is not --team-only, a resolve, file names outside the proposal's
|
|
29
|
+
// folder, and anything over the limits below — before anything is uploaded.
|
|
30
|
+
//
|
|
31
|
+
// Locally this goes through the Supabase CLI; with CM_OPERATOR_KEY set (CI)
|
|
32
|
+
// through the Edge Function operator-api, which checks the same again (store.mjs).
|
|
33
|
+
//
|
|
34
|
+
// Images go to the "comment-images" bucket as webp, at "<project id>/<id of the
|
|
35
|
+
// new reply>/<file>", so they have the same visibility as that reply (the
|
|
36
|
+
// bucket's policies in the unbase migrations).
|
|
37
|
+
// Desktop is at most 1600 wide; before and after get exactly the same size,
|
|
38
|
+
// or the slider does not line up.
|
|
39
|
+
//
|
|
40
|
+
// Text conventions (`fetch` reads them back): a question starts with
|
|
41
|
+
// "Question:", a proposal with "Proposal:", the PR notice names the PR link,
|
|
42
|
+
// the wrap-up starts with "Changed". The Dutch forms ("Vraag:", "Voorstel:",
|
|
43
|
+
// "Aangepast") are recognised too; `language` in comment-modus.json picks them.
|
|
44
|
+
// ============================================================================
|
|
45
|
+
|
|
46
|
+
import { readFileSync, mkdtempSync, rmSync, existsSync } from "node:fs";
|
|
47
|
+
import { tmpdir } from "node:os";
|
|
48
|
+
import { basename, dirname, join } from "node:path";
|
|
49
|
+
import { randomUUID } from "node:crypto";
|
|
50
|
+
import sharp from "sharp";
|
|
51
|
+
import { args, LANGUAGE } from "./lib.mjs";
|
|
52
|
+
import { insertReply, knownProjectId, operatorProfile, pinsByNr, uploadImage, VIA_OPERATOR } from "./store.mjs";
|
|
53
|
+
|
|
54
|
+
/** Limits of a proposal; operator-api enforces the same. */
|
|
55
|
+
const LIMITS = { text: 4000, variants: 4, screens: 2, title: 200, diff: 20000 };
|
|
56
|
+
const KINDS = ["clear", "variants", "question", "covered", "nothing"];
|
|
57
|
+
|
|
58
|
+
const WORDS = {
|
|
59
|
+
en: {
|
|
60
|
+
proposal: "Proposal:", question: "Question:", note: "Note:", conflict: "Against the design rules:",
|
|
61
|
+
chooseBelow: "Choose below, or reply in your own words.", okBelow: "Agreed? Choose below, or reply in your own words.",
|
|
62
|
+
nothingToDo: "Nothing to build.", skip: "Skip", desktop: "Desktop", mobile: "Mobile",
|
|
63
|
+
},
|
|
64
|
+
nl: {
|
|
65
|
+
proposal: "Voorstel:", question: "Vraag:", note: "Let op:", conflict: "Botst met de huisstijl:",
|
|
66
|
+
chooseBelow: "Kies hieronder, of antwoord in je eigen woorden.", okBelow: "Akkoord? Kies hieronder, of antwoord in je eigen woorden.",
|
|
67
|
+
nothingToDo: "Er hoeft niets gebouwd te worden.", skip: "Overslaan", desktop: "Desktop", mobile: "Mobiel",
|
|
68
|
+
},
|
|
69
|
+
}[LANGUAGE];
|
|
70
|
+
|
|
71
|
+
const o = args();
|
|
72
|
+
if (!o.json && !o.check && !o.proposal) {
|
|
73
|
+
console.error("Usage: --json replies.json [--dry] | --check | --proposal proposal.json --only-nr <n> --team-only [--dry]");
|
|
74
|
+
process.exit(1);
|
|
75
|
+
}
|
|
76
|
+
const dry = Boolean(o.dry);
|
|
77
|
+
const fail = (m) => {
|
|
78
|
+
console.error(`Refused: ${m}`);
|
|
79
|
+
process.exit(1);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const operator = await operatorProfile();
|
|
83
|
+
if (!operator) fail("There is no Operator account for this project (projects.operator_account_id). See the Unbase project settings.");
|
|
84
|
+
if (o.check) {
|
|
85
|
+
console.log(`Ready to post as ${operator.name} (${operator.id})${VIA_OPERATOR ? " through operator-api" : ""}.`);
|
|
86
|
+
process.exit(0);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const tmp = mkdtempSync(join(tmpdir(), "cm-image-"));
|
|
90
|
+
|
|
91
|
+
async function toWebp(source, target, size) {
|
|
92
|
+
let image = sharp(source);
|
|
93
|
+
const meta = await image.metadata();
|
|
94
|
+
const width = size?.width ?? Math.min(meta.width, meta.width > 1000 ? 1600 : meta.width);
|
|
95
|
+
const height = size?.height ?? Math.round((meta.height * width) / meta.width);
|
|
96
|
+
image = image.resize(width, height, { fit: "fill" }).webp({ quality: 80 });
|
|
97
|
+
await image.toFile(target);
|
|
98
|
+
return { width, height };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A crop of [x, y, w, h] (px of the source), at most 640 wide. */
|
|
102
|
+
async function crop(source, target, [x, y, w, h]) {
|
|
103
|
+
const meta = await sharp(source).metadata();
|
|
104
|
+
const left = Math.max(0, Math.min(Math.round(x), meta.width - 1));
|
|
105
|
+
const top = Math.max(0, Math.min(Math.round(y), meta.height - 1));
|
|
106
|
+
const width = Math.max(1, Math.min(Math.round(w), meta.width - left));
|
|
107
|
+
const height = Math.max(1, Math.min(Math.round(h), meta.height - top));
|
|
108
|
+
const out = Math.min(width, 640);
|
|
109
|
+
await sharp(source).extract({ left, top, width, height }).resize(out).webp({ quality: 82 }).toFile(target);
|
|
110
|
+
return { width: out, height: Math.round((height * out) / width) };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// ── a proposal → one reply ───────────────────────────────────────────────────
|
|
114
|
+
|
|
115
|
+
async function fromProposal(file) {
|
|
116
|
+
const dir = dirname(file);
|
|
117
|
+
const p = JSON.parse(readFileSync(file, "utf8"));
|
|
118
|
+
const nr = Number(o["only-nr"]);
|
|
119
|
+
if (!Number.isInteger(nr)) fail("--proposal needs --only-nr <n>: the one pin this run may post under.");
|
|
120
|
+
if (!o["team-only"]) fail("--proposal needs --team-only: a proposal is for the team.");
|
|
121
|
+
if (Number(p.nr) !== nr) fail(`the proposal is about #${p.nr}, this run only about #${nr}.`);
|
|
122
|
+
if (!KINDS.includes(p.kind)) fail(`unknown kind "${p.kind}".`);
|
|
123
|
+
if (p.resolve) fail("a proposal never resolves a pin.");
|
|
124
|
+
|
|
125
|
+
// Every file is a bare name in the proposal's own folder: nothing from elsewhere on the runner.
|
|
126
|
+
const local = (name) => {
|
|
127
|
+
if (!name) return null;
|
|
128
|
+
if (typeof name !== "string" || name !== basename(name) || name.startsWith(".")) fail(`"${name}" is not a file name in ${dir}.`);
|
|
129
|
+
const path = join(dir, name);
|
|
130
|
+
if (!existsSync(path)) fail(`${name} does not exist.`);
|
|
131
|
+
return path;
|
|
132
|
+
};
|
|
133
|
+
const variants = p.variants ?? [];
|
|
134
|
+
if (variants.length > LIMITS.variants) fail(`${variants.length} variants; at most ${LIMITS.variants}.`);
|
|
135
|
+
for (const v of variants) {
|
|
136
|
+
if (!/^[A-Z]$/.test(v.id ?? "")) fail(`variant id "${v.id}" is not one capital letter.`);
|
|
137
|
+
if (!v.title || String(v.title).length > LIMITS.title) fail(`variant ${v.id} has no title, or one over ${LIMITS.title} characters.`);
|
|
138
|
+
}
|
|
139
|
+
if (new Set(variants.map((v) => v.id)).size !== variants.length) fail("two variants with the same id.");
|
|
140
|
+
if (p.recommended && !variants.some((v) => v.id === p.recommended)) fail(`recommended "${p.recommended}" is not a variant.`);
|
|
141
|
+
if (p.zoom && !(Array.isArray(p.zoom) && p.zoom.length === 4 && p.zoom.every((n) => Number.isFinite(n) && n >= 0))) fail("zoom is not [x, y, w, h].");
|
|
142
|
+
|
|
143
|
+
// Text: the reading, the plan, and what to do with it. The first word is what fetch reads back.
|
|
144
|
+
const parts = [];
|
|
145
|
+
if (p.kind === "question") parts.push(`${WORDS.question} ${p.question ?? p.reading}`);
|
|
146
|
+
else parts.push(`${WORDS.proposal} ${p.reading}`);
|
|
147
|
+
if (p.plan && p.kind !== "covered" && p.kind !== "nothing") parts.push(p.plan);
|
|
148
|
+
if (p.conflict) parts.push(`${WORDS.conflict} ${p.conflict}`);
|
|
149
|
+
if (p.note) parts.push(`${WORDS.note} ${p.note}`);
|
|
150
|
+
if (p.kind === "variants" || (p.kind === "question" && variants.length)) parts.push(WORDS.chooseBelow);
|
|
151
|
+
else if (p.kind === "clear") parts.push(WORDS.okBelow);
|
|
152
|
+
else parts.push(WORDS.nothingToDo);
|
|
153
|
+
const text = parts.join("\n\n");
|
|
154
|
+
if (text.length > LIMITS.text) fail(`the text is ${text.length} characters; at most ${LIMITS.text}.`);
|
|
155
|
+
|
|
156
|
+
const id = randomUUID();
|
|
157
|
+
const uploads = [];
|
|
158
|
+
let attachment = null;
|
|
159
|
+
|
|
160
|
+
const shown = variants.filter((v) => v.after?.desktop || v.after?.mobile);
|
|
161
|
+
if (shown.length) {
|
|
162
|
+
const before = { desktop: local(p.before?.desktop), mobile: local(p.before?.mobile) };
|
|
163
|
+
const screens = [
|
|
164
|
+
["desktop", WORDS.desktop],
|
|
165
|
+
["mobile", WORDS.mobile],
|
|
166
|
+
].filter(([k]) => before[k]);
|
|
167
|
+
if (!screens.length) fail("variants with images need a before (before.desktop or before.mobile).");
|
|
168
|
+
|
|
169
|
+
// Before once per screen; every variant gets exactly its size.
|
|
170
|
+
const sizes = {};
|
|
171
|
+
for (const [k] of screens) {
|
|
172
|
+
sizes[k] = await toWebp(before[k], join(tmp, `nu-${k}.webp`));
|
|
173
|
+
uploads.push([join(tmp, `nu-${k}.webp`), `${id}/nu-${k}.webp`]);
|
|
174
|
+
}
|
|
175
|
+
let current = null;
|
|
176
|
+
if (p.zoom && before.desktop) {
|
|
177
|
+
current = { path: `${id}/nu-uitsnede.webp`, ...(await crop(before.desktop, join(tmp, "nu-uitsnede.webp"), p.zoom)) };
|
|
178
|
+
uploads.push([join(tmp, "nu-uitsnede.webp"), current.path]);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const built = [];
|
|
182
|
+
for (const v of variants) {
|
|
183
|
+
const images = [];
|
|
184
|
+
for (const [k, label] of screens) {
|
|
185
|
+
const after = local(v.after?.[k]);
|
|
186
|
+
if (!after) continue;
|
|
187
|
+
const name = `${v.id}-${k}.webp`;
|
|
188
|
+
await toWebp(after, join(tmp, name), sizes[k]);
|
|
189
|
+
uploads.push([join(tmp, name), `${id}/${name}`]);
|
|
190
|
+
images.push({ label, before: `${id}/nu-${k}.webp`, after: `${id}/${name}`, width: sizes[k].width, height: sizes[k].height });
|
|
191
|
+
}
|
|
192
|
+
if (images.length > LIMITS.screens) fail(`variant ${v.id} has more than ${LIMITS.screens} screens.`);
|
|
193
|
+
let cropped = null;
|
|
194
|
+
if (p.zoom && v.after?.desktop) {
|
|
195
|
+
cropped = { path: `${id}/${v.id}-uitsnede.webp`, ...(await crop(local(v.after.desktop), join(tmp, `${v.id}-uitsnede.webp`), p.zoom)) };
|
|
196
|
+
uploads.push([join(tmp, `${v.id}-uitsnede.webp`), cropped.path]);
|
|
197
|
+
}
|
|
198
|
+
// The diff goes along, so a build run applies exactly this variant.
|
|
199
|
+
const diffFile = v.diff ? local(v.diff) : null;
|
|
200
|
+
const diff = diffFile ? readFileSync(diffFile, "utf8") : null;
|
|
201
|
+
built.push({ id: v.id, title: String(v.title).replace(" | ", " · "), crop: cropped, images, ...(diff && diff.length <= LIMITS.diff ? { diff } : {}) });
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
attachment = {
|
|
205
|
+
kind: "variants",
|
|
206
|
+
recommended: p.recommended ?? null,
|
|
207
|
+
current,
|
|
208
|
+
variants: built,
|
|
209
|
+
choices: p.kind === "covered" || p.kind === "nothing" ? [] : [...built.map((v) => [v.id, `${v.id} · ${v.title}`]), ["skip", WORDS.skip]],
|
|
210
|
+
// Flat, for a viewer that only knows before/after: every variant per screen.
|
|
211
|
+
images: built.flatMap((v) => v.images.map((b) => ({ ...b, label: `${v.id} · ${b.label}` }))),
|
|
212
|
+
};
|
|
213
|
+
} else if (variants.length && p.kind !== "covered" && p.kind !== "nothing") {
|
|
214
|
+
// Text variants only: no images, still the buttons.
|
|
215
|
+
const built = variants.map((v) => ({ id: v.id, title: String(v.title).replace(" | ", " · "), crop: null, images: [] }));
|
|
216
|
+
attachment = {
|
|
217
|
+
kind: "variants", recommended: p.recommended ?? null, current: null, variants: built,
|
|
218
|
+
choices: [...built.map((v) => [v.id, `${v.id} · ${v.title}`]), ["skip", WORDS.skip]], images: [],
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return { id, pin: nr, text, teamOnly: true, attachment, uploads };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// ── replies.json → replies ───────────────────────────────────────────────────
|
|
226
|
+
|
|
227
|
+
async function fromReplies(r) {
|
|
228
|
+
const id = randomUUID();
|
|
229
|
+
const uploads = [];
|
|
230
|
+
// The attachment keeps the shape the layer reads (src/lib/supabase.ts,
|
|
231
|
+
// Attachment): { kind: "before-after", images: [{ label, before, after, width, height }] }.
|
|
232
|
+
let attachment = null;
|
|
233
|
+
if (r.images?.length) {
|
|
234
|
+
attachment = { kind: "before-after", images: [] };
|
|
235
|
+
for (const [i, b] of r.images.entries()) {
|
|
236
|
+
const name = `${i + 1}-${String(b.label).toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "")}`;
|
|
237
|
+
const afterFile = join(tmp, `${name}-after.webp`);
|
|
238
|
+
// The size of "before" leads; "after" gets exactly the same.
|
|
239
|
+
const size = b.before ? await toWebp(b.before, join(tmp, `${name}-before.webp`)) : null;
|
|
240
|
+
const afterSize = await toWebp(b.after, afterFile, size ?? undefined);
|
|
241
|
+
const item = { label: b.label, after: `${id}/${name}-after.webp`, width: afterSize.width, height: afterSize.height };
|
|
242
|
+
if (b.before) {
|
|
243
|
+
item.before = `${id}/${name}-before.webp`;
|
|
244
|
+
uploads.push([join(tmp, `${name}-before.webp`), item.before]);
|
|
245
|
+
}
|
|
246
|
+
uploads.push([afterFile, item.after]);
|
|
247
|
+
attachment.images.push(item);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return { id, pin: Number(r.pin), text: r.text, teamOnly: Boolean(r.teamOnly), resolve: Boolean(r.resolve), attachment, uploads };
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** A copy of the attachment with every image path (before, after, path)
|
|
254
|
+
* replaced by where it was stored: "<id>/<file>" → "<project id>/<id>/<file>". */
|
|
255
|
+
function withPaths(value, stored) {
|
|
256
|
+
if (Array.isArray(value)) return value.map((v) => withPaths(v, stored));
|
|
257
|
+
if (!value || typeof value !== "object") return value;
|
|
258
|
+
const out = {};
|
|
259
|
+
for (const [k, v] of Object.entries(value)) {
|
|
260
|
+
out[k] = (k === "before" || k === "after" || k === "path") && typeof v === "string" ? (stored.get(v) ?? v) : withPaths(v, stored);
|
|
261
|
+
}
|
|
262
|
+
return out;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
try {
|
|
266
|
+
let replies;
|
|
267
|
+
if (o.proposal) replies = [await fromProposal(o.proposal)];
|
|
268
|
+
else {
|
|
269
|
+
const input = JSON.parse(readFileSync(o.json, "utf8"));
|
|
270
|
+
replies = [];
|
|
271
|
+
for (const r of Array.isArray(input) ? input : [input]) replies.push(await fromReplies(r));
|
|
272
|
+
}
|
|
273
|
+
if (!replies.length) process.exit(0);
|
|
274
|
+
if (o["only-nr"] && replies.some((r) => r.pin !== Number(o["only-nr"]))) fail(`a reply under another pin than #${o["only-nr"]}.`);
|
|
275
|
+
if (o["team-only"] && replies.some((r) => !r.teamOnly)) fail("a reply that is not team-only.");
|
|
276
|
+
|
|
277
|
+
const pins = await pinsByNr([...new Set(replies.map((r) => r.pin))]);
|
|
278
|
+
for (const r of replies) {
|
|
279
|
+
const pin = pins.get(r.pin);
|
|
280
|
+
if (!pin) throw new Error(`Comment #${r.pin} does not exist (or is a reply)`);
|
|
281
|
+
if (o.proposal && pin.status !== "open") fail(`#${r.pin} is resolved; no proposal under it.`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
for (const r of replies) {
|
|
285
|
+
const pin = pins.get(r.pin);
|
|
286
|
+
const a = r.attachment;
|
|
287
|
+
const what =
|
|
288
|
+
`#${r.pin}${r.teamOnly ? " (team only)" : ""}${r.resolve ? " → resolved" : ""}` +
|
|
289
|
+
(a?.kind === "variants" ? ` + ${a.variants.length} variant(s), ${r.uploads.length} image(s)` : a ? ` + ${a.images.length} screen(s)` : "") +
|
|
290
|
+
`: ${r.text.slice(0, 80).replace(/\n/g, " ")}`;
|
|
291
|
+
if (dry) {
|
|
292
|
+
console.log(`--dry ${what}`);
|
|
293
|
+
if (o.proposal) {
|
|
294
|
+
// The paths as they would be stored, when the project's id is known here.
|
|
295
|
+
const project = knownProjectId();
|
|
296
|
+
const shown = project ? withPaths(a, new Map(r.uploads.map(([, path]) => [path, `${project}/${path}`]))) : a;
|
|
297
|
+
console.log(JSON.stringify({ text: r.text, attachment: shown }, null, 2));
|
|
298
|
+
}
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
// Upload first; the attachment then names every image where it was stored.
|
|
302
|
+
const stored = new Map();
|
|
303
|
+
for (const [file, path] of r.uploads) stored.set(path, await uploadImage(file, path));
|
|
304
|
+
await insertReply({ id: r.id, pin, operatorId: operator.id, text: r.text, teamOnly: r.teamOnly, attachment: withPaths(a, stored), resolve: r.resolve });
|
|
305
|
+
console.log(`Posted ${what}`);
|
|
306
|
+
}
|
|
307
|
+
} finally {
|
|
308
|
+
rmSync(tmp, { recursive: true, force: true });
|
|
309
|
+
}
|