diff --git a/scripts/legacy/split-index-html.js b/scripts/legacy/split-index-html.js
index ee069c5..4a47af3 100644
--- a/scripts/legacy/split-index-html.js
+++ b/scripts/legacy/split-index-html.js
@@ -12,7 +12,7 @@
const fs = require('node:fs');
const path = require('node:path');
-// [partial, first line of it]. Each regex must match a line that comes after the previous cut.
+// [partial, first line of it]. Each regex is looked for after the previous cut.
const CSS = [
['styles/00-base.css', /^\/\* Layout: full-bleed/],
['styles/10-hud.css', /^\/\* HUD \*\/$/],
@@ -134,18 +134,21 @@ function liftOverComments(lines, i, floor) {
// Cuts lines[from, to) into [file, text] pairs by the spec.
function cut(lines, from, to, spec, what) {
- const starts = [];
+ // Older versions lack some later sections: a part whose marker is missing is left out, and its code (if
+ // any) stays in the part before it. The first part of each section is required.
+ const found = [];
let at = from;
for (const [file, re] of spec) {
let i = at;
while (i < to && !re.test(lines[i])) i++;
- if (i >= to) throw new Error(`${what}: no line matches ${re} (for ${file}) after line ${at + 1}`);
- const s = starts.length ? liftOverComments(lines, i, starts[starts.length - 1]) : from;
- starts.push(s);
+ if (i >= to) {
+ if (!found.length) throw new Error(`${what}: no line matches ${re} (for ${file})`);
+ continue;
+ }
+ found.push([file, found.length ? liftOverComments(lines, i, found[found.length - 1][1]) : from]);
at = i + 1;
}
- if (starts[0] !== from) throw new Error(`${what}: unexpected text before ${spec[0][0]}`);
- return spec.map(([file], k) => [file, lines.slice(starts[k], k + 1 < starts.length ? starts[k + 1] : to).map((l) => l + '\n').join('')]);
+ return found.map(([file, s], k) => [file, lines.slice(s, k + 1 < found.length ? found[k + 1][1] : to).map((l) => l + '\n').join('')]);
}
function split(html) {
diff --git a/scripts/legacy/transforms.js b/scripts/legacy/transforms.js
index ecff6af..08dbf38 100644
--- a/scripts/legacy/transforms.js
+++ b/scripts/legacy/transforms.js
@@ -31,13 +31,11 @@ function splitActions(text) {
if (open < 0 || close < open) throw new Error('90-actions: no `const ACT={ … };` block');
if (lines.slice(close + 1).some((l) => l.trim())) throw new Error('90-actions: code after the ACT block');
const keyAt = (k) => lines.findIndex((l, i) => i > open && i < close && l.startsWith(` ${k}:`));
- const starts = ACT_GROUPS.map(([file, k]) => {
- const i = keyAt(k);
- if (i < 0) throw new Error(`90-actions: action ${k} not found (for ${file})`);
- return i;
- });
+ // A group whose first action does not exist (yet, in an older version) is left out.
+ const groups = ACT_GROUPS.map(([file, k], g) => [file, g ? keyAt(k) : 0]).filter(([, i]) => i >= 0);
+ const starts = groups.map(([, i]) => i);
if (starts.some((s, i) => i && s <= starts[i - 1])) throw new Error('90-actions: actions are out of the expected order');
- return ACT_GROUPS.map(([file], g) => {
+ return groups.map(([file], g) => {
const from = g ? starts[g] : 0, to = g + 1 < starts.length ? starts[g + 1] : close;
const body = lines.slice(from, to);
const out = g ? ['Object.assign(ACT,{', ...body, '});'] : [...body, '};'];