-
Notifications
You must be signed in to change notification settings - Fork 0
/
bcdbuilder.js
81 lines (70 loc) · 2.38 KB
/
bcdbuilder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2024 Joseph P Medley
'use strict';
const fs = require('fs');
const utils = require('./utils.js');
const API_TEMPLATE = 'templates/bcd-api.txt';
const CONSTR_TEMPLATE = 'templates/bcd-constructor.txt';
const MEMBER_TEMPLATE = 'templates/bcd-member.txt';
const NEST_LEVEL = 2;
function getTemplate(templateName) {
let source = fs.readFileSync(templateName);
source = (' ' + source).slice(1);
return source
}
class _BCDBuilder {
constructor(interfaceData, type = 'api', options) {
// Later this will need to distinguish BCD categories.
this._bcdString = '';
this._interfaceData = interfaceData;
this._loadBCD();
}
async write(outFilePath) {
if (fs.existsSync(outFilePath)) { return; }
// Poor man's way of fixing the nesting.
const temp = JSON.parse(this._bcdString);
this._bcdString = JSON.stringify(temp, null, NEST_LEVEL);
fs.writeFileSync(outFilePath, this._bcdString);
const msg = `BCD boilerplate has been written to ${outFilePath}.`;
utils.sendUserOutput(msg);
}
getBCD() {
const msg = 'BCDBuilder.getBCD() is deprecated. Use getBCDObject() instead.'
return this.getBCDObject();
}
getBCDObject() {
return JSON.parse(this.getRawBCD());
}
getRawBCD() {
return this._bcdString;
}
_loadBCD() {
let membersBCD = [];
const members = this._interfaceData.getMembers( { includeUnnamed: false });
const skipMembers = ["(getter)", "(iterable)", "(setter)"];
let constructorFound = false;
for (let m of members) {
if (skipMembers.includes(m.name)) { continue; }
if (m.type === 'Constructor') {
if (!constructorFound) {
membersBCD.push(getTemplate(CONSTR_TEMPLATE));
constructorFound = true;
}
} else {
let member = getTemplate(MEMBER_TEMPLATE)
.replace(/\[\[member-name\]\]/g, m.name);
membersBCD.push(member);
}
}
this._bcdString = getTemplate(API_TEMPLATE);
let memberString = '';
if (membersBCD.length) {
memberString = membersBCD.join(',\n');
memberString = memberString.replace('\n,', ',');
memberString = `,${memberString}`;
}
this._bcdString = this._bcdString.replace('[[members]]', memberString);
this._bcdString = this._bcdString.replace(/\[\[api-name\]\]/g, this._interfaceData.name);
return this._bcdString;
}
}
module.exports.BCDBuilder = _BCDBuilder;