zazz-ui 0.0.0 → 0.3.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/CHANGELOG.md +14 -0
- package/LICENSE +21 -0
- package/README.md +37 -2
- package/dist/add-uh698Oeq.js +129 -0
- package/dist/cli.js +44 -0
- package/dist/diff-DAn1ZKsj.js +197 -0
- package/dist/errors-DC2U2USS.js +33 -0
- package/dist/init-CDXpbPiY.js +3 -0
- package/dist/init-DsZP08nV.js +193 -0
- package/dist/update-C4m0n9xX.js +670 -0
- package/dist/update-CAg-JSg2.js +3 -0
- package/dist/wiring-BBm_hxrx.js +685 -0
- package/package.json +49 -11
|
@@ -0,0 +1,670 @@
|
|
|
1
|
+
import { t as ZazzError } from "./errors-DC2U2USS.js";
|
|
2
|
+
import { _ as resolveKit, a as renderIndexJs, c as createUi, f as baseFiles, g as kitSpec, h as loadFetchOptions, i as renderIndexCss, l as apply, m as primitiveFiles, o as sha256, p as baseScriptFiles, r as renderHead, t as appendJsImports, u as describe, v as loadConfig, y as filterCollisions } from "./wiring-BBm_hxrx.js";
|
|
3
|
+
import { t as parseVersionArg } from "./init-DsZP08nV.js";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
import { existsSync } from "node:fs";
|
|
6
|
+
import { readFile } from "node:fs/promises";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import { mergeDiff3 } from "node-diff3";
|
|
9
|
+
|
|
10
|
+
//#region src/changelog.ts
|
|
11
|
+
/**
|
|
12
|
+
* @fileoverview Slices the kit's CHANGELOG.md down to what a project cares
|
|
13
|
+
* about: versions in `(from, to]`, sections whose scope touches the vendored
|
|
14
|
+
* set. The changelog format is the kit's contract (see its intro): `## x.y.z`
|
|
15
|
+
* version headers, `### scope` sections named after a primitive or `base`,
|
|
16
|
+
* bullet lines, breaking entries flagged **BREAKING**.
|
|
17
|
+
*/
|
|
18
|
+
/**
|
|
19
|
+
* @description Renders the relevant changelog slice, or null when nothing in
|
|
20
|
+
* the range touches the given scopes. `scopes` should contain the vendored
|
|
21
|
+
* primitive names; `base` and the generic `primitives` bucket always match.
|
|
22
|
+
*/
|
|
23
|
+
function sliceChangelog(markdown, range, scopes) {
|
|
24
|
+
if (compareVersions(range.to, range.from) <= 0) return null;
|
|
25
|
+
const relevant = (scope) => scope === "base" || scope === "primitives" || scopes.has(scope);
|
|
26
|
+
const out = [];
|
|
27
|
+
for (const block of parseChangelog(markdown)) {
|
|
28
|
+
if (compareVersions(block.version, range.from) <= 0 || compareVersions(block.version, range.to) > 0) continue;
|
|
29
|
+
const sections = block.sections.filter((section) => relevant(section.scope));
|
|
30
|
+
if (sections.length === 0 && block.sections.length > 0) continue;
|
|
31
|
+
out.push(...block.header.map(styleLine));
|
|
32
|
+
for (const section of sections) {
|
|
33
|
+
out.push(chalk.bold(`### ${section.scope}`));
|
|
34
|
+
out.push(...section.lines.map(styleLine));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const text = trimBlankRuns(out).join("\n").trim();
|
|
38
|
+
return text.length > 0 ? text : null;
|
|
39
|
+
}
|
|
40
|
+
/** Splits the changelog into version blocks; tolerant of prose around them. */
|
|
41
|
+
function parseChangelog(markdown) {
|
|
42
|
+
const blocks = [];
|
|
43
|
+
let current = null;
|
|
44
|
+
let section = null;
|
|
45
|
+
for (const line of markdown.split(/\r?\n/)) {
|
|
46
|
+
const versionMatch = /^##\s+(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)\b(.*)$/.exec(line);
|
|
47
|
+
if (versionMatch?.[1] !== void 0) {
|
|
48
|
+
current = {
|
|
49
|
+
version: versionMatch[1],
|
|
50
|
+
header: [line],
|
|
51
|
+
sections: []
|
|
52
|
+
};
|
|
53
|
+
section = null;
|
|
54
|
+
blocks.push(current);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (current === null) continue;
|
|
58
|
+
const scopeMatch = /^###\s+(\S+)\s*$/.exec(line);
|
|
59
|
+
if (scopeMatch?.[1] !== void 0) {
|
|
60
|
+
section = {
|
|
61
|
+
scope: scopeMatch[1],
|
|
62
|
+
lines: []
|
|
63
|
+
};
|
|
64
|
+
current.sections.push(section);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (section) section.lines.push(line);
|
|
68
|
+
else current.header.push(line);
|
|
69
|
+
}
|
|
70
|
+
return blocks;
|
|
71
|
+
}
|
|
72
|
+
/** Highlights breaking entries; everything else passes through. */
|
|
73
|
+
function styleLine(line) {
|
|
74
|
+
if (line.startsWith("## ")) return chalk.bold(line);
|
|
75
|
+
if (line.includes("**BREAKING**")) return chalk.red.bold(line);
|
|
76
|
+
return line;
|
|
77
|
+
}
|
|
78
|
+
function trimBlankRuns(lines) {
|
|
79
|
+
const out = [];
|
|
80
|
+
for (const line of lines) {
|
|
81
|
+
if (line.trim() === "" && out[out.length - 1]?.trim() === "") continue;
|
|
82
|
+
out.push(line);
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
/** Plain x.y.z(-pre) comparison: negative when a < b. */
|
|
87
|
+
function compareVersions(a, b) {
|
|
88
|
+
const [coreA = "", preA] = a.split("-", 2);
|
|
89
|
+
const [coreB = "", preB] = b.split("-", 2);
|
|
90
|
+
const partsA = coreA.split(".").map(Number);
|
|
91
|
+
const partsB = coreB.split(".").map(Number);
|
|
92
|
+
for (let i = 0; i < 3; i += 1) {
|
|
93
|
+
const delta = (partsA[i] ?? 0) - (partsB[i] ?? 0);
|
|
94
|
+
if (delta !== 0) return delta;
|
|
95
|
+
}
|
|
96
|
+
if (preA === preB) return 0;
|
|
97
|
+
if (preA === void 0) return 1;
|
|
98
|
+
if (preB === void 0) return -1;
|
|
99
|
+
return preA < preB ? -1 : 1;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/merge.ts
|
|
104
|
+
/**
|
|
105
|
+
* @fileoverview Per-file update classification and 3-way merging.
|
|
106
|
+
* @description `classify` turns one file's three states — ours (on disk),
|
|
107
|
+
* base (pristine bytes at the recorded version), theirs (pristine bytes at
|
|
108
|
+
* the target version) — plus the recorded hash into a disposition. The
|
|
109
|
+
* recorded hash is what makes edits detectable: it is always the hash of
|
|
110
|
+
* pristine bytes at vendor time, never the on-disk state. Text conflicts are
|
|
111
|
+
* merged through node-diff3 with diff3-style labeled markers; anything
|
|
112
|
+
* containing NUL bytes is treated as binary and never machine-merged.
|
|
113
|
+
*/
|
|
114
|
+
function classify(input) {
|
|
115
|
+
const { ours, base, theirs, recordedHash } = input;
|
|
116
|
+
if (theirs === null) {
|
|
117
|
+
if (ours === null) return { kind: "delete-pristine" };
|
|
118
|
+
if (recordedHash !== null && sha256(ours) === recordedHash) return { kind: "delete-pristine" };
|
|
119
|
+
return { kind: "removed-upstream-edited" };
|
|
120
|
+
}
|
|
121
|
+
if (recordedHash === null) return { kind: "create" };
|
|
122
|
+
if (ours === null) return { kind: "restore" };
|
|
123
|
+
if (bytesEqual(ours, theirs)) return { kind: "unchanged" };
|
|
124
|
+
if (!(sha256(ours) !== recordedHash)) return { kind: "take-theirs" };
|
|
125
|
+
if (base !== null && bytesEqual(base, theirs)) return { kind: "keep-ours" };
|
|
126
|
+
if (isBinary(ours) || base !== null && isBinary(base) || isBinary(theirs)) return {
|
|
127
|
+
kind: "conflict",
|
|
128
|
+
markers: null
|
|
129
|
+
};
|
|
130
|
+
return merge3(ours.toString("utf8"), base === null ? "" : toText(base), toText(theirs), input.labels);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* @description 3-way text merge. `excludeFalseConflicts` keeps identical
|
|
134
|
+
* both-sides edits out of the conflict count; splitting on `\r?\n` and
|
|
135
|
+
* joining with `\n` means merged output is LF-normalized.
|
|
136
|
+
*/
|
|
137
|
+
function merge3(ours, base, theirs, labels) {
|
|
138
|
+
const merged = mergeDiff3(ours, base, theirs, {
|
|
139
|
+
excludeFalseConflicts: true,
|
|
140
|
+
stringSeparator: /\r?\n/,
|
|
141
|
+
label: {
|
|
142
|
+
a: labels.ours,
|
|
143
|
+
o: labels.base,
|
|
144
|
+
b: labels.theirs
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
const content = merged.result.join("\n");
|
|
148
|
+
if (merged.conflict) return {
|
|
149
|
+
kind: "conflict",
|
|
150
|
+
markers: content
|
|
151
|
+
};
|
|
152
|
+
return {
|
|
153
|
+
kind: "auto-merged",
|
|
154
|
+
content
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
/** NUL byte anywhere = binary; the same heuristic git uses. */
|
|
158
|
+
function isBinary(content) {
|
|
159
|
+
return typeof content === "string" ? content.includes("\0") : content.includes(0);
|
|
160
|
+
}
|
|
161
|
+
function toText(content) {
|
|
162
|
+
return typeof content === "string" ? content : content.toString("utf8");
|
|
163
|
+
}
|
|
164
|
+
function bytesEqual(a, b) {
|
|
165
|
+
const bufA = typeof a === "string" ? Buffer.from(a) : a;
|
|
166
|
+
const bufB = typeof b === "string" ? Buffer.from(b) : b;
|
|
167
|
+
return bufA.equals(bufB);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/commands/update.ts
|
|
172
|
+
/**
|
|
173
|
+
* @fileoverview `zazz-ui update` — move vendored files to a new kit version
|
|
174
|
+
* with provenance-recorded 3-way merges.
|
|
175
|
+
* @description Whole-kit by default; naming primitives narrows the update
|
|
176
|
+
* (base files stay put, skew recorded via per-primitive versions). Every
|
|
177
|
+
* file is classified against its pristine bytes at the *recorded* version
|
|
178
|
+
* (the merge ancestor) and at the target: pristine files silently take
|
|
179
|
+
* theirs, edited-but-upstream-unchanged files stay yours, real conflicts
|
|
180
|
+
* prompt (keep / theirs / markers / skip). A skip rolls back the whole unit —
|
|
181
|
+
* that primitive (or the base platform) stays at its recorded version so a
|
|
182
|
+
* re-run can offer the merge again — and turns the exit code to 2. Generated
|
|
183
|
+
* entries (index.css / index.js / head.html) belong to the base unit; their
|
|
184
|
+
* merge ancestor and target are regenerated, not read from the tarball.
|
|
185
|
+
* head.html is CLI-owned: it always takes the regenerated version.
|
|
186
|
+
*/
|
|
187
|
+
/** Splits positional args: the @-prefixed one is the version, the rest names. */
|
|
188
|
+
function splitVersionArgs(args) {
|
|
189
|
+
let version;
|
|
190
|
+
const names = [];
|
|
191
|
+
for (const arg of args) if (arg.startsWith("@")) {
|
|
192
|
+
if (version !== void 0) throw new ZazzError(`two version arguments: @${version} and ${arg}`);
|
|
193
|
+
version = parseVersionArg(arg);
|
|
194
|
+
} else names.push(arg);
|
|
195
|
+
return {
|
|
196
|
+
version: version ?? "latest",
|
|
197
|
+
names
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
async function runUpdate(args, flags, global) {
|
|
201
|
+
const cwd = path.resolve(global.cwd);
|
|
202
|
+
const ui = createUi({
|
|
203
|
+
silent: global.silent === true,
|
|
204
|
+
yes: global.yes === true
|
|
205
|
+
});
|
|
206
|
+
const fetch = await loadFetchOptions({
|
|
207
|
+
cwd,
|
|
208
|
+
registry: global.registry,
|
|
209
|
+
offline: global.offline,
|
|
210
|
+
preferOffline: global.preferOffline
|
|
211
|
+
});
|
|
212
|
+
const loaded = await loadConfig(cwd);
|
|
213
|
+
if (!loaded) throw new ZazzError("no zazz.json found — this project isn't initialized", { hint: "run `zazz-ui init` first" });
|
|
214
|
+
const { config, root } = loaded;
|
|
215
|
+
const { version, names } = splitVersionArgs(args);
|
|
216
|
+
const strategy = flags.keep === true ? "keep" : flags.theirs === true ? "theirs" : flags.markers === true ? "markers" : null;
|
|
217
|
+
for (const name of names) if (!config.primitives[name]) throw new ZazzError(`"${name}" isn't vendored in this project`, { hint: `vendored: ${Object.keys(config.primitives).sort().join(", ") || "(none — run zazz-ui add)"}` });
|
|
218
|
+
ui.intro("zazz-ui update");
|
|
219
|
+
const target = await ui.spinner(`Resolving @zazz-ui/core@${version}…`, () => resolveKit(kitSpec(version), fetch), (resolved) => `Target @zazz-ui/core@${resolved.version}`);
|
|
220
|
+
const wholeKit = names.length === 0;
|
|
221
|
+
const vendoredNames = Object.keys(config.primitives);
|
|
222
|
+
const scopeNames = wholeKit ? vendoredNames : names;
|
|
223
|
+
const survivors = scopeNames.filter((name) => target.manifest.primitives[name]);
|
|
224
|
+
const freshDeps = target.manifest.resolveClosure(survivors).filter((name) => !config.primitives[name]);
|
|
225
|
+
if (!(freshDeps.length > 0 || scopeNames.some((name) => config.primitives[name]?.version !== target.version) || wholeKit && (config.kit.version !== target.version || (config.base.version ?? config.kit.version) !== target.version))) {
|
|
226
|
+
ui.outro(`Already at ${target.version} — nothing to update.`);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const changelog = await readChangelog(target);
|
|
230
|
+
if (changelog && compareVersions(target.version, config.kit.version) > 0) {
|
|
231
|
+
const slice = sliceChangelog(changelog, {
|
|
232
|
+
from: config.kit.version,
|
|
233
|
+
to: target.version
|
|
234
|
+
}, /* @__PURE__ */ new Set([...vendoredNames, ...freshDeps]));
|
|
235
|
+
if (slice) ui.step(slice);
|
|
236
|
+
}
|
|
237
|
+
const kitCache = /* @__PURE__ */ new Map();
|
|
238
|
+
kitCache.set(target.version, Promise.resolve(target));
|
|
239
|
+
const kitAt = (v) => {
|
|
240
|
+
let kit = kitCache.get(v);
|
|
241
|
+
if (!kit) {
|
|
242
|
+
kit = resolveKit(kitSpec(v), fetch);
|
|
243
|
+
kitCache.set(v, kit);
|
|
244
|
+
}
|
|
245
|
+
return kit;
|
|
246
|
+
};
|
|
247
|
+
const context = {
|
|
248
|
+
config,
|
|
249
|
+
root,
|
|
250
|
+
target,
|
|
251
|
+
kitAt,
|
|
252
|
+
ui,
|
|
253
|
+
strategy,
|
|
254
|
+
interactive: ui.interactive
|
|
255
|
+
};
|
|
256
|
+
const units = [];
|
|
257
|
+
for (const name of scopeNames) units.push(await planPrimitiveUnit(name, context));
|
|
258
|
+
const primitiveVersionAfter = new Map(vendoredNames.map((name) => [name, config.primitives[name]?.version ?? target.version]));
|
|
259
|
+
for (const unit of units) if (!unit.skipped) primitiveVersionAfter.set(unit.unit, target.version);
|
|
260
|
+
for (const dep of freshDeps) primitiveVersionAfter.set(dep, target.version);
|
|
261
|
+
const freshWrites = [];
|
|
262
|
+
const freshHashes = /* @__PURE__ */ new Map();
|
|
263
|
+
for (const dep of freshDeps) {
|
|
264
|
+
const entry = target.manifest.primitives[dep];
|
|
265
|
+
if (!entry) continue;
|
|
266
|
+
const hashes = {};
|
|
267
|
+
for (const file of primitiveFiles(entry, config.language, { examples: false })) {
|
|
268
|
+
const content = await target.readFile(file);
|
|
269
|
+
hashes[file] = sha256(content);
|
|
270
|
+
freshWrites.push({
|
|
271
|
+
dest: path.join(root, config.dir, ...file.split("/")),
|
|
272
|
+
content,
|
|
273
|
+
note: "vendor"
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
freshHashes.set(dep, hashes);
|
|
277
|
+
}
|
|
278
|
+
const baseUnit = await planBaseUnit(context, {
|
|
279
|
+
wholeKit,
|
|
280
|
+
primitiveVersionAfter,
|
|
281
|
+
removedPrimitives: units.filter((unit) => unit.removedFromKit && !unit.skipped)
|
|
282
|
+
});
|
|
283
|
+
units.push(baseUnit);
|
|
284
|
+
const { kept: keptFresh, skipped: freshSkipped } = await filterCollisions(freshWrites, {
|
|
285
|
+
ui,
|
|
286
|
+
force: flags.force === true
|
|
287
|
+
});
|
|
288
|
+
const applied = units.filter((unit) => !unit.skipped);
|
|
289
|
+
const skippedUnits = units.filter((unit) => unit.skipped);
|
|
290
|
+
for (const unit of applied) {
|
|
291
|
+
if (unit.unit === "base") continue;
|
|
292
|
+
if (unit.removedFromKit) delete config.primitives[unit.unit];
|
|
293
|
+
else config.primitives[unit.unit] = {
|
|
294
|
+
version: target.version,
|
|
295
|
+
files: unit.hashes
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
if (!baseUnit.skipped) config.base.files = baseUnit.hashes;
|
|
299
|
+
for (const dep of freshDeps) config.primitives[dep] = {
|
|
300
|
+
version: target.version,
|
|
301
|
+
files: freshHashes.get(dep) ?? {}
|
|
302
|
+
};
|
|
303
|
+
if (wholeKit) {
|
|
304
|
+
const oldBaseVersion = config.base.version ?? config.kit.version;
|
|
305
|
+
config.kit = {
|
|
306
|
+
version: target.version,
|
|
307
|
+
integrity: target.integrity
|
|
308
|
+
};
|
|
309
|
+
if (baseUnit.skipped) config.base.version = oldBaseVersion;
|
|
310
|
+
else delete config.base.version;
|
|
311
|
+
}
|
|
312
|
+
const tx = {
|
|
313
|
+
root,
|
|
314
|
+
writes: [...applied.flatMap((unit) => unit.writes), ...keptFresh],
|
|
315
|
+
deletes: applied.flatMap((unit) => unit.deletes),
|
|
316
|
+
config
|
|
317
|
+
};
|
|
318
|
+
if (flags.dryRun === true) {
|
|
319
|
+
ui.step(`plan:\n${describe(tx)}`);
|
|
320
|
+
ui.outro("Dry run — nothing written.");
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
await apply(tx);
|
|
324
|
+
for (const unit of applied) for (const note of unit.notes) ui.step(note);
|
|
325
|
+
if (freshDeps.length > 0) ui.step(`New dependencies vendored at ${target.version}: ${freshDeps.join(", ")}`);
|
|
326
|
+
const straySkips = applied.reduce((count, unit) => count + unit.straySkips, 0);
|
|
327
|
+
if (skippedUnits.length > 0 || freshSkipped.length > 0 || straySkips > 0) {
|
|
328
|
+
if (skippedUnits.length > 0) ui.warn(`Skipped (left at their recorded version): ${skippedUnits.map((unit) => unit.unit).join(", ")}\n re-run \`zazz-ui update\` to resolve, or pass --keep / --theirs / --markers`);
|
|
329
|
+
if (freshSkipped.length > 0) ui.warn(`Skipped ${freshSkipped.length} existing file(s) (kept yours; --force overwrites):\n ${freshSkipped.map((dest) => path.relative(root, dest)).join("\n ")}`);
|
|
330
|
+
process.exitCode = 2;
|
|
331
|
+
}
|
|
332
|
+
const updatedPrimitives = applied.filter((unit) => unit.unit !== "base" && !unit.removedFromKit);
|
|
333
|
+
ui.outro(wholeKit ? `Project now tracks @zazz-ui/core@${target.version} (${applied.length} unit${applied.length === 1 ? "" : "s"} updated${skippedUnits.length > 0 ? `, ${skippedUnits.length} skipped` : ""}).` : `Updated ${updatedPrimitives.map((unit) => unit.unit).join(", ") || "nothing"} to ${target.version}.`);
|
|
334
|
+
}
|
|
335
|
+
/** Plans one primitive's update; prompts are answered here, writes staged. */
|
|
336
|
+
async function planPrimitiveUnit(name, context) {
|
|
337
|
+
const { config, target } = context;
|
|
338
|
+
const record = config.primitives[name];
|
|
339
|
+
if (!record) throw new ZazzError(`primitive record vanished: ${name}`);
|
|
340
|
+
const oldKit = await context.kitAt(record.version);
|
|
341
|
+
const newEntry = target.manifest.primitives[name];
|
|
342
|
+
const unit = {
|
|
343
|
+
unit: name,
|
|
344
|
+
fromVersion: record.version,
|
|
345
|
+
writes: [],
|
|
346
|
+
deletes: [],
|
|
347
|
+
hashes: {},
|
|
348
|
+
skipped: false,
|
|
349
|
+
removedFromKit: false,
|
|
350
|
+
notes: [],
|
|
351
|
+
straySkips: 0
|
|
352
|
+
};
|
|
353
|
+
if (!newEntry) {
|
|
354
|
+
unit.removedFromKit = true;
|
|
355
|
+
for (const [file, recordedHash] of Object.entries(record.files)) if (!await resolveFile(unit, context, {
|
|
356
|
+
file,
|
|
357
|
+
recordedHash,
|
|
358
|
+
base: () => readKitFile(oldKit, file),
|
|
359
|
+
theirs: async () => null
|
|
360
|
+
})) return unit;
|
|
361
|
+
unit.notes.push(`${name}: removed upstream in ${target.version}`);
|
|
362
|
+
return unit;
|
|
363
|
+
}
|
|
364
|
+
const wantExamples = newEntry.examples.some((file) => file in record.files);
|
|
365
|
+
const needed = primitiveFiles(newEntry, config.language, { examples: wantExamples });
|
|
366
|
+
const files = [.../* @__PURE__ */ new Set([...Object.keys(record.files), ...needed])];
|
|
367
|
+
for (const file of files) if (!await resolveFile(unit, context, {
|
|
368
|
+
file,
|
|
369
|
+
recordedHash: record.files[file] ?? null,
|
|
370
|
+
base: () => readKitFile(oldKit, file),
|
|
371
|
+
theirs: () => readKitFile(target, file)
|
|
372
|
+
})) return unit;
|
|
373
|
+
return unit;
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* @description Plans the base unit. Whole-kit updates merge every base file;
|
|
377
|
+
* narrowed updates only regenerate the entries and vendor base scripts the
|
|
378
|
+
* updated/new primitives now need — the platform itself stays put.
|
|
379
|
+
*/
|
|
380
|
+
async function planBaseUnit(context, options) {
|
|
381
|
+
const { config, target } = context;
|
|
382
|
+
const baseVersion = config.base.version ?? config.kit.version;
|
|
383
|
+
const oldKit = await context.kitAt(baseVersion);
|
|
384
|
+
const entriesKit = options.wholeKit ? target : oldKit;
|
|
385
|
+
const unit = {
|
|
386
|
+
unit: "base",
|
|
387
|
+
fromVersion: baseVersion,
|
|
388
|
+
writes: [],
|
|
389
|
+
deletes: [],
|
|
390
|
+
hashes: {},
|
|
391
|
+
skipped: false,
|
|
392
|
+
removedFromKit: false,
|
|
393
|
+
notes: [],
|
|
394
|
+
straySkips: 0
|
|
395
|
+
};
|
|
396
|
+
const entryScript = config.language === "ts" ? "index.ts" : "index.js";
|
|
397
|
+
const generated = /* @__PURE__ */ new Set([
|
|
398
|
+
"index.css",
|
|
399
|
+
"index.js",
|
|
400
|
+
"index.ts",
|
|
401
|
+
"head.html"
|
|
402
|
+
]);
|
|
403
|
+
const removed = new Set(options.removedPrimitives.map((plan) => plan.unit));
|
|
404
|
+
const beforeVersions = new Map(Object.entries(config.primitives).map(([name, entry]) => [name, entry.version]));
|
|
405
|
+
const afterVersions = new Map([...options.primitiveVersionAfter].filter(([name]) => !removed.has(name)));
|
|
406
|
+
const entriesBefore = await entrySources(context, oldKit, beforeVersions);
|
|
407
|
+
const entriesAfter = await entrySources(context, entriesKit, afterVersions);
|
|
408
|
+
const afterEntries = [];
|
|
409
|
+
for (const [name, primitiveVersion] of afterVersions) {
|
|
410
|
+
const entry = (await context.kitAt(primitiveVersion)).manifest.primitives[name];
|
|
411
|
+
if (entry) afterEntries.push(entry);
|
|
412
|
+
}
|
|
413
|
+
const neededScripts = baseScriptFiles(afterEntries, config.language);
|
|
414
|
+
const vendorable = options.wholeKit ? [.../* @__PURE__ */ new Set([
|
|
415
|
+
...Object.keys(config.base.files).filter((file) => !generated.has(file)),
|
|
416
|
+
...baseFiles(target.manifest, config.language),
|
|
417
|
+
...neededScripts
|
|
418
|
+
])] : neededScripts.filter((file) => !(file in config.base.files));
|
|
419
|
+
if (!options.wholeKit) {
|
|
420
|
+
for (const [file, hash] of Object.entries(config.base.files)) if (!generated.has(file)) unit.hashes[file] = hash;
|
|
421
|
+
}
|
|
422
|
+
for (const file of vendorable) if (!await resolveFile(unit, context, {
|
|
423
|
+
file,
|
|
424
|
+
recordedHash: config.base.files[file] ?? null,
|
|
425
|
+
base: () => readKitFile(oldKit, file),
|
|
426
|
+
theirs: () => readKitFile(options.wholeKit ? target : oldKit, file)
|
|
427
|
+
})) return unit;
|
|
428
|
+
for (const file of ["index.css", entryScript]) if (!await resolveFile(unit, context, {
|
|
429
|
+
file,
|
|
430
|
+
recordedHash: config.base.files[file] ?? null,
|
|
431
|
+
base: async () => file === "index.css" ? entriesBefore.css : entriesBefore.js,
|
|
432
|
+
theirs: async () => file === "index.css" ? entriesAfter.css : entriesAfter.js
|
|
433
|
+
})) return unit;
|
|
434
|
+
const nextHead = renderHead(entriesKit, config);
|
|
435
|
+
const headPath = destPath(context, "head.html");
|
|
436
|
+
if (nextHead !== (existsSync(headPath) ? await readFile(headPath, "utf8") : null)) unit.writes.push({
|
|
437
|
+
dest: headPath,
|
|
438
|
+
content: nextHead,
|
|
439
|
+
note: "head"
|
|
440
|
+
});
|
|
441
|
+
unit.hashes["head.html"] = sha256(nextHead);
|
|
442
|
+
return unit;
|
|
443
|
+
}
|
|
444
|
+
/** Regenerates the entry pair for a vendored set at explicit versions. */
|
|
445
|
+
async function entrySources(context, baseKit, versions) {
|
|
446
|
+
const cascade = new Map(context.target.manifest.cssCascadeOrder.map((name, index) => [name, index]));
|
|
447
|
+
const ordered = [...versions.keys()].sort((a, b) => (cascade.get(a) ?? Infinity) - (cascade.get(b) ?? Infinity));
|
|
448
|
+
const primitives = [];
|
|
449
|
+
const scripts = [];
|
|
450
|
+
for (const name of ordered) {
|
|
451
|
+
const version = versions.get(name);
|
|
452
|
+
if (version === void 0) continue;
|
|
453
|
+
const entry = (await context.kitAt(version)).manifest.primitives[name];
|
|
454
|
+
if (!entry) continue;
|
|
455
|
+
primitives.push({
|
|
456
|
+
name,
|
|
457
|
+
css: entry.css
|
|
458
|
+
});
|
|
459
|
+
scripts.push(...entry.base, ...entry.js);
|
|
460
|
+
}
|
|
461
|
+
return {
|
|
462
|
+
css: renderIndexCss({
|
|
463
|
+
kit: baseKit,
|
|
464
|
+
legacy: context.config.legacy,
|
|
465
|
+
primitives
|
|
466
|
+
}),
|
|
467
|
+
js: appendJsImports(renderIndexJs({
|
|
468
|
+
kit: baseKit,
|
|
469
|
+
language: context.config.language
|
|
470
|
+
}), [...new Set(scripts)], context.config.language)
|
|
471
|
+
};
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* @description Classifies one file and resolves its disposition into the
|
|
475
|
+
* unit plan. Returns false when the user (or non-interactive default) chose
|
|
476
|
+
* to skip — the caller abandons the whole unit.
|
|
477
|
+
*/
|
|
478
|
+
async function resolveFile(unit, context, input) {
|
|
479
|
+
const { file } = input;
|
|
480
|
+
const dest = destPath(context, file);
|
|
481
|
+
const rel = path.relative(context.root, dest);
|
|
482
|
+
const ours = existsSync(dest) ? await readFile(dest) : null;
|
|
483
|
+
const theirs = await input.theirs();
|
|
484
|
+
const base = await input.base();
|
|
485
|
+
const labels = {
|
|
486
|
+
ours: "yours",
|
|
487
|
+
base: unit.fromVersion,
|
|
488
|
+
theirs: context.target.version
|
|
489
|
+
};
|
|
490
|
+
const disposition = classify({
|
|
491
|
+
ours,
|
|
492
|
+
base,
|
|
493
|
+
theirs,
|
|
494
|
+
recordedHash: input.recordedHash,
|
|
495
|
+
labels
|
|
496
|
+
});
|
|
497
|
+
const recordTheirs = () => {
|
|
498
|
+
if (theirs !== null) unit.hashes[file] = sha256(theirs);
|
|
499
|
+
};
|
|
500
|
+
switch (disposition.kind) {
|
|
501
|
+
case "unchanged":
|
|
502
|
+
recordTheirs();
|
|
503
|
+
return true;
|
|
504
|
+
case "take-theirs":
|
|
505
|
+
if (theirs !== null) unit.writes.push({
|
|
506
|
+
dest,
|
|
507
|
+
content: theirs,
|
|
508
|
+
note: "update"
|
|
509
|
+
});
|
|
510
|
+
recordTheirs();
|
|
511
|
+
return true;
|
|
512
|
+
case "keep-ours":
|
|
513
|
+
recordTheirs();
|
|
514
|
+
return true;
|
|
515
|
+
case "auto-merged":
|
|
516
|
+
unit.writes.push({
|
|
517
|
+
dest,
|
|
518
|
+
content: disposition.content,
|
|
519
|
+
note: "merge"
|
|
520
|
+
});
|
|
521
|
+
recordTheirs();
|
|
522
|
+
unit.notes.push(`${rel}: merged your edits with ${context.target.version}`);
|
|
523
|
+
return true;
|
|
524
|
+
case "restore":
|
|
525
|
+
if (theirs !== null) unit.writes.push({
|
|
526
|
+
dest,
|
|
527
|
+
content: theirs,
|
|
528
|
+
note: "restore"
|
|
529
|
+
});
|
|
530
|
+
recordTheirs();
|
|
531
|
+
unit.notes.push(`${rel}: was deleted locally — restored at ${context.target.version}`);
|
|
532
|
+
return true;
|
|
533
|
+
case "create":
|
|
534
|
+
if (ours !== null && theirs !== null && sha256(ours) === sha256(theirs)) {
|
|
535
|
+
recordTheirs();
|
|
536
|
+
return true;
|
|
537
|
+
}
|
|
538
|
+
if (ours !== null) {
|
|
539
|
+
if (!await context.ui.confirm(`${dest} already exists. Overwrite?`, false)) {
|
|
540
|
+
recordTheirs();
|
|
541
|
+
unit.notes.push(`${rel}: kept your existing file (new upstream file skipped)`);
|
|
542
|
+
unit.straySkips += 1;
|
|
543
|
+
return true;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
if (theirs !== null) unit.writes.push({
|
|
547
|
+
dest,
|
|
548
|
+
content: theirs,
|
|
549
|
+
note: "vendor"
|
|
550
|
+
});
|
|
551
|
+
recordTheirs();
|
|
552
|
+
return true;
|
|
553
|
+
case "delete-pristine":
|
|
554
|
+
if (ours !== null) unit.deletes.push(dest);
|
|
555
|
+
unit.notes.push(`${rel}: removed upstream — deleted (it was unedited)`);
|
|
556
|
+
return true;
|
|
557
|
+
case "removed-upstream-edited": return resolveRemovedEdited(unit, context, {
|
|
558
|
+
file,
|
|
559
|
+
dest,
|
|
560
|
+
rel
|
|
561
|
+
});
|
|
562
|
+
case "conflict": return resolveConflict(unit, context, {
|
|
563
|
+
file,
|
|
564
|
+
dest,
|
|
565
|
+
rel,
|
|
566
|
+
theirs,
|
|
567
|
+
markers: disposition.markers
|
|
568
|
+
});
|
|
569
|
+
default: return true;
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
/** Upstream removed a file the user edited: keep (untracked) or delete. */
|
|
573
|
+
async function resolveRemovedEdited(unit, context, file) {
|
|
574
|
+
if ((context.strategy === "theirs" ? "delete" : context.strategy !== null || !context.interactive ? "keep" : await context.ui.select(`${file.rel} was removed in ${context.target.version}, but you've edited it.`, [{
|
|
575
|
+
value: "keep",
|
|
576
|
+
label: "Keep the file",
|
|
577
|
+
hint: "it becomes yours; no longer tracked"
|
|
578
|
+
}, {
|
|
579
|
+
value: "delete",
|
|
580
|
+
label: "Delete it",
|
|
581
|
+
hint: "follows upstream"
|
|
582
|
+
}], "keep")) === "delete") {
|
|
583
|
+
unit.deletes.push(file.dest);
|
|
584
|
+
unit.notes.push(`${file.rel}: removed upstream — deleted`);
|
|
585
|
+
} else unit.notes.push(`${file.rel}: removed upstream — kept your edited copy (now untracked)`);
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
/** A real conflict: strategy flag, prompt, or (non-interactive) skip. */
|
|
589
|
+
async function resolveConflict(unit, context, file) {
|
|
590
|
+
const binary = file.markers === null;
|
|
591
|
+
let choice;
|
|
592
|
+
if (context.strategy !== null) {
|
|
593
|
+
choice = context.strategy;
|
|
594
|
+
if (choice === "markers" && binary) {
|
|
595
|
+
context.ui.warn(`${file.rel}: binary conflict — markers are impossible, skipping ${unit.unit}`);
|
|
596
|
+
choice = "skip";
|
|
597
|
+
}
|
|
598
|
+
} else if (!context.interactive) choice = "skip";
|
|
599
|
+
else {
|
|
600
|
+
const choices = [
|
|
601
|
+
...binary ? [] : [{
|
|
602
|
+
value: "markers",
|
|
603
|
+
label: "Write conflict markers",
|
|
604
|
+
hint: "resolve in your editor"
|
|
605
|
+
}],
|
|
606
|
+
{
|
|
607
|
+
value: "keep",
|
|
608
|
+
label: "Keep your version",
|
|
609
|
+
hint: "still records the new pristine base"
|
|
610
|
+
},
|
|
611
|
+
{
|
|
612
|
+
value: "theirs",
|
|
613
|
+
label: `Take ${context.target.version}`,
|
|
614
|
+
hint: "discards your edits"
|
|
615
|
+
},
|
|
616
|
+
{
|
|
617
|
+
value: "skip",
|
|
618
|
+
label: `Skip ${unit.unit}`,
|
|
619
|
+
hint: "leaves the whole unit at its recorded version"
|
|
620
|
+
}
|
|
621
|
+
];
|
|
622
|
+
choice = await context.ui.select(`${file.rel}: your edits conflict with ${context.target.version}${binary ? " (binary)" : ""}`, choices, binary ? "keep" : "markers");
|
|
623
|
+
}
|
|
624
|
+
switch (choice) {
|
|
625
|
+
case "skip":
|
|
626
|
+
unit.skipped = true;
|
|
627
|
+
return false;
|
|
628
|
+
case "keep":
|
|
629
|
+
if (file.theirs !== null) unit.hashes[file.file] = sha256(file.theirs);
|
|
630
|
+
unit.notes.push(`${file.rel}: kept your version (conflicted with ${context.target.version})`);
|
|
631
|
+
return true;
|
|
632
|
+
case "theirs":
|
|
633
|
+
if (file.theirs !== null) {
|
|
634
|
+
unit.writes.push({
|
|
635
|
+
dest: file.dest,
|
|
636
|
+
content: file.theirs,
|
|
637
|
+
note: "update"
|
|
638
|
+
});
|
|
639
|
+
unit.hashes[file.file] = sha256(file.theirs);
|
|
640
|
+
}
|
|
641
|
+
return true;
|
|
642
|
+
case "markers":
|
|
643
|
+
if (file.markers !== null) unit.writes.push({
|
|
644
|
+
dest: file.dest,
|
|
645
|
+
content: file.markers,
|
|
646
|
+
note: "merge"
|
|
647
|
+
});
|
|
648
|
+
if (file.theirs !== null) unit.hashes[file.file] = sha256(file.theirs);
|
|
649
|
+
unit.notes.push(`${file.rel}: conflict markers written — resolve them in your editor`);
|
|
650
|
+
return true;
|
|
651
|
+
default: return true;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
function destPath(context, file) {
|
|
655
|
+
return path.join(context.root, context.config.dir, ...file.split("/"));
|
|
656
|
+
}
|
|
657
|
+
async function readKitFile(kit, file) {
|
|
658
|
+
return kit.has(file) ? kit.readFile(file) : null;
|
|
659
|
+
}
|
|
660
|
+
/** The kit's CHANGELOG.md, from the extracted tarball root; null if absent. */
|
|
661
|
+
async function readChangelog(kit) {
|
|
662
|
+
try {
|
|
663
|
+
return await readFile(path.join(kit.extractDir, "CHANGELOG.md"), "utf8");
|
|
664
|
+
} catch {
|
|
665
|
+
return null;
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
//#endregion
|
|
670
|
+
export { compareVersions as a, isBinary as i, runUpdate as n, sliceChangelog as o, splitVersionArgs as r, readChangelog as t };
|