@unbase-app/cli 0.0.0-stage → 0.2.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/AGENTS.md +37 -0
- package/LICENSE +21 -0
- package/README.md +211 -2
- package/agents/astro.md +162 -0
- package/agents/next.md +109 -0
- package/agents/vite-react.md +108 -0
- package/bin/unbase.mjs +147 -0
- package/lib/args.mjs +49 -0
- package/lib/build.mjs +71 -0
- package/lib/compare.mjs +96 -0
- package/lib/config-edit.mjs +629 -0
- package/lib/doctor.mjs +37 -0
- package/lib/errors.mjs +15 -0
- package/lib/feature.mjs +23 -0
- package/lib/frameworks/astro.mjs +485 -0
- package/lib/frameworks/index.mjs +50 -0
- package/lib/frameworks/next.mjs +321 -0
- package/lib/frameworks/vite-react.mjs +299 -0
- package/lib/init.mjs +165 -0
- package/lib/next-config.mjs +131 -0
- package/lib/pm.mjs +109 -0
- package/lib/readers.mjs +1918 -0
- package/lib/site.mjs +153 -0
- package/lib/uninstall.mjs +136 -0
- package/lib/versions.mjs +71 -0
- package/lib/vite-config.mjs +294 -0
- package/package.json +43 -4
package/bin/unbase.mjs
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// ============================================================================
|
|
3
|
+
// unbase — adds Unbase to a site (Astro, Vite + React, Next.js: lib/frameworks), checks it, and takes it out again.
|
|
4
|
+
//
|
|
5
|
+
// unbase init [--comments | --no-comments] [--project <id>] [--pointer[=FILE] | --no-pointer]
|
|
6
|
+
// [--no-install] [--tarballs <dir>] [--yes] [--json] [--dir <path>]
|
|
7
|
+
// unbase doctor [--no-build] [--json] [--dir <path>]
|
|
8
|
+
// unbase uninstall [--yes] [--no-build] [--no-install] [--json] [--dir <path>]
|
|
9
|
+
//
|
|
10
|
+
// Exit codes: 0 passed, 1 failed, 2 could not run. Never asks without a
|
|
11
|
+
// terminal: a choice without its flag is an error. Never calls Unbase.
|
|
12
|
+
// ============================================================================
|
|
13
|
+
|
|
14
|
+
import { createInterface } from "node:readline/promises";
|
|
15
|
+
import { doctor } from "../lib/doctor.mjs";
|
|
16
|
+
import { COULD_NOT_RUN, CouldNotRun } from "../lib/errors.mjs";
|
|
17
|
+
import { FRAMEWORKS } from "../lib/frameworks/index.mjs";
|
|
18
|
+
import { init } from "../lib/init.mjs";
|
|
19
|
+
import { parseArgs } from "../lib/args.mjs";
|
|
20
|
+
import { uninstall } from "../lib/uninstall.mjs";
|
|
21
|
+
import { SELF } from "../lib/versions.mjs";
|
|
22
|
+
|
|
23
|
+
const USAGE = `unbase ${SELF.version}: Unbase in a site (${FRAMEWORKS.map((f) => f.name).join(", ")})
|
|
24
|
+
|
|
25
|
+
unbase init Add the framework's package (or move it up to the pinned version; a site
|
|
26
|
+
from before 03-11 moves across to it), its line in the framework's config
|
|
27
|
+
(${FRAMEWORKS.map((f) => f.configFiles[0].replace(/\.[a-z]+$/, "")).join(", ")}), and comment-modus.json
|
|
28
|
+
--comments | --no-comments comments on the live site (?comment); default: in, or as the
|
|
29
|
+
site has it
|
|
30
|
+
--project <id or slug> link comment mode to an Unbase project now
|
|
31
|
+
--pointer[=AGENTS.md|CLAUDE.md] | --no-pointer
|
|
32
|
+
a line pointing the site's coding agent at Unbase's conventions
|
|
33
|
+
--no-install change package.json only; install yourself
|
|
34
|
+
--tarballs <dir> install packages from npm pack tarballs in <dir> (testing)
|
|
35
|
+
--yes the defaults for every choice not given
|
|
36
|
+
unbase doctor Check every feature as Unbase will read it
|
|
37
|
+
--no-build skip the production build and the checks that need it
|
|
38
|
+
unbase uninstall Take it all out again, and compare the built pages before and after
|
|
39
|
+
--yes do it without asking
|
|
40
|
+
--no-build skip the comparison
|
|
41
|
+
--no-install change package.json only
|
|
42
|
+
|
|
43
|
+
All: --json (for programs), --dir <path> (the site's folder; default: here)
|
|
44
|
+
Exit codes: 0 passed, 1 failed, 2 could not run.
|
|
45
|
+
After init: node_modules/${SELF.name}/AGENTS.md has the steps left to do.`;
|
|
46
|
+
|
|
47
|
+
const COMMANDS = { init, doctor, uninstall };
|
|
48
|
+
|
|
49
|
+
function makeIo(json) {
|
|
50
|
+
const interactive = !json && !!process.stdin.isTTY && !!process.stdout.isTTY;
|
|
51
|
+
return {
|
|
52
|
+
json,
|
|
53
|
+
interactive,
|
|
54
|
+
progress: (line) => (json ? undefined : process.stderr.write(`${line}\n`)),
|
|
55
|
+
async ask(question, fallback) {
|
|
56
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
57
|
+
try {
|
|
58
|
+
const answer = (await rl.question(`${question} ${fallback ? "[Y/n]" : "[y/N]"} `)).trim().toLowerCase();
|
|
59
|
+
return answer ? answer.startsWith("y") : fallback;
|
|
60
|
+
} finally {
|
|
61
|
+
rl.close();
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const MARK = { ok: "✓", missing: "✗", waiting: "…", info: "·", skipped: "–" };
|
|
68
|
+
const FEATURE = { on: "On", off: "Off", waiting: "Waiting", "not-installed": "Not installed" };
|
|
69
|
+
|
|
70
|
+
function render(r) {
|
|
71
|
+
const out = [];
|
|
72
|
+
if (r.command === "init") {
|
|
73
|
+
out.push(r.changed ? `Unbase added to ${r.site}` : `Nothing to change: Unbase is already in ${r.site}`);
|
|
74
|
+
for (const p of r.packages) {
|
|
75
|
+
const what =
|
|
76
|
+
p.action === "added"
|
|
77
|
+
? `+ ${p.name} ${p.version}`
|
|
78
|
+
: p.action === "updated"
|
|
79
|
+
? `↑ ${p.name} ${p.from} → ${p.to}`
|
|
80
|
+
: p.action === "removed"
|
|
81
|
+
? `- ${p.name}${p.replacedBy ? ` (replaced by ${p.replacedBy})` : ""}`
|
|
82
|
+
: `= ${p.name} ${p.have} (kept${p.note ? `: ${p.note}` : ""})`;
|
|
83
|
+
out.push(` ${what}${p.dev ? " (dev)" : ""}`);
|
|
84
|
+
}
|
|
85
|
+
for (const f of r.files) {
|
|
86
|
+
out.push(` ${f.action} ${f.file}`);
|
|
87
|
+
for (const c of f.changes) out.push(` ${c}`);
|
|
88
|
+
}
|
|
89
|
+
if (r.comments) out.push(r.project ? ` comments: linked to "${r.project}"` : " comments: waiting for the project (Unbase fills in comment-modus.json when the site is connected)");
|
|
90
|
+
out.push("", "Next:", ...r.next.map((n) => ` - ${n}`));
|
|
91
|
+
} else if (r.command === "doctor") {
|
|
92
|
+
for (const f of r.features) {
|
|
93
|
+
out.push(`${f.name}: ${FEATURE[f.state]}`);
|
|
94
|
+
for (const c of f.checks) {
|
|
95
|
+
out.push(` ${MARK[c.state]} ${c.message}`);
|
|
96
|
+
if (c.state === "missing" || c.state === "waiting") {
|
|
97
|
+
if (c.file) out.push(` file: ${c.file}`);
|
|
98
|
+
if (c.step) out.push(` fix: ${c.step}`);
|
|
99
|
+
}
|
|
100
|
+
if (c.detail && c.state !== "ok") out.push(...String(c.detail).split("\n").map((l) => ` | ${l}`));
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const s = r.summary;
|
|
104
|
+
out.push("", r.ok ? `Passed: ${s.on} on${s.waiting ? `, ${s.waiting} waiting on something outside this repository` : ""}${s.notInstalled ? `, ${s.notInstalled} not installed` : ""}.` : `Failed: ${s.off} feature(s) need a change in the site.`);
|
|
105
|
+
} else if (r.command === "uninstall") {
|
|
106
|
+
if (r.refused === "drafts") out.push(r.message, ...r.drafts.map((d) => ` ${d}`));
|
|
107
|
+
else {
|
|
108
|
+
for (const f of r.files ?? []) {
|
|
109
|
+
out.push(` ${f.action} ${f.file}`);
|
|
110
|
+
for (const c of f.changes) out.push(` ${c}`);
|
|
111
|
+
}
|
|
112
|
+
out.push("", r.message);
|
|
113
|
+
for (const d of r.comparison?.differences ?? []) {
|
|
114
|
+
out.push(` ${d.page}: ${d.change}`);
|
|
115
|
+
if (d.before) out.push(` before: …${d.before}…`, ` after: …${d.after}…`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return out.join("\n");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function main(argv) {
|
|
123
|
+
let json = argv.includes("--json");
|
|
124
|
+
try {
|
|
125
|
+
const { command, flags } = parseArgs(argv);
|
|
126
|
+
if (command === "help") {
|
|
127
|
+
console.log(USAGE);
|
|
128
|
+
return 0;
|
|
129
|
+
}
|
|
130
|
+
if (command === "version") {
|
|
131
|
+
console.log(SELF.version);
|
|
132
|
+
return 0;
|
|
133
|
+
}
|
|
134
|
+
json = !!flags.json;
|
|
135
|
+
const result = await COMMANDS[command](flags, makeIo(json));
|
|
136
|
+
console.log(json ? JSON.stringify(result, null, 2) : render(result));
|
|
137
|
+
return result.exitCode;
|
|
138
|
+
} catch (e) {
|
|
139
|
+
const known = e instanceof CouldNotRun;
|
|
140
|
+
const error = { code: known ? e.code : "internal", message: e.message, ...(known && e.file ? { file: e.file } : {}) };
|
|
141
|
+
if (json) console.log(JSON.stringify({ command: argv[0] ?? null, ok: false, exitCode: COULD_NOT_RUN, error }, null, 2));
|
|
142
|
+
else console.error(`unbase: ${e.message}${known ? "" : `\n${e.stack}`}${error.code === "usage" ? "\n\nRun unbase --help for the commands and flags." : ""}`);
|
|
143
|
+
return COULD_NOT_RUN;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
process.exitCode = await main(process.argv.slice(2));
|
package/lib/args.mjs
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Flags. Every choice is a flag (`--comments` / `--no-comments`, `--yes`):
|
|
3
|
+
// without a terminal a missing one is an error, never a question. Unknown
|
|
4
|
+
// flags are an error too, so a typo in a script fails instead of being
|
|
5
|
+
// ignored.
|
|
6
|
+
// ============================================================================
|
|
7
|
+
|
|
8
|
+
import { CouldNotRun } from "./errors.mjs";
|
|
9
|
+
|
|
10
|
+
/** Per command: flag → "bool" | "value" | "optional-value". */
|
|
11
|
+
export const FLAGS = {
|
|
12
|
+
init: { comments: "bool", "no-comments": "bool", project: "value", pointer: "optional-value", "no-pointer": "bool", "no-install": "bool", tarballs: "value", yes: "bool", json: "bool", dir: "value" },
|
|
13
|
+
doctor: { json: "bool", "no-build": "bool", dir: "value" },
|
|
14
|
+
uninstall: { yes: "bool", json: "bool", "no-build": "bool", "no-install": "bool", dir: "value" },
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** @returns {{ command: string | null, flags: Record<string, string | true> }} */
|
|
18
|
+
export function parseArgs(argv) {
|
|
19
|
+
const [command, ...rest] = argv;
|
|
20
|
+
if (!command || command === "--help" || command === "-h" || command === "help") return { command: "help", flags: {} };
|
|
21
|
+
if (command === "--version" || command === "-v") return { command: "version", flags: {} };
|
|
22
|
+
const known = FLAGS[command];
|
|
23
|
+
if (!known) throw new CouldNotRun("usage", `Unknown command "${command}". Commands: init, doctor, uninstall.`);
|
|
24
|
+
/** @type {Record<string, string | true>} */
|
|
25
|
+
const flags = {};
|
|
26
|
+
for (let i = 0; i < rest.length; i++) {
|
|
27
|
+
const arg = rest[i];
|
|
28
|
+
if (!arg.startsWith("--")) throw new CouldNotRun("usage", `Unexpected argument "${arg}" for ${command}.`);
|
|
29
|
+
const [name, inline] = arg.slice(2).split(/=(.*)/s, 2);
|
|
30
|
+
const kind = known[name];
|
|
31
|
+
if (!kind) throw new CouldNotRun("usage", `Unknown flag --${name} for ${command}. Flags: ${Object.keys(known).map((f) => `--${f}`).join(", ")}.`);
|
|
32
|
+
if (kind === "bool") {
|
|
33
|
+
if (inline !== undefined) throw new CouldNotRun("usage", `--${name} takes no value.`);
|
|
34
|
+
flags[name] = true;
|
|
35
|
+
} else if (inline !== undefined) {
|
|
36
|
+
flags[name] = inline;
|
|
37
|
+
} else if (kind === "value") {
|
|
38
|
+
const next = rest[i + 1];
|
|
39
|
+
if (next === undefined || next.startsWith("--")) throw new CouldNotRun("usage", `--${name} needs a value.`);
|
|
40
|
+
flags[name] = next;
|
|
41
|
+
i++;
|
|
42
|
+
} else {
|
|
43
|
+
flags[name] = true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (flags.comments && flags["no-comments"]) throw new CouldNotRun("usage", "Use --comments or --no-comments, not both.");
|
|
47
|
+
if (flags.pointer && flags["no-pointer"]) throw new CouldNotRun("usage", "Use --pointer or --no-pointer, not both.");
|
|
48
|
+
return { command, flags };
|
|
49
|
+
}
|
package/lib/build.mjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// A production build of the site, as its host would make it, into a folder
|
|
3
|
+
// of our own (so the site's dist/ is left alone), and the HTML it wrote. How
|
|
4
|
+
// is the framework's (frameworks/*.mjs `build`).
|
|
5
|
+
// UNBASE_DEPLOY_CONTEXT=production makes the draft-pages kit treat it as Live
|
|
6
|
+
// whatever the environment says.
|
|
7
|
+
// ============================================================================
|
|
8
|
+
|
|
9
|
+
import { spawnSync } from "node:child_process";
|
|
10
|
+
import { existsSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs";
|
|
11
|
+
import { tmpdir } from "node:os";
|
|
12
|
+
import { join, relative, sep } from "node:path";
|
|
13
|
+
|
|
14
|
+
function htmlUnder(dir) {
|
|
15
|
+
const out = new Map();
|
|
16
|
+
const walk = (d) => {
|
|
17
|
+
for (const e of readdirSync(d, { withFileTypes: true })) {
|
|
18
|
+
const p = join(d, e.name);
|
|
19
|
+
if (e.isDirectory()) walk(p);
|
|
20
|
+
else if (e.name.endsWith(".html")) out.set(relative(dir, p).split(sep).join("/"), readFileSync(p, "utf8"));
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
if (existsSync(dir)) walk(dir);
|
|
24
|
+
return out;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Builds the site with its own framework. Returns ok, the build's output (for messages), the HTML
|
|
29
|
+
* per file, and the folder (removed by `dispose`).
|
|
30
|
+
*/
|
|
31
|
+
export function buildSite(site, { label = "build" } = {}) {
|
|
32
|
+
const { name, build } = site.framework;
|
|
33
|
+
const bin = join(site.root, "node_modules", ".bin", process.platform === "win32" ? `${build.bin}.cmd` : build.bin);
|
|
34
|
+
if (!existsSync(bin)) return { ok: false, output: `${name} is not installed in node_modules: install the site's dependencies first.`, html: new Map(), dir: null, dispose() {} };
|
|
35
|
+
const dir = mkdtempSync(join(tmpdir(), `unbase-${label}-`));
|
|
36
|
+
const r = spawnSync(bin, build.args(dir), {
|
|
37
|
+
cwd: site.root,
|
|
38
|
+
encoding: "utf8",
|
|
39
|
+
env: { ...process.env, UNBASE_DEPLOY_CONTEXT: "production", ...build.env, FORCE_COLOR: "0", NO_COLOR: "1" },
|
|
40
|
+
maxBuffer: 256 * 1024 * 1024,
|
|
41
|
+
shell: process.platform === "win32",
|
|
42
|
+
});
|
|
43
|
+
const output = `${r.stdout ?? ""}${r.stderr ?? ""}`;
|
|
44
|
+
let html = htmlUnder(dir);
|
|
45
|
+
if (!html.size) for (const alt of build.output ?? []) if (!html.size) html = htmlUnder(join(site.root, alt));
|
|
46
|
+
return {
|
|
47
|
+
ok: r.status === 0,
|
|
48
|
+
output,
|
|
49
|
+
html,
|
|
50
|
+
dir,
|
|
51
|
+
/** Every JS file of the build, for telling what a script tag loads. */
|
|
52
|
+
file: (p) => {
|
|
53
|
+
try {
|
|
54
|
+
return readFileSync(join(dir, p), "utf8");
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
dispose() {
|
|
60
|
+
rmSync(dir, { recursive: true, force: true });
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The last lines of a build's output, for a message. */
|
|
66
|
+
export const tailOf = (output, n = 15) =>
|
|
67
|
+
output
|
|
68
|
+
.split("\n")
|
|
69
|
+
.filter((l) => l.trim())
|
|
70
|
+
.slice(-n)
|
|
71
|
+
.join("\n");
|
package/lib/compare.mjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// ============================================================================
|
|
2
|
+
// Two builds' pages, compared: what a visitor gets, before and after
|
|
3
|
+
// uninstall. What only the packages put in a page is taken out first (comment
|
|
4
|
+
// mode's markers and its loader scripts), and what differs between any two
|
|
5
|
+
// builds (asset hashes, Astro's scope and island ids, white space, comments).
|
|
6
|
+
// Anything else that differs is a difference.
|
|
7
|
+
// ============================================================================
|
|
8
|
+
|
|
9
|
+
/** comment-modus's loader and the head script that keeps its toolbar across pages carry its name. */
|
|
10
|
+
const OURS = /comment-modus/;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The page without comment mode's scripts: inline ones that carry its name always; a script file
|
|
14
|
+
* that carries it only with `files` (Astro may bundle the loader with the site's own page scripts
|
|
15
|
+
* into one file, which then stays after uninstall).
|
|
16
|
+
*/
|
|
17
|
+
function withoutOurScripts(html, file, files = true) {
|
|
18
|
+
return html.replace(/<script\b([^>]*)>([\s\S]*?)<\/script>/g, (whole, attrs, body) => {
|
|
19
|
+
if (OURS.test(body)) return "";
|
|
20
|
+
const src = /\bsrc=["']?([^"'\s>]+)/.exec(attrs)?.[1];
|
|
21
|
+
if (files && src && src.startsWith("/") && OURS.test(file(src.replace(/^\/+/, "").replace(/[?#].*$/, "")) ?? "")) return "";
|
|
22
|
+
return whole;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A start tag with its attributes in name order: a minifier that sorts them by how often they occur reorders them once data-c is gone. */
|
|
27
|
+
const TAG = /<([a-zA-Z][\w:-]*)((?:\s+[^\s=>\/"']+(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^\s"'>]+))?)*)\s*(\/?)>/g;
|
|
28
|
+
const ATTR = /([^\s=>\/"']+)(?:\s*=\s*("[^"]*"|'[^']*'|[^\s"'>]+))?/g;
|
|
29
|
+
const sortedAttributes = (html) =>
|
|
30
|
+
html.replace(TAG, (whole, tag, attrs, slash) => {
|
|
31
|
+
if (!attrs?.trim()) return whole;
|
|
32
|
+
const list = [...attrs.matchAll(ATTR)].map((m) => (m[2] === undefined ? m[1] : `${m[1]}=${m[2]}`)).sort();
|
|
33
|
+
return `<${tag} ${list.join(" ")}${slash}>`;
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export function normalise(html, file = () => null, files = true) {
|
|
37
|
+
return sortedAttributes(withoutOurScripts(html, file, files).replace(/\sdata-c="[^"]*"/g, ""))
|
|
38
|
+
.replace(/<!--[\s\S]*?-->/g, "")
|
|
39
|
+
.replace(/\sdata-astro-cid-[a-z0-9]+(="[^"]*")?/g, "")
|
|
40
|
+
.replace(/data-astro-cid-[a-z0-9]+/g, "data-astro-cid")
|
|
41
|
+
.replace(/\b(astro-island[^>]*?)\s(uid|prefix)="[^"]*"/g, "$1")
|
|
42
|
+
.replace(/(\/_astro\/[^"'\s),?]*?)\.[A-Za-z0-9_-]{6,}\.([a-z0-9]+)/g, "$1.$2")
|
|
43
|
+
.replace(/[ \t\n\r\f]+/g, " ")
|
|
44
|
+
.replace(/> </g, "><")
|
|
45
|
+
.trim();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const tokens = (html) => html.split(/([<>"'=\s])/).filter(Boolean);
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Whether `a` equals `b` outside the places where two builds of the same code (`b`, `b2`) differ
|
|
52
|
+
* themselves: an id a component makes at random on every render is noise, not a change.
|
|
53
|
+
*/
|
|
54
|
+
function sameBesideNoise(a, b, b2) {
|
|
55
|
+
const ta = tokens(a);
|
|
56
|
+
const tb = tokens(b);
|
|
57
|
+
const t2 = tokens(b2);
|
|
58
|
+
if (ta.length !== tb.length || tb.length !== t2.length) return false;
|
|
59
|
+
for (let i = 0; i < tb.length; i++) if (ta[i] !== tb[i] && tb[i] === t2[i]) return false;
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* The pages that differ, with the first place they differ; [] when none. With `again` (a second
|
|
65
|
+
* build of the after state), what differs between the two after builds counts as noise. `pre`: the
|
|
66
|
+
* framework's own normalising, before the shared one (frameworks/*.mjs `normalise`).
|
|
67
|
+
*/
|
|
68
|
+
export function comparePages(before, after, again = null, pre = (html) => html) {
|
|
69
|
+
const diffs = [];
|
|
70
|
+
const paths = [...new Set([...before.html.keys(), ...after.html.keys()])].sort();
|
|
71
|
+
for (const p of paths) {
|
|
72
|
+
const a = before.html.has(p) ? pre(before.html.get(p)) : undefined;
|
|
73
|
+
const b = after.html.has(p) ? pre(after.html.get(p)) : undefined;
|
|
74
|
+
if (a === undefined || b === undefined) {
|
|
75
|
+
diffs.push({ page: p, change: a === undefined ? "only after" : "only before" });
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
// The loader's script file either goes (it held only the loader) or stays (bundled with the site's own page scripts).
|
|
79
|
+
const nb = normalise(b, after.file);
|
|
80
|
+
const candidates = [normalise(a, before.file, false), normalise(a, before.file)];
|
|
81
|
+
if (candidates.includes(nb)) continue;
|
|
82
|
+
const b2 = again?.html.has(p) ? pre(again.html.get(p)) : undefined;
|
|
83
|
+
if (b2 !== undefined) {
|
|
84
|
+
const n2 = normalise(b2, again.file);
|
|
85
|
+
if (candidates.some((na) => sameBesideNoise(na, nb, n2))) continue;
|
|
86
|
+
}
|
|
87
|
+
const na = candidates[1];
|
|
88
|
+
let i = 0;
|
|
89
|
+
while (i < na.length && na[i] === nb[i]) i++;
|
|
90
|
+
diffs.push({ page: p, change: "changed", before: na.slice(Math.max(0, i - 60), i + 120), after: nb.slice(Math.max(0, i - 60), i + 120) });
|
|
91
|
+
}
|
|
92
|
+
return { pages: paths.length, diffs };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Whether a page carries comment mode's loader (inline, or in a script file it loads). */
|
|
96
|
+
export const hasOurScript = (html, file = () => null) => withoutOurScripts(html, file) !== html;
|