-
Notifications
You must be signed in to change notification settings - Fork 0
/
finder.js
285 lines (263 loc) · 8.15 KB
/
finder.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
// Copyright 2024 Joseph P Medley
'use strict';
const { Select } = require('enquirer');
const { DirectoryManager } = require('./directorymanager.js');
const { FileProcessor } = require('./fileprocessor.js');
const { IDLBuilder } = require('./builder.js');
const utils = require('./utils.js');
const { util } = require('config');
const NOTHING_FOUND = "Could not find matching IDL files.\n"
const TRY_RUNNING = "\nTry running this command with the -f or -o flags to search for items behind\nflags or in origin trials.\n";
const CANCEL = '(none)';
function _finderFactory(args) {
//First few args are no longer needed.
args.shift();
const matches = args[0].match(/app_([^\.]+)\.js/);
const revisedArgs = [];
revisedArgs.push(matches[1]);
args.shift();
const searchDomain = args[0].toLowerCase();
let msg = `The action must be one of \'css\' or \'idl\'. The value ${searchDomain} was provided`;
if (!args[0]) {
msg = `You must provide an action type. ${msg}`;
}
args.shift();
revisedArgs.push(...args);
switch (searchDomain) {
case 'css':
return new CSSFinder(revisedArgs);
case 'idl':
return new IDLFinder(revisedArgs);
default:
throw new Error(msg);
}
}
class CSSFinder {
constructor(args, options) {
utils.sendUserOutput("CSSFinder is not yet available.\n");
process.exit();
}
}
class IDLFinder {
constructor(args, options = { iDLDirectory: `${utils.APP_ROOT}/idl/` }) {
this._processArguments(args)
const dmOptions = { types: ['interface', 'includes'] }
let dm = new DirectoryManager(options.iDLDirectory, dmOptions);
this._interfaces = dm.interfaceSet;
}
async _selectMixinsToPing(interfaces) {
if (interfaces[0].mixin) {
let promptMsg = `\nThe ${interfaces[0].name} interface is a mixin and will not appear with that\n`;
promptMsg += `name on MDN. Its members will appear as part of the interfaces below. Which\n`;
promptMsg += `interfaces would yo like to ping?\n`;
let names = []
for (let i of interfaces) {
if (i.type === 'includes') {
names.push(i.name);
}
}
names = names.sort();
names.push(CANCEL);
const prompt = new Select({
name: 'interface',
message: promptMsg,
choices: names
});
let answer = await prompt.run();
if (answer === CANCEL) { return; }
return interfaces.find(i => {
return i.name === answer;
});
} else {
return interfaces[0];
}
}
_findInterfaces(interfacesNamed) {
const matches = this._interfaces.findMatching(
interfacesNamed,
this._includeFlags,
this._includeOriginTrials
);
return matches;
}
_processArguments(args) {
this._searchString = args[1];
this._interactive = args.some(arg => {
return (arg.includes('-i') || (arg.includes('--interactive')));
});
this._includeFlags = args.some(arg => {
return (arg.includes('-f') || (arg.includes('--flags')));
});
this._includeOriginTrials = args.some(arg => {
return (arg.includes('-o') || (arg.includes('--origin-trials')));
});
this.__bcdOnly = args.some(arg => {
return (arg.includes('-b') || (arg.includes('--bcdOnly')));
});
if (args[0] === 'Builder') {
this._landingPageOnly = args.some(arg => {
return (arg.includes('-l') || (arg.includes('--landing-page')));
});
}
if (args[0] === 'Finder') {
this._ping = args.some(arg => {
return (arg.includes('-p') || (arg.includes('--ping')));
});
this._dump = args.some(arg => {
return (arg.includes('-d') || (arg.includes('--dump-names')));
});
}
}
async _findForListing() {
const matches = this._findInterfaces(this._searchString);
if (matches.length == 0) {
utils.sendUserOutput(NOTHING_FOUND);
if (!this._includeFlags && !this._includeOriginTrials) {
utils.sendUserOutput(TRY_RUNNING);
}
process.exit();
}
let names = [];
for (let m of matches) {
let steps = m.path.split('/');
const type = ((m) => {
if (m.type === 'includes') { return 'mixin'; }
return m.type;
})(m);
names.push(`"${m.keys[0]}",`);
}
names = names.sort();
let nameList = names.join('\n');
utils.sendUserOutput(nameList);
}
async _findForUI() {
const matches = this._findInterfaces(this._searchString);
if (matches.length == 0) {
utils.sendUserOutput(NOTHING_FOUND);
if (!this._includeFlags && !this._includeOriginTrials) {
utils.sendUserOutput(TRY_RUNNING);
}
process.exit();
}
let names = [];
for (let m of matches) {
let steps = m.path.split('/');
const type = ((m) => {
if (m.type === 'includes') { return 'mixin'; }
return m.type;
})(m);
names.push(`${m.keys[0]} (${type} from ${steps[steps.length-1]})`);
}
names = names.sort();
names.push(CANCEL);
const prompt = new Select({
name: 'idlFile',
message: 'Which interface do you want to work with?',
choices: names
});
let answer = await prompt.run();
if (answer === CANCEL) { process.exit(); }
const pieces = answer.split(' ');
const key = pieces[3].slice(0, -1).trim();
const answerData = matches.find(elem => {
return elem._sourcePath.includes(`/${key}`);
});
return answerData;
}
_show(file) {
let idlFile = utils.getIDLFile(file.path);
utils.sendUserOutput(idlFile);
utils.sendUserOutput(`File located at ${file.path}.`);
}
_printInstructions() {
const msg = `Use the up and down arrow to find the interface you want. Then press return.\n`
utils.sendUserOutput(msg);
}
async findAndShow() {
if (this._dump) {
this._findForListing();
} else {
await this._select();
}
}
async _select() {
this._printInstructions();
let metaFile = await this._findForUI();
let show = true;
if (this._ping) {
let ids = [];
const fp = new FileProcessor(metaFile.path);
fp.process((result) => {
ids.push(result);
});
let id = await this._selectMixinsToPing(ids);
if (id) {
utils.sendUserOutput('Checking for existing MDN pages. This may take a few minutes.\n');
const pingRecords = await id.ping(false);
this._showPingResults(pingRecords);
const msg = 'Display IDL file?';
show = await utils.confirm(msg);
}
}
if (show) {
utils.sendUserOutput();
this._show(metaFile);
}
}
_showPingResults(pingRecords) {
let lines = [];
let longest = 0;
pingRecords.forEach(r => {
if (r.key.length > longest) { longest = r.key.length; }
});
pingRecords.forEach(r => {
let exists = r.mdn_exists.toString().padEnd(8);
let key = r.key.toString().padEnd(longest + 1);
let url = r.mdn_url.toString();
lines.push(`${exists}${key}${url}`);
});
let ifaceHeader = "Interface".padEnd(longest + 1);
let header = `Exists? ${ifaceHeader}URL`;
utils.sendUserOutput(header);
utils.sendUserOutput('-'.repeat(header.length * 2));
lines.forEach(l => {
utils.sendUserOutput(l);
})
utils.sendUserOutput();
}
async findAndReturn() {
let metaFiles = this._findInterfaces(this._searchString);
if (!metaFiles) { return; }
let interfaceDataFiles = [];
metaFiles.forEach(mf => {
let fp = new FileProcessor(mf.path);
fp.process(result => {
interfaceDataFiles.push(result);
});
});
return interfaceDataFiles;
}
async findAndBuild() {
this._printInstructions()
let metaFile = await this._findForUI();
let ids = [];
const fp = new FileProcessor(metaFile.path);
fp.process((result) => {
ids.push(result);
}, true);
let id = ids.find((id) => {
return id.name === metaFile.name;
})
const options = {
interactive: this._interactive,
interfaceData: id,
bcdOnly: this.__bcdOnly,
landingPageOnly: this._landingPageOnly
};
const builder = new IDLBuilder(options);
builder.build();
}
}
module.exports.FinderFactory = _finderFactory;
module.exports.CSSFinder = CSSFinder;
module.exports.Finder = IDLFinder;