Files

71 lines
3.2 KiB
JavaScript
Raw Permalink Normal View History

2026-10-12 07:54:37 -04:00
import { readFileSync, writeFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { createServer } from 'vite';
import react from '@vitejs/plugin-react-swc';
2026-10-12 08:55:18 -04:00
import { classicViewSources } from './classic-sources.mjs';
2026-10-12 07:54:37 -04:00
2026-10-12 23:40:08 -04:00
// Loads the engine's view definitions and writes the catalog, keyed by name, that `pterodactyl-theme list` prints
// and generate-sdk-docs builds the view catalogue from; `classic` names the file and export `pterodactyl-theme eject` copies. `slots` are the slots a layer of
2026-10-12 19:14:14 -04:00
// the view renders, `engineSlots` those the engine renders around a page, and `areaSlots` those of the area layouts,
// which a `layout.app` layer renders where it draws an area's navigation.
2026-10-12 07:54:37 -04:00
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const output = resolve(root, 'packages/sdk/views.json');
2026-10-12 08:55:18 -04:00
const classic = classicViewSources(resolve(root, 'resources/scripts/themes/classic'));
2026-10-12 07:54:37 -04:00
const server = await createServer({
configFile: false,
root,
logLevel: 'error',
appType: 'custom',
plugins: [react()],
resolve: { alias: { '@': resolve(root, 'resources/scripts'), '@feature': resolve(root, 'resources/scripts/components/server/features') } },
server: { middlewareMode: true, hmr: false, ws: false },
optimizeDeps: { noDiscovery: true, include: [] },
});
let catalog;
try {
2026-10-12 16:21:19 -04:00
const { loadViewCatalog } = await server.ssrLoadModule('/resources/scripts/views/catalog.ts');
2026-10-12 19:14:14 -04:00
const { pageSlots } = await server.ssrLoadModule('/resources/scripts/views/pages.ts');
const views = await loadViewCatalog();
const slotsOf = (name) => views.find((view) => view.name === name)?.slots ?? [];
2026-10-12 23:40:08 -04:00
// `pterodactyl-theme eject` copies the classic source of any view.
const unsourced = views.filter((view) => !classic[view.name]).map((view) => view.name);
if (unsourced.length) throw new Error(`Classic has no source for ${unsourced.join(', ')}.`);
2026-10-12 08:13:40 -04:00
catalog = Object.fromEntries(
2026-10-12 19:14:14 -04:00
views.map((view) => {
const engineSlots = view.kind === 'page' ? [...(pageSlots(view.name) ?? [])] : [];
return [
view.name,
{
kind: view.kind,
context: view.context,
slots: (view.slots ?? []).filter((slot) => !engineSlots.includes(slot)),
...(engineSlots.length ? { engineSlots } : {}),
...(view.name === 'layout.app'
? { areaSlots: ['layout.account', 'layout.server', 'layout.admin'].flatMap(slotsOf) }
: {}),
fixtures: Object.keys(view.fixtures ?? {}),
2026-10-12 23:40:08 -04:00
classic: classic[view.name],
2026-10-12 19:14:14 -04:00
},
];
})
2026-10-12 08:13:40 -04:00
);
2026-10-12 07:54:37 -04:00
} finally {
await server.close();
}
const json = JSON.stringify(catalog, null, 4) + '\n';
if (process.argv.includes('--check')) {
let existing = '';
try {
existing = readFileSync(output, 'utf8');
} catch {}
if (existing !== json) {
console.error('packages/sdk/views.json is stale. Run npm run sdk:generate.');
process.exit(1);
}
} else {
writeFileSync(output, json);
}
2026-10-12 08:13:40 -04:00
console.log(`SDK: ${Object.keys(catalog).length} views`);