Files
RuneStone/scripts/menuSurfaceAudit.mjs
T
Knotty Wyvern 4d09da30d9 Support the migrated plugins in the host tests
- The pty_open mocks in layout/workbench and plugins/lifecycle send
  output through the channel that api.pty passes. Without a channel they
  keep the old events, so released Terminal versions still pass.
- plugins/gitToolbar resolves the Git plugin's "@runestone/plugin-sdk"
  imports to this repository's SDK build. The smoke runner builds the
  SDK when it is missing.
- The menu audit no longer compares the Terminal plugin's copies of the
  host menu files. The plugin now uses host menus through the SDK.

Checked with the released plugins and with the migrated Terminal, Git,
and Project Management branches.
2026-10-28 03:11:07 -07:00

56 lines
3.4 KiB
JavaScript

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: /<ContextMenu\b/g,
nativeChoice: /<select\b|(?:createElement|el)\(\s*['"]select['"]/g,
picker: /rs-picker(?:[\s"']|$)|role=['"]listbox['"]|setAttribute\(\s*['"]role['"],\s*['"]listbox['"]/g,
localPopup: /(?:class|className)\s*=\s*['"][^'"\n]*(?:menu|picker|popup)|\.popover\s*=/g,
};
async function files(dir) {
const entries = await readdir(dir, { withFileTypes: true });
const results = await Promise.all(entries.filter(entry => !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}`);
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;
}