import { readdir, readFile } from 'node:fs/promises'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; const root = fileURLToPath(new URL('../', import.meta.url)); const pluginRepos = process.env.RUNESTONE_PLUGIN_REPOS || path.resolve(root, '..'); export const projects = ['RuneStone', 'RuneStone_Project_Management', 'RuneStone_Terminal', 'RuneStone_Background_Slideshow', 'RuneStone_Local_Autocomplete', 'RuneStone_Git', 'RuneStone_PDF_Viewer', 'RuneStone_CSV_Viewer']; const patterns = { action: /\b(?:showActionMenu|actionMenu)\s*\(/g, svelteMenu: / !entry.isSymbolicLink()).map(entry => entry.isDirectory() ? files(path.join(dir, entry.name)) : /\.(svelte|[cm]?[jt]s)$/.test(entry.name) ? [path.join(dir, entry.name)] : [])); return results.flat(); } export async function discover() { const surfaces = {}, missing = []; for (const project of projects) { const base = project === 'RuneStone' ? root : path.resolve(pluginRepos, project); let sources; try { sources = await files(path.join(base, 'src')); } catch (error) { if (error.code === 'ENOENT') { missing.push(project); continue; } throw error; } // The plugin SDK declares the public menu API of RuneStone. if (project === 'RuneStone') sources.push(...await files(path.join(base, 'packages/plugin-sdk/src'))); for (const file of sources) { const source = await readFile(file, 'utf8'); const counts = Object.fromEntries(Object.entries(patterns).map(([name, pattern]) => [name, [...source.matchAll(pattern)].length]).filter(([, count]) => count)); if (Object.keys(counts).length) surfaces[`${project}/${path.relative(base, file).replaceAll('\\', '/')}`] = counts; } } return { surfaces, missing }; } export async function checkInventory() { const { surfaces, missing } = await discover(); const inventory = JSON.parse(await readFile(path.join(root, 'docs/menu-surfaces.json'), 'utf8')); const failures = missing.map(project => `Missing checkout: ${project}`); for (const [file, counts] of Object.entries(surfaces)) { const entry = inventory.surfaces[file]; if (!entry) failures.push(`Unreviewed surface: ${file}`); else if (JSON.stringify(counts) !== JSON.stringify(entry.counts)) failures.push(`Changed entry points: ${file}`); else if (!entry.design || !entry.check) failures.push(`Missing design or check: ${file}`); } for (const file of Object.keys(inventory.surfaces)) if (!(file in surfaces)) failures.push(`Stale inventory: ${file}`); for (const name of ['actionMenu.ts', 'managedActionMenu.ts', 'IconButton.svelte']) { try { const host = await readFile(path.join(root, 'src/lib/ui', name), 'utf8'); const terminal = await readFile(path.resolve(pluginRepos, 'RuneStone_Terminal/vendor/runestone/ui', name), 'utf8'); if (host.replaceAll('\r\n', '\n') !== terminal.replaceAll('\r\n', '\n')) failures.push(`Terminal shared snapshot differs: ${name}`); } catch (error) { failures.push(`Cannot check Terminal snapshot ${name}: ${error.code}`); } } if (failures.length) { console.error(failures.join('\n')); return false; } console.log(`PASS: ${Object.keys(surfaces).length} source files across ${projects.length} projects match the reviewed menu inventory. Runtime checks remain separate.`); return true; } if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { if (process.argv.includes('--discover')) console.log(JSON.stringify(await discover(), null, 2)); else if (!await checkInventory()) process.exitCode = 1; }