Files
pplus-core/scripts/engines-diff.mjs
T
2026-10-05 03:03:38 +00:00

72 lines
3.4 KiB
JavaScript

#!/usr/bin/env node
/**
* Compare the vendored course code with a checkout of the course site.
*
* pixi run engines-diff ../course/static
*
* <static> is the course repository's static/ directory. For each engine
* module in src/engines/ (pseudo, asm86, shared) and each stylesheet in
* styles/course/, prints whether it is the same as the course's copy, differs
* only in whitespace and line endings, or differs in content (with the number
* of changed lines, whitespace ignored), and lists files only one side has.
* It changes nothing. docs/engines.md says what to do with the answer.
*/
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const ROOT = fileURLToPath(new URL('..', import.meta.url));
const dir = process.argv[2];
if (!dir || !fs.existsSync(path.join(dir, 'js'))) {
console.error('usage: pixi run engines-diff <the course repository\'s static/ directory>');
process.exit(2);
}
const PAIRS = [
...['pseudo', 'asm86', 'shared'].map((f) => [`src/engines/${f}`, `js/${f}`, '.js']),
];
const CSS = { 'tokens.css': 'css/base/tokens.css', 'kit.css': 'css/widgets/kit.css', 'asm86.css': 'css/widgets/asm86.css' };
const squash = (s) => s.replace(/\r/g, '').split('\n').map((l) => l.replace(/\s+/g, ' ').trim()).filter(Boolean);
/** Lines changed, whitespace ignored: the size of the symmetric difference of the two line multisets. */
function changed(a, b) {
const count = new Map();
for (const l of a) count.set(l, (count.get(l) || 0) + 1);
for (const l of b) count.set(l, (count.get(l) || 0) - 1);
let n = 0;
for (const v of count.values()) n += Math.abs(v);
return n;
}
function compare(ours, theirs) {
const a = fs.readFileSync(ours, 'utf8'), b = fs.readFileSync(theirs, 'utf8');
if (a === b) return { kind: 'same' };
const n = changed(squash(a), squash(b));
return n === 0 ? { kind: 'whitespace' } : { kind: 'content', n };
}
const rows = [];
for (const [mine, course, ext] of PAIRS) {
const m = path.join(ROOT, mine), c = path.join(dir, course);
const names = new Set([...(fs.existsSync(m) ? fs.readdirSync(m) : []), ...(fs.existsSync(c) ? fs.readdirSync(c) : [])].filter((f) => f.endsWith(ext)));
for (const f of [...names].sort()) {
const a = path.join(m, f), b = path.join(c, f);
const label = `${mine}/${f}`;
if (!fs.existsSync(a)) rows.push([label, 'only in the course']);
else if (!fs.existsSync(b)) rows.push([label, 'only here']);
else { const r = compare(a, b); rows.push([label, r.kind === 'content' ? `differs: ${r.n} lines` : r.kind === 'whitespace' ? 'whitespace only' : 'same']); }
}
}
for (const [f, course] of Object.entries(CSS)) {
const a = path.join(ROOT, 'styles/course', f), b = path.join(dir, course);
const label = `styles/course/${f}`;
if (!fs.existsSync(b)) rows.push([label, 'missing in the course']);
else { const r = compare(a, b); rows.push([label, r.kind === 'content' ? `differs: ${r.n} lines` : r.kind === 'whitespace' ? 'whitespace only' : 'same']); }
}
const w = Math.max(...rows.map((r) => r[0].length));
for (const [f, v] of rows) console.log(`${f.padEnd(w)} ${v}`);
const content = rows.filter((r) => r[1].startsWith('differs')).length;
console.log(`\n${rows.length} files: ${rows.filter((r) => r[1] === 'same').length} same, ${rows.filter((r) => r[1] === 'whitespace only').length} whitespace only, ${content} differ in content`);