blob: 9910d0c63490c62c8ef5ca8ea166b9a92d332fc5 [file] [log] [blame]
Victor Porof6e096922022-06-10 13:01:05 +00001const fs = require('fs').promises;
2const path = require('path');
3
4const WebIDL2 = require('webidl2');
5
6class IDLFile {
7 constructor(dir, file) {
8 this.filename = file;
9 this.shortname = path.basename(file, '.idl');
10 this.path = path.join(dir, file);
11 }
12
13 async text() {
14 const text = await fs.readFile(this.path, 'utf8');
15 return text;
16 }
17
18 async parse() {
19 const text = await this.text();
20 return WebIDL2.parse(text);
21 }
22}
23
24async function listAll({folder = __dirname} = {}) {
25 const all = {};
26 const files = await fs.readdir(folder);
27 for (const f of files) {
28 if (f.endsWith('.idl')) {
29 const idlFile = new IDLFile(folder, f);
30 all[idlFile.shortname] = idlFile;
31 }
32 }
33 return all;
34}
35
36async function parseAll(options) {
37 const all = await listAll(options);
38 for (const [key, value] of Object.entries(all)) {
39 all[key] = await value.parse();
40 }
41 return all;
42}
43
44module.exports = {listAll, parseAll};