Elffuss BraidHub

utopia-ia/elffuss-code public

main · 2.2 KiBRawPermalink
1// Monaco 0.52.2's built-in languages, as Code's page sees them before any
2// extension: their ids, and whether each is a Monarch language (its definition
3// has a `loader`, so an extension's TextMate grammar can replace its colouring;
4// the others, such as JSON, keep their own). The panel asks the live editor
5// (js/ext/host.js: builtinLanguage, grammarSkippedFor); the static catalog
6// census, which runs in Node, reads the same list from monaco-languages.json.
7//
8// node tests/lib/monaco-languages.mjs print the live list
9// node tests/lib/monaco-languages.mjs --write rewrite monaco-languages.json
10// tests/x2_ux.mjs checks that the file still matches the live editor.
11import fs from 'node:fs';
12import path from 'node:path';
13import { fileURLToPath } from 'node:url';
14
15const HERE = path.dirname(fileURLToPath(import.meta.url));
16export const FILE = path.join(HERE, 'monaco-languages.json');
17
18// in a page of Code where Monaco is loaded
19export const liveLanguages = page => page.evaluate(() => window.monaco.languages.getLanguages()
20 .map(l => ({ id: l.id, monarch: typeof l.loader === 'function' }))
21 .sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0)));
22
23export const readLanguages = () => JSON.parse(fs.readFileSync(FILE, 'utf8'));
24
25if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
26 const { chromium } = await import('playwright');
27 const { startServer } = await import('./x2-server.mjs');
28 const srv = await startServer();
29 const browser = await chromium.launch();
30 try {
31 const page = await browser.newPage();
32 await page.goto(srv.codeOrigin + '/?test-opfs&noext', { waitUntil: 'domcontentloaded' });
33 await page.waitForFunction(() => !!window.monaco?.languages?.getLanguages?.().length, null, { timeout: 60000 });
34 const languages = await liveLanguages(page);
35 const out = { monaco: '0.52.2', note: 'Generated by tests/lib/monaco-languages.mjs --write from the live editor; tests/x2_ux.mjs checks it.', languages };
36 if (process.argv.includes('--write')) { fs.writeFileSync(FILE, JSON.stringify(out, null, 1) + '\n'); console.log(`${languages.length} languages written`); }
37 else console.log(JSON.stringify(out, null, 1));
38 } finally { await browser.close(); await srv.close(); }
39}