-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.js
379 lines (347 loc) · 11 KB
/
builder.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright 2024 Joseph P Medley
'use strict';
const { bcd } = require('./bcd.js');
const { BCDBuilder } = require('./bcdbuilder.js');
const { BuilderError } = require('./errors.js');
const { help } = require('./help/help.js');
const { pageFactory } = require('./page.js');
const path = require('path');
const { Questions } = require('./questions.js');
const utils = require('./utils.js');
const FLAGS = {
"-c":"--constructor",
"--constructor":"--constructor",
"-d": "--directive",
"--directive": "--directive",
"-e":"--event",
"--event":"--event",
"-h":"--handler",
"-H":"--header",
"--handler":"--handler",
"--header":"--header",
"-l":"--landing",
"--landing":"--landing",
"-m":"--method",
"--method":"--method",
"-p":"--property",
"--property":"--property",
"-r":"--interface",
"--reference":"--interface",
"-s": "--css",
"--css": "--css"
}
// const NOT_NEEDED = [ "deleter", "getter", "iterable", "setter", "stringifier" ];
function getNamedArg(arg) {
if (arg in FLAGS) {
return FLAGS[arg];
} else {
return arg;
}
}
function pageExists(arg, pageData) {
let args = arg.split(',');
let page = pageData.find(aPage=>{
return aPage.key.includes(args[1]);
});
if (page) { return page.mdn_exists; }
return false;
}
class Builder {
constructor(options) {
}
_normalizeArguments(args) {
// Remove 'node _command.js_' from args.
args.shift();
args.shift();
switch (args[0]) {
case 'css':
return this._normalizeCSSArgs(args);
break;
case 'header':
if (!FLAGS[args[3]]) {
throw new Error('This command requires more than one flag.');
}
return this._normalizeHeaderArgs(args);
break;
case 'interface':
if (!FLAGS[args[3]]) {
throw new Error('This command requires more than one flag.');
}
return this._normalizeInterfaceArgs(args);
break;
}
}
_normalizeCSSArgs(args) {
let trueArgs = new Array();
trueArgs.push(args[0]);
trueArgs.push('n,' + args[2]);
trueArgs.push('css,' + args[2]);
return trueArgs;
}
_normalizeHeaderArgs(args) {
let trueArgs = new Array();
args.forEach((arg, index, args) => {
arg = getNamedArg(arg);
switch (arg) {
case '--directive':
trueArgs.push(arg);
break;
case '--header':
trueArgs.push(arg);
trueArgs.push(args[2]);
break;
default:
trueArgs.push(arg);
}
});
trueArgs = this._rearrangeArgs(trueArgs);
return trueArgs;
}
_normalizeInterfaceArgs(args) {
let trueArgs = new Array();
args.forEach((arg, index, args) => {
arg = getNamedArg(arg);
switch (arg) {
case '--constructor':
// NEW
trueArgs.push(arg);
trueArgs.push(args[2] + '.' + args[2]);
break;
case '--header':
case '--interface':
trueArgs.push(arg);
trueArgs.push(args[2]);
break;
case '-it':
const iterables = ['entries', 'forEach', 'keys', 'values'];
iterables.forEach((functionName) => {
trueArgs.push('-' + functionName);
trueArgs.push(functionName);
});
break;
case '-m':
case '--method':
if (args[index+1].endsWith(')')) {
args[index+1] = args[index+1].slice(0, -1);
}
if (args[index+1].endsWith('(')) {
args[index+1] = args[index+1].slice(0, -1);
}
trueArgs.push(arg);
break;
case '-mp':
const maplike = ['clear', 'delete', 'entries', 'forEach', 'get', 'has', 'keys', 'set', 'size', 'values'];
maplike.forEach((functionName) => {
trueArgs.push('-' + functionName);
trueArgs.push(functionName);
});
break;
case '-mr':
const readonlyMaplike = ['entries', 'forEach', 'get', 'has', 'keys', 'size', 'values'];
readonlyMaplike.forEach((functionName) => {
trueArgs.push('-' + functionName);
trueArgs.push(functionName);
});
break;
case '--landing':
trueArgs.push(arg)
trueArgs.push(args[2]);
break;
default:
trueArgs.push(arg);
};
});
trueArgs = this._rearrangeArgs(trueArgs);
return trueArgs;
}
_rearrangeArgs(args) {
for (let i = 0; i < args.length; i++) {
if (args[i].startsWith('--')) {
args[i] = args[i].replace('--', '@@');
}
if (args[i].startsWith('-')) {
args[i] = args[i].replace('-', '@@');
}
}
let argString = args.join();
let arrangedArgs = argString.split('@@');
if (arrangedArgs[0]=='') { arrangedArgs.shift(); }
for (let arg in arrangedArgs) {
if (arrangedArgs[arg].endsWith(',')) {
arrangedArgs[arg] = arrangedArgs[arg].slice(0, arrangedArgs[arg].length -1);
}
}
return arrangedArgs;
}
}
class _CLIBuilder extends Builder {
constructor(options) {
super(options);
this._args = options.args;
this._writeOnly = options.args.some(arg => {
return (arg.includes('-w') || arg.includes('--writefiles'))
});
}
_initPages() {
let args = this._normalizeArguments(this._args);
let parentType = args[0];
let parentName = args[1].split(',')[1];
// Add space for interface or header name to sharedQuestions,
// and remove it from args.
let introMessage = `\nSHARED QUESTIONS\n` + (`-`.repeat(80)) + `\nYou will now be asked questions for answers that are shared\namong all the files to be created.\n`;
let sharedQuestions = new Questions(introMessage);
sharedQuestions[parentType] = parentName;
sharedQuestions['name'] = parentName;
sharedQuestions.add(parentType, parentName);
// We no longer need the conent type and name.
args.shift();
args.shift();
// Create an array for the question objects.
this._pages = new Array();
// Process remaining arguments.
args.forEach((arg, index, args) => {
if ((arg.trim() === 'w') || arg.trim() === 'writeOnly') { return; }
let members = arg.split(',');
const pageOptions = {
interfaceData: this._interfaceData,
root: this._outPath
}
const aPage = pageFactory(members[1], members[0], sharedQuestions, pageOptions);
this._pages.push(aPage);
});
}
async build() {
this._initPages();
for (let p of this._pages) {
if (!this._writeOnly) {
await p.askQuestions();
}
p.write();
}
}
}
class _CSSBuilder extends Builder {
constructor(options) {
super(options);
}
}
class _IDLBuilder extends Builder {
constructor(options) {
super(options);
this._interactive = options.interactive || false;
this._interfaceData = options.interfaceData;
this._interfaceOnly = options.interfaceOnly || false;
this._bcdOnly = options.bcdOnly || false;
this._landingPageOnly = options.landingPageOnly || false;
this._mode = options.mode || 'standard';
this.withholdBCD = options.withholdBCD || false;
if (!options.outPath) {
this._outPath = utils.getOutputDirectory();
} else {
this._outPath = options.outPath;
}
if (!options.bcdPath) {
this._bcdPath = utils.getBCDPath();
} else {
this._bcdPath = options.bcdPath;
}
if (this._bcdOnly && this.withholdBCD) {
const msg = `The arguments options.jsonOnly and options.noJson cannot both be true.`;
throw new BuilderError(msg, __filename, 279);
}
}
_includeLandingPage() {
if (this._mode === 'batch') { return false; }
if (this._mode === 'standard') { return true; }
return !this._interfaceOnly;
}
async _initPages() {
// Add space for interface or header name to sharedQuestions.
const introMessage = help.intro + (`-`.repeat(80)) + `\nSHARED QUESTIONS\n` + (`-`.repeat(80)) + `\n` + help.shared;
const sharedQuestions = new Questions(introMessage);
sharedQuestions['interface'] = this._interfaceData.name;
sharedQuestions['name'] = this._interfaceData.name;
sharedQuestions.add('interface', this._interfaceData.name);
if (this._interfaceData.getSecureContext()) {
sharedQuestions.add('isSecureContext', '{{securecontext_header}}');
} else {
sharedQuestions.add('isSecureContext', '');
}
// Create an array for the question objects.
this._pages = new Array();
// Add an object for the landing page.
if (this._includeLandingPage()) {
const pageOptions = {
interfaceData: this._interfaceData,
root: this._outPath
}
const aPage = pageFactory('landing', 'landing', sharedQuestions, pageOptions);
this._pages.push(aPage);
if (this._landingPageOnly) { return; }
}
// Process remaining arguments.
let skippingPages = new Array();
const missingPages = await this._interfaceData.ping();
missingPages.forEach((page, index, pages) => {
if (this._interfaceOnly && (page.type !== 'interface')) { return; }
if (page.mdn_exists) {
skippingPages.push([page.type, page.key]);
} else {
const pageOptions = {
interfaceData: this._interfaceData,
root: this._outPath
}
const newPage = pageFactory(page.name, page.type, sharedQuestions, pageOptions);
this._pages.push(newPage);
}
});
if (skippingPages.length > 0) {
let msg = '\nThe following pages from this interface already exist. You will not be asked\nquestions about them.\n';
for (let s of skippingPages) {
msg += `\t ${s[1]} ${s[0]}\n`;
}
utils.sendUserOutput(msg);
await utils.pause();
}
if (missingPages === 0) {
let msg = 'No MDN pages have been created for this interface.';
utils.sendUserOutput(msg)
}
}
async _writeBCD() {
let name = this._interfaceData.name;
if (bcd.api[name]) {
const msg = `\nA BCD file already exists for ${name}. You will need to manually\nverify it for completeness.\n`;
utils.sendUserOutput(msg);
return;
}
let bcdBuilder = new BCDBuilder(this._interfaceData, {});
// let outFilePath = this._resolveBCDPath(name);
// await bcdBuilder.write(outFilePath);
let outFilePath = path.join(this._bcdPath, `${name}.json`);
await bcdBuilder.write(outFilePath);
}
async build(overwrite = 'prompt') {
if (!this.withholdBCD) { await this._writeBCD(); }
if (this._bcdOnly) { return; }
await this._initPages();
let msg;
if (this._pages.length === 0) {
msg = '\nThere are no undocumented members for this interface.\n';
utils.sendUserOutput(msg);
return;
}
for (let p of this._pages) {
if (this._interactive) {
await p.askQuestions();
}
await p.write(overwrite);;
}
msg = `\nMDN drafts were written to ${this._outPath}${this._interfaceData.name}.`
utils.sendUserOutput(msg);
}
}
module.exports.pageExists = pageExists;
module.exports.CLIBuilder = _CLIBuilder;
module.exports.CSSBuilder = _CSSBuilder;
module.exports.IDLBuilder = _IDLBuilder;