import { existsSync, mkdtempSync, readFileSync, rmdirSync, statSync, unlinkSync, writeFileSync, } from 'node:fs' import { basename, join } from 'node:path' import { expandGenerator, filesUnder, isGenerator } from './inputs.mjs' import { FIXTURES_DIR, ORACLES_R4_DIR, relativeToRepo } from './paths.mjs' /** * The import file set of import-baseline.mjs (groups base, r3, step6, * tolerance, read), listed the same way, so the R4 import measures the * TypeScript baseline's perimeter file for file. * * shortcut: a copy of import-baseline.mjs's listing (R4.2e leaves the * baseline script untouched); compare-imports.mjs checks that both runs * hold the same files. Upgrade path: import-baseline.mjs imports this * module. */ const READ_DIR = join(FIXTURES_DIR, 'pdf/r4/read') const BASE_FILES = [ 'report-v2.full.pdf', 'report-v2.minimal.pdf', 'report-v3.bare.pdf', ] const isPdf = (path) => path.endsWith('.pdf') function pdfsIn(group, dir, recursive = false) { const paths = filesUnder(join(FIXTURES_DIR, dir), recursive).filter(isPdf) return paths.map((path) => ({ group, path })) } function fixedGroups() { return [ ...BASE_FILES.map((f) => ({ group: 'base', path: join(FIXTURES_DIR, f) })), ...pdfsIn('r3', 'pdf/r3', true), ...pdfsIn('step6', 'pdf/step6'), ...pdfsIn('tolerance', 'pdf/r4/tolerance'), ] } /** The hostile set as read.json (O7) lists it; absent listed files reported. */ function hostileGroup() { const oraclePath = join(ORACLES_R4_DIR, 'read.json') if (!existsSync(READ_DIR)) { return { inputs: [], missing: ['pdf/r4/read/'], listedBy: null } } const listed = existsSync(oraclePath) ? JSON.parse(readFileSync(oraclePath, 'utf8')).inputs : filesUnder(READ_DIR).map((path) => ({ file: basename(path), kind: null })) const present = listed.filter((i) => existsSync(join(READ_DIR, i.file))) const missing = listed.filter((i) => !present.includes(i)).map((i) => i.file) const inputs = present.map((i) => ({ group: 'read', kind: i.kind, path: join(READ_DIR, i.file), })) return { inputs, missing, listedBy: existsSync(oraclePath) ? relativeToRepo(oraclePath) : 'directory', } } /** Every import input, narrowed by `--groups` (null = all). */ export function importInputs(groups) { const hostile = hostileGroup() const wanted = (i) => !groups || groups.includes(i.group) const inputs = [...fixedGroups(), ...hostile.inputs].filter(wanted) return { inputs, hostile } } /** The bytes an input stands for: its size, or its description's length. */ export function inputBytes(path) { if (!isGenerator(path)) return statSync(path).size return JSON.parse(readFileSync(path, 'utf8')).length } /** A generator description becomes a temporary file, removed after the run. */ export function materialise(input, scratch) { if (!isGenerator(input.path)) { return { path: input.path, cleanup: () => undefined } } const path = join(scratch, `${basename(input.path, '.gen.json')}.pdf`) writeFileSync(path, expandGenerator(input.path)) return { path, cleanup: () => unlinkSync(path) } } /** A fresh directory under `parent` for the run's temporary files. */ export function scratchDir(parent) { const dir = mkdtempSync(join(parent, 'faunosteo-r4-import-')) return { dir, remove: () => rmdirSync(dir) } }