#!/usr/bin/env node /** * check-consistency.mjs * * Plain Node script (no dependency, no test framework) that proves README.md * and the site content in public/src/data carry the same set of sections and * principles, and that docs/section-principle-map.md maps every one of them to * a README heading and a data key that actually exist. * * Where the two id sets come from: * - README.md: the `` HTML comment anchors. * - public/src/data/manifesto.js: the `sectionMap` export, which spreads the * four principle rows in from `principleSections`; the module is imported, * so the spread is resolved by the language rather than re-parsed here. * * The run fails (exit 1) when: * - an id exists on only one side; * - an id that exists on either side has no row in the map; * - a map row names an id that is on neither side; * - a map row names a README heading or a data key that does not exist. * * Two things are reported but never fail the run, because neither is an id * set: `framework.subsections` (a content list; the run reports how many names * it holds and how many of them match a README heading) and the map's anchor * line numbers (they move whenever an edit above them adds a line). * * Usage: * node scripts/check-consistency.mjs * node scripts/check-consistency.mjs [--readme ] [--manifesto ] * [--assessment ] [--map ] * * Options: * --readme README to read the id anchors from (default: README.md) * --manifesto data file holding `sectionMap` (default: public/src/data/manifesto.js) * --assessment data file holding the assessment keys (default: public/src/data/assessment.js) * --map mapping document (default: docs/section-principle-map.md) * * Exit codes: 0 = the two id sets match and every map row resolves, * 1 = at least one of the checks above fails, * 2 = usage or I/O error. */ import { existsSync, readFileSync } from 'node:fs' import { dirname, join, relative, resolve } from 'node:path' import { fileURLToPath, pathToFileURL } from 'node:url' const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url)) const REPO_ROOT = resolve(SCRIPT_DIR, '..') const DEFAULTS = { readme: join(REPO_ROOT, 'README.md'), manifesto: join(REPO_ROOT, 'public', 'src', 'data', 'manifesto.js'), assessment: join(REPO_ROOT, 'public', 'src', 'data', 'assessment.js'), map: join(REPO_ROOT, 'docs', 'section-principle-map.md') } const usage = [ 'Usage: node scripts/check-consistency.mjs [--readme ] [--manifesto ]', ' [--assessment ] [--map ]', '', 'Compares the README id anchors and the manifesto sectionMap against the rows of', 'docs/section-principle-map.md.', '', 'Options:', ' --readme README to read the id anchors from (default: README.md)', ' --manifesto data file holding sectionMap (default: public/src/data/manifesto.js)', ' --assessment data file holding the assessment keys (default: public/src/data/assessment.js)', ' --map mapping document (default: docs/section-principle-map.md)', '', 'Exit codes: 0 = sets match and every map row resolves, 1 = a check fails, 2 = usage or I/O error.' ].join('\n') const ANCHOR_RE = //g const HEADING_RE = /^(#{1,6})\s+(.+?)\s*$/ const CODE_SPAN_RE = /`([^`]+)`/ // The map table is the one table whose header carries all four columns; the // other tables in the document (the five situations, the out-of-scope // namespaces) have a different column set and are not map rows. const MAP_COLUMNS = { id: /^id$/i, heading: /readme heading/i, anchor: /anchor line/i, dataKey: /site data key/i } /* ------------------------------------------------------------------ options */ function readOptions (argv) { const takesValue = new Map([ ['--readme', 'readme'], ['--manifesto', 'manifesto'], ['--assessment', 'assessment'], ['--map', 'map'] ]) const options = { ...DEFAULTS } for (let index = 0; index < argv.length; index++) { const argument = argv[index] if (argument === '--help' || argument === '-h') { options.help = true continue } if (!argument.startsWith('-')) throw new Error(`unexpected argument: ${argument}`) const equals = argument.indexOf('=') const name = equals === -1 ? argument : argument.slice(0, equals) if (!takesValue.has(name)) throw new Error(`unknown option: ${argument}`) let value if (equals === -1) { value = argv[index + 1] index += 1 if (value === undefined || value.startsWith('-')) throw new Error(`${name} needs a value`) } else { value = argument.slice(equals + 1) if (value === '') throw new Error(`${name} needs a value`) } options[takesValue.get(name)] = resolve(value) } return options } /* ------------------------------------------------------------ README side */ /** The `` anchors, with the line each one sits on. */ function readReadmeIds (markdown) { const ids = [] const lines = markdown.split('\n') for (let index = 0; index < lines.length; index++) { ANCHOR_RE.lastIndex = 0 let match = ANCHOR_RE.exec(lines[index]) while (match !== null) { ids.push({ id: match[1], line: index + 1 }) match = ANCHOR_RE.exec(lines[index]) } } return ids } /** The Markdown headings, with level, text and line. */ function readReadmeHeadings (markdown) { const headings = [] const lines = markdown.split('\n') for (let index = 0; index < lines.length; index++) { const match = HEADING_RE.exec(lines[index]) if (match === null) continue headings.push({ level: match[1].length, text: match[2].replace(/\s+#+$/, '').trim(), line: index + 1 }) } return headings } /* -------------------------------------------------------------- data side */ async function loadDataModule (file) { if (!existsSync(file)) throw new Error(`data file not found: ${file}`) return import(pathToFileURL(file).href) } /** The `sectionMap` export, as `{ id, heading, dataKey }` rows. */ function readSectionMap (namespace, file) { const sectionMap = namespace.sectionMap if (!Array.isArray(sectionMap)) { throw new Error(`${file} has no sectionMap array export`) } return sectionMap.map((row, index) => { if (row === null || typeof row !== 'object' || typeof row.id !== 'string' || row.id.trim() === '') { throw new Error(`${file} sectionMap[${index}] has no string id`) } return { id: row.id.trim(), heading: typeof row.heading === 'string' ? row.heading : '', dataKey: typeof row.dataKey === 'string' ? row.dataKey : '' } }) } /** Resolve a dotted key such as `principles.focus` against a module namespace. */ function resolveDataKey (key, modules) { for (const [label, namespace] of modules) { let value = namespace let found = true for (const part of key.split('.')) { if (value === null || typeof value !== 'object' || !(part in value)) { found = false break } value = value[part] } if (found) return { file: label, value } } return null } /* ------------------------------------------------------------- map document */ function splitCells (line) { return line .replace(/^\|/, '') .replace(/\|$/, '') .split('|') .map((cell) => cell.trim()) } /** The first code span of a cell, which is how the map quotes ids and keys. */ function firstCodeSpan (cell) { const match = CODE_SPAN_RE.exec(cell) return match === null ? null : match[1].trim() } function readMapRows (markdown) { const lines = markdown.split('\n') const rows = [] let columns = null for (let index = 0; index < lines.length; index++) { const line = lines[index].trim() if (!line.startsWith('|')) { columns = null continue } const cells = splitCells(line) if (columns === null) { const found = {} for (const [name, matcher] of Object.entries(MAP_COLUMNS)) { const at = cells.findIndex((cell) => matcher.test(cell)) if (at !== -1) found[name] = at } if (Object.keys(found).length === Object.keys(MAP_COLUMNS).length) columns = found continue } if (cells.every((cell) => /^:?-{2,}:?$/.test(cell))) continue rows.push({ cells, columns, line: index + 1 }) } return rows.map(({ cells, columns, line }) => { const cell = (name) => cells[columns[name]] ?? '' const anchorMatch = /line\s+(\d+)/i.exec(cell('anchor')) return { line, id: firstCodeSpan(cell('id')), heading: firstCodeSpan(cell('heading')), anchorLine: anchorMatch === null ? null : Number(anchorMatch[1]), dataKey: firstCodeSpan(cell('dataKey')) } }) } /* -------------------------------------------------------------------- main */ const normalize = (text) => text.replace(/\s+/g, ' ').trim().toLowerCase() /** First line after `line` that carries content: not blank, not another anchor. */ function markedLine (lines, line) { for (let index = line; index < lines.length; index++) { const text = lines[index].trim() if (text === '' || text.startsWith('