#!/usr/bin/env node /** * Compare this repository's engines with a checkout of the course site. * * pixi run engines-diff ../course/static * * is the course repository's static/ directory. This repository owns * the engines and the course copies from here (pixi run engines-export), so * this shows what the course has not taken yet. For each engine module (pseudo, * asm86, shared) it compares the file the course would receive (src/engines/ * as written, or dist/engines/ for a module written in TypeScript) with the * course's copy, and each stylesheet in styles/course/ with the course's; * prints whether they are the same, differ only in whitespace and line * endings, or differ in content (with the number of changed lines, whitespace * ignored), and lists files only one side has. It changes nothing. */ import fs from 'node:fs'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; import { engineModules, ENGINE_DIRS } from './engines-files.mjs'; 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 '); process.exit(2); } 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 = []; const strip = (t) => t.replace(/\n\/\/# sourceMappingURL=.*\n?$/, '\n'); const mine = engineModules(ROOT); for (const m of mine) { const theirs = path.join(dir, 'js', m.dir, m.file); if (!fs.existsSync(m.ours)) rows.push([m.label, 'not built: run pixi run build']); else if (!fs.existsSync(theirs)) rows.push([m.label, 'only here']); else { const a = strip(fs.readFileSync(m.ours, 'utf8')), b = fs.readFileSync(theirs, 'utf8'); const r = a === b ? { kind: 'same' } : (changed(squash(a), squash(b)) === 0 ? { kind: 'whitespace' } : { kind: 'content', n: changed(squash(a), squash(b)) }); rows.push([m.label, r.kind === 'content' ? `differs: ${r.n} lines` : r.kind === 'whitespace' ? 'whitespace only' : 'same']); } } for (const d of ENGINE_DIRS) { const c = path.join(dir, 'js', d); if (!fs.existsSync(c)) continue; for (const f of fs.readdirSync(c).filter((x) => x.endsWith('.js')).sort()) { if (!mine.some((m) => m.dir === d && m.file === f)) rows.push([`js/${d}/${f}`, 'only in the course']); } } 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`);