Fewer merge conflicts: actions by area, one city per line, sorted dictionaries
- ACT is split by area into game/90-actions/*.js (const ACT in the first file, Object.assign in the rest) - CITY_LL, CITY_NUDGE and LANDS_LL have one entry per line; the id-keyed ones are sorted - site/i18n/*.js are sorted, one entry per line; i18n-check enforces it and --fix (npm run i18n:fix) restores it; scripts/i18n-add.js adds, removes and renames entries in place - scripts/legacy/port-branch.js moves a branch's site/index.html edits into src/ during a merge with main - perf workflow and PR template build the base page with scripts/lib/page.js
This commit is contained in:
parent
67a40942b4
commit
c64b139a86
27 changed files with 9582 additions and 8971 deletions
39
scripts/i18n-add.js
Normal file
39
scripts/i18n-add.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
#!/usr/bin/env node
|
||||
// Adds, changes, renames or removes dictionary entries, keeping every site/i18n/*.js sorted (scripts/lib/dict.js).
|
||||
//
|
||||
// node scripts/i18n-add.js changes.json {"it": {"English": "Italiano", "Old text": null}, "pt": {…}}
|
||||
// a string adds or replaces the entry, null removes it
|
||||
// node scripts/i18n-add.js - the same JSON from stdin
|
||||
// node scripts/i18n-add.js --rename "Old English" "New English" move a key in every dictionary
|
||||
const fs = require('node:fs');
|
||||
const D = require('./lib/dict');
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
if (args[0] === '--rename') {
|
||||
const [, from, to] = args;
|
||||
if (!from || !to) { console.error('usage: i18n-add.js --rename "Old English" "New English"'); process.exit(2); }
|
||||
for (const lang of D.langs()) {
|
||||
const { header, dict } = D.read(lang);
|
||||
if (!(from in dict)) { console.log(`${lang}: no entry for ${JSON.stringify(from)}`); continue; }
|
||||
if (to in dict && to !== from) { console.error(`${lang}: already has ${JSON.stringify(to)}`); process.exit(1); }
|
||||
dict[to] = dict[from];
|
||||
if (to !== from) delete dict[from];
|
||||
D.write(lang, dict, header);
|
||||
console.log(`${lang}: renamed`);
|
||||
}
|
||||
process.exit(0);
|
||||
}
|
||||
if (args.length !== 1) { console.error('usage: i18n-add.js changes.json | - | --rename OLD NEW'); process.exit(2); }
|
||||
const changes = JSON.parse(fs.readFileSync(args[0] === '-' ? 0 : args[0], 'utf8'));
|
||||
for (const [lang, entries] of Object.entries(changes)) {
|
||||
if (!D.langs().includes(lang)) { console.error(`no dictionary site/i18n/${lang}.js`); process.exit(1); }
|
||||
const { header, dict } = D.read(lang);
|
||||
let add = 0, del = 0;
|
||||
for (const [en, xx] of Object.entries(entries)) {
|
||||
if (xx === null) { if (en in dict) { delete dict[en]; del++; } continue; }
|
||||
if (typeof xx !== 'string' || !xx.trim()) { console.error(`${lang}: empty translation for ${JSON.stringify(en)}`); process.exit(1); }
|
||||
dict[en] = xx; add++;
|
||||
}
|
||||
D.write(lang, dict, header);
|
||||
console.log(`${lang}: ${add} added or changed, ${del} removed`);
|
||||
}
|
||||
|
|
@ -2,7 +2,8 @@
|
|||
// Checks a translation against the English text in the game page (assembled from src/): every tr()/trn()/N_() literal and
|
||||
// every data-i18n text, aria-label and title in the static markup should have an entry, no stale entry
|
||||
// should linger, and each translation must keep the {placeholders} and HTML tags of its English key.
|
||||
// Usage: node scripts/i18n-check.js [lang ...] [--summary FILE] (default: every site/i18n/*.js)
|
||||
// Usage: node scripts/i18n-check.js [lang ...] [--summary FILE] [--fix] (default: every site/i18n/*.js)
|
||||
// Dictionaries must also be sorted, one entry per line (scripts/lib/dict.js); --fix sorts them.
|
||||
// It also checks that each language is offered in the game and cached for offline play, and with --summary
|
||||
// appends a Markdown coverage table (the CI job writes it to the run's summary page).
|
||||
const fs = require('node:fs');
|
||||
|
|
@ -65,6 +66,7 @@ function registration(lang) {
|
|||
const m = html.match(/SOT_LANGS=\{([^}]*)\}/);
|
||||
if (!m || !new RegExp(String.raw`(^|[,{\s])${lang}\s*:`).test(m[1])) out.push('not in the language list (SOT_LANGS in src/index.html)');
|
||||
if (!sw.includes(`/i18n/${lang}.js`)) out.push('not cached for offline play (CORE in site/sw.js)');
|
||||
if (!require('./lib/dict').tidy(lang)) out.push('not sorted one entry per line: run npm run i18n:fix');
|
||||
return out;
|
||||
}
|
||||
|
||||
|
|
@ -72,6 +74,12 @@ if (require.main === module) {
|
|||
const args = process.argv.slice(2);
|
||||
const si = args.indexOf('--summary');
|
||||
const summary = si >= 0 ? args.splice(si, 2)[1] : null; // append a Markdown table here (CI: $GITHUB_STEP_SUMMARY)
|
||||
const fi = args.indexOf('--fix');
|
||||
if (fi >= 0) {
|
||||
args.splice(fi, 1);
|
||||
const D = require('./lib/dict');
|
||||
for (const lang of args.length ? args : D.langs()) { const { header, dict } = D.read(lang); D.write(lang, dict, header); }
|
||||
}
|
||||
const keys = sourceKeys(page());
|
||||
const files = (args.length ? args.map((l) => `${l}.js`) : fs.readdirSync(path.join(SITE, 'i18n')))
|
||||
.map((f) => path.join(SITE, 'i18n', f));
|
||||
|
|
|
|||
96
scripts/legacy/port-branch.js
Normal file
96
scripts/legacy/port-branch.js
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
#!/usr/bin/env node
|
||||
// Carries a branch that still edits site/index.html (started before the page was split into src/) over
|
||||
// to the src/ layout. Run it in the middle of merging main into that branch:
|
||||
//
|
||||
// git checkout my-branch
|
||||
// git merge origin/main # stops: site/index.html was deleted on main
|
||||
// node scripts/legacy/port-branch.js # moves your edits into src/, merges the dictionaries
|
||||
// git status # fix any conflict markers it reports, then
|
||||
// git commit
|
||||
//
|
||||
// How: your branch's site/index.html and the one you started from are both split with the same rules
|
||||
// that made src/ (split-index-html.js + transforms.js), and each partial gets a three-way merge
|
||||
// (git merge-file) into the src/ file from main. Your dictionary changes (site/i18n/*.js) are applied
|
||||
// entry by entry onto main's sorted dictionaries, so appended lines never conflict.
|
||||
//
|
||||
// Options: --base REF (the pre-split commit your page started from; default: merge-base of HEAD and
|
||||
// MERGE_HEAD) and --theirs REF (your branch; default HEAD).
|
||||
const fs = require('node:fs');
|
||||
const os = require('node:os');
|
||||
const path = require('node:path');
|
||||
const { execFileSync, spawnSync } = require('node:child_process');
|
||||
const { split } = require('./split-index-html');
|
||||
const { transform } = require('./transforms');
|
||||
const D = require('../lib/dict');
|
||||
|
||||
const ROOT = path.join(__dirname, '..', '..');
|
||||
const args = process.argv.slice(2);
|
||||
const opt = (k, d) => { const i = args.indexOf(k); return i >= 0 ? args[i + 1] : d; };
|
||||
const git = (...a) => execFileSync('git', a, { cwd: ROOT, encoding: 'utf8', maxBuffer: 1 << 28, stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
const show = (ref, p) => { try { return git('show', `${ref}:${p}`); } catch { return null; } };
|
||||
const die = (m) => { console.error(`port-branch: ${m}`); process.exit(1); };
|
||||
|
||||
let mergeHead = null;
|
||||
try { mergeHead = git('rev-parse', '-q', '--verify', 'MERGE_HEAD').trim(); } catch {}
|
||||
const theirs = opt('--theirs', 'HEAD');
|
||||
const base = opt('--base', null) || (mergeHead ? git('merge-base', theirs, mergeHead).trim() : null);
|
||||
if (!base) die('no merge in progress: run `git merge origin/main` first, or pass --base REF');
|
||||
if (!fs.existsSync(path.join(ROOT, 'src', 'index.html'))) die('src/ is missing: merge main (which has src/) first');
|
||||
|
||||
const basePage = show(base, 'site/index.html'), theirPage = show(theirs, 'site/index.html');
|
||||
if (!basePage) die(`${base} has no site/index.html; is it from before the split?`);
|
||||
if (!theirPage) die(`${theirs} has no site/index.html; nothing to port`);
|
||||
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'tubes-port-'));
|
||||
const parts = (html, label) => {
|
||||
try { return new Map(transform(split(html))); }
|
||||
catch (e) { die(`could not split ${label}'s site/index.html the way src/ was made: ${e.message}`); }
|
||||
};
|
||||
const B = parts(basePage, 'the base'), T = parts(theirPage, 'your branch');
|
||||
let changed = 0;
|
||||
const conflicts = [];
|
||||
for (const [file, t] of T) {
|
||||
const b = B.get(file);
|
||||
if (b === t) continue; // you did not touch this part
|
||||
const ours = path.join(ROOT, 'src', file);
|
||||
if (!fs.existsSync(ours)) { conflicts.push(`${file} (no longer exists on main; apply your change by hand)`); continue; }
|
||||
const fb = path.join(tmp, 'base'), ft = path.join(tmp, 'theirs');
|
||||
fs.writeFileSync(fb, b || '');
|
||||
fs.writeFileSync(ft, t);
|
||||
const r = spawnSync('git', ['merge-file', '-L', 'main', '-L', 'base', '-L', 'your branch', ours, fb, ft], { cwd: ROOT });
|
||||
changed++;
|
||||
if (r.status !== 0) conflicts.push(`src/${file}`);
|
||||
}
|
||||
|
||||
// Dictionaries: your additions, edits and removals, applied onto main's sorted files.
|
||||
for (const lang of D.langs()) {
|
||||
const rel = `site/i18n/${lang}.js`;
|
||||
const tb = show(base, rel), tt = show(theirs, rel);
|
||||
if (!tt || tb === tt) continue;
|
||||
const db = tb ? D.parse(tb, lang) : {}, dt = D.parse(tt, lang);
|
||||
const main = mergeHead ? show(mergeHead, rel) : null;
|
||||
// During a merge the working file may hold conflict markers, so start from main's own copy.
|
||||
const { header, dict } = main ? { header: main.slice(0, main.indexOf('(window.I18N')), dict: D.parse(main, lang) } : D.read(lang);
|
||||
let n = 0;
|
||||
for (const k of new Set([...Object.keys(db), ...Object.keys(dt)])) {
|
||||
if (db[k] === dt[k]) continue;
|
||||
if (k in dict && db[k] !== dict[k] && dict[k] !== dt[k] && k in dt) conflicts.push(`${rel}: ${JSON.stringify(k)} changed on both sides (kept yours)`);
|
||||
if (k in dt) dict[k] = dt[k]; else delete dict[k];
|
||||
n++;
|
||||
}
|
||||
D.write(lang, dict, header);
|
||||
git('add', rel);
|
||||
console.log(`${rel}: ${n} of your entries applied`);
|
||||
}
|
||||
|
||||
if (fs.existsSync(path.join(ROOT, 'site', 'index.html'))) fs.rmSync(path.join(ROOT, 'site', 'index.html'));
|
||||
try { git('rm', '-q', '--cached', '--ignore-unmatch', 'site/index.html'); } catch {}
|
||||
fs.rmSync(tmp, { recursive: true, force: true });
|
||||
console.log(`Ported your site/index.html changes into ${changed} file(s) under src/.`);
|
||||
if (conflicts.length) {
|
||||
console.log('Resolve these by hand (look for <<<<<<< markers), then git add them:');
|
||||
for (const c of conflicts) console.log(` ${c}`);
|
||||
process.exit(1);
|
||||
}
|
||||
git('add', 'src');
|
||||
console.log('No conflicts. Run npm test, then git commit.');
|
||||
|
|
@ -1,2 +1,98 @@
|
|||
// Tidying applied after the split (filled in by the next commit).
|
||||
module.exports = { transform: (parts) => parts };
|
||||
// Tidying applied right after the split, to cut down merge conflicts. Each one keeps behaviour the same
|
||||
// and only changes where code sits or how a literal is laid out. They run as part of
|
||||
// split-index-html.js, so work from older branches lines up with src/ when it is ported.
|
||||
//
|
||||
// 1. The ACT map (every player action) becomes one file per area under game/90-actions/: the first file
|
||||
// declares `const ACT={…}` and the others add to it with Object.assign. Two people adding actions in
|
||||
// different areas no longer touch the same lines.
|
||||
// 2. Coordinate tables that lived on one very long line (CITY_LL, CITY_NUDGE, LANDS_LL) get one entry per
|
||||
// line, CITY_LL and CITY_NUDGE sorted by id: adding a city no longer edits a 4,000-character line
|
||||
// that everyone else is editing too.
|
||||
|
||||
// [file, first action in it]. A group runs until the next group's first action.
|
||||
const ACT_GROUPS = [
|
||||
['game/90-actions/10-interface.js', 'speed'],
|
||||
['game/90-actions/20-pops-transit-ix.js', 'buildPop'],
|
||||
['game/90-actions/30-route-script-and-tools.js', 'teEdit'],
|
||||
['game/90-actions/40-sites-peering-caches.js', 'addSite'],
|
||||
['game/90-actions/50-customers.js', 'advertise'],
|
||||
['game/90-actions/60-links.js', 'upLink'],
|
||||
['game/90-actions/70-dashboard-lists.js', 'custGo'],
|
||||
['game/90-actions/80-security-and-china.js', 'applyCn'],
|
||||
['game/90-actions/85-routing-and-routers.js', 'via'],
|
||||
['game/90-actions/87-fiber-and-hardware.js', 'dfMode'],
|
||||
['game/90-actions/89-game-and-settings.js', 'randomName'],
|
||||
];
|
||||
|
||||
function splitActions(text) {
|
||||
const lines = text.split('\n');
|
||||
if (lines[lines.length - 1] === '') lines.pop();
|
||||
const open = lines.indexOf('const ACT={'), close = lines.lastIndexOf('};');
|
||||
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;
|
||||
});
|
||||
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) => {
|
||||
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, '};'];
|
||||
return [file, out.join('\n') + '\n'];
|
||||
});
|
||||
}
|
||||
|
||||
// The top-level entries of an array or object literal source "a,b,c" (no brackets around it).
|
||||
function entries(src) {
|
||||
const out = [];
|
||||
let depth = 0, q = null, start = 0;
|
||||
for (let i = 0; i < src.length; i++) {
|
||||
const c = src[i];
|
||||
if (q) { if (c === '\\') i++; else if (c === q) q = null; continue; }
|
||||
if (c === '"' || c === "'" || c === '`') q = c;
|
||||
else if ('[{('.includes(c)) depth++;
|
||||
else if (']})'.includes(c)) depth--;
|
||||
else if (c === ',' && depth === 0) { out.push(src.slice(start, i).trim()); start = i + 1; }
|
||||
}
|
||||
if (depth || q) throw new Error('unbalanced literal');
|
||||
const last = src.slice(start).trim();
|
||||
if (last) out.push(last);
|
||||
return out;
|
||||
}
|
||||
|
||||
const idOf = (e) => e.match(/^["']?([\w$]+)["']?\s*:/)[1];
|
||||
const byId = (a, b) => (idOf(a) < idOf(b) ? -1 : idOf(a) > idOf(b) ? 1 : 0);
|
||||
|
||||
// `HEAD{entries}TAIL` on one line → HEAD, one entry per line, TAIL. Lines that don't match stay as they are.
|
||||
function oneLine(text, re, { sort = false, indent = ' ' } = {}) {
|
||||
return text.split('\n').map((l) => {
|
||||
const m = re.exec(l);
|
||||
if (!m) return l;
|
||||
let es = entries(m[2]);
|
||||
if (sort) {
|
||||
// Sorting is only safe without duplicate keys (with duplicates, the last one wins).
|
||||
const ids = es.map(idOf);
|
||||
if (new Set(ids).size !== ids.length) throw new Error(`duplicate keys in ${m[1]}`);
|
||||
es = es.slice().sort(byId);
|
||||
}
|
||||
return [m[1], ...es.map((e) => indent + e + ','), m[3]].join('\n');
|
||||
}).join('\n');
|
||||
}
|
||||
|
||||
function transform(parts) {
|
||||
const out = [];
|
||||
for (const [file, text] of parts) {
|
||||
if (file === 'game/90-actions/00-actions.js') out.push(...splitActions(text));
|
||||
else if (file === 'game/20-world/40-city-coords.js') out.push([file, oneLine(text, /^(const CITY_LL=\{)(.*)(\};)$/, { sort: true })]);
|
||||
else if (file === 'game/20-world/50-cities.js') out.push([file, oneLine(text, /^(const CITY_NUDGE=\{)(.*)(\};)$/, { sort: true })]);
|
||||
else if (file === 'game/20-world/20-landmasses.js') {
|
||||
out.push([file, oneLine(oneLine(text, /^(const LANDS_LL=\[)(.*)(\];)$/), /^(LANDS_LL\.push\()(.*)(\);)$/)]);
|
||||
} else out.push([file, text]);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
module.exports = { transform, entries };
|
||||
|
|
|
|||
46
scripts/lib/dict.js
Normal file
46
scripts/lib/dict.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// Reads and writes the translation dictionaries in site/i18n/<lang>.js.
|
||||
//
|
||||
// Every dictionary is kept in one canonical layout: the header comment, then one `"English":"Translation",`
|
||||
// entry per line, sorted by the English text. Sorted entries spread new strings across the file instead of
|
||||
// piling them up at the end, so two pull requests that each add strings rarely touch the same lines.
|
||||
// `npm run i18n:check` reports a dictionary that is out of order; `npm run i18n:fix` puts it back.
|
||||
const fs = require('node:fs');
|
||||
const path = require('node:path');
|
||||
const vm = require('node:vm');
|
||||
|
||||
const DIR = path.join(__dirname, '..', '..', 'site', 'i18n');
|
||||
const fileOf = (lang) => path.join(DIR, `${lang}.js`);
|
||||
const langs = () => fs.readdirSync(DIR).filter((f) => f.endsWith('.js')).map((f) => path.basename(f, '.js')).sort();
|
||||
|
||||
// Plain code-unit order: the same on every machine, whatever its locale.
|
||||
const cmp = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
|
||||
|
||||
function parse(text, lang) {
|
||||
const window = {};
|
||||
vm.runInNewContext(text, { window });
|
||||
return (window.I18N || {})[lang] || {};
|
||||
}
|
||||
|
||||
function read(lang) {
|
||||
const text = fs.readFileSync(fileOf(lang), 'utf8');
|
||||
const open = text.indexOf(`(window.I18N=window.I18N||{}).${lang}={`);
|
||||
if (open < 0) throw new Error(`${lang}.js: missing the (window.I18N=window.I18N||{}).${lang}={ line`);
|
||||
return { text, header: text.slice(0, open), dict: parse(text, lang) };
|
||||
}
|
||||
|
||||
function format(lang, header, dict) {
|
||||
const keys = Object.keys(dict).sort(cmp);
|
||||
return `${header}(window.I18N=window.I18N||{}).${lang}={\n${keys.map((k) => `${JSON.stringify(k)}:${JSON.stringify(dict[k])},\n`).join('')}};\n`;
|
||||
}
|
||||
|
||||
// True when the file is already in the canonical layout.
|
||||
function tidy(lang) {
|
||||
const { text, header, dict } = read(lang);
|
||||
return text === format(lang, header, dict);
|
||||
}
|
||||
|
||||
function write(lang, dict, header = read(lang).header) {
|
||||
fs.writeFileSync(fileOf(lang), format(lang, header, dict));
|
||||
}
|
||||
|
||||
module.exports = { DIR, fileOf, langs, read, write, format, tidy, parse, cmp };
|
||||
|
|
@ -74,7 +74,7 @@ function locate(map, line) {
|
|||
|
||||
// The page at a git ref: that commit's src/ through its own assembler, or site/index.html before the split.
|
||||
function assembleRef(ref, cwd = ROOT) {
|
||||
const git = (...a) => execFileSync('git', a, { cwd, encoding: 'utf8', maxBuffer: 1 << 28 });
|
||||
const git = (...a) => execFileSync('git', a, { cwd, encoding: 'utf8', maxBuffer: 1 << 28, stdio: ['ignore', 'pipe', 'pipe'] });
|
||||
const has = (p) => { try { git('cat-file', '-e', `${ref}:${p}`); return true; } catch { return false; } };
|
||||
if (!has('src/index.html')) return git('show', `${ref}:site/index.html`);
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'tubes-ref-'));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue