Victor Porof | 6e09692 | 2022-06-10 13:01:05 +0000 | [diff] [blame^] | 1 | const fs = require('fs').promises; |
| 2 | const path = require('path'); |
| 3 | |
| 4 | const WebIDL2 = require('webidl2'); |
| 5 | |
| 6 | class 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 | |
| 24 | async 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 | |
| 36 | async 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 | |
| 44 | module.exports = {listAll, parseAll}; |