forked from webhintio/hint
/
new-parser.ts
336 lines (289 loc) · 10.4 KB
/
new-parser.ts
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
import * as path from 'path';
import { promisify } from 'util';
import * as fs from 'fs-extra';
import * as inquirer from 'inquirer';
import * as mkdirp from 'mkdirp';
import * as logger from 'hint/dist/src/lib/utils/logging';
import isOfficial from 'hint/dist/src/lib/utils/packages/is-official';
import normalize from 'hint/dist/src/lib/utils/misc/normalize-string-by-delimeter';
import writeFileAsync from 'hint/dist/src/lib/utils/fs/write-file-async';
import { escapeSafeString, compileTemplate } from './handlebars-utils';
import { sendPendingData, trackEvent } from 'hint/dist/src/lib/utils/app-insights';
/*
* ------------------------------------------------------------------------------
* Types.
* ------------------------------------------------------------------------------
*/
/** Type for questions about the events in the parser. */
type EventType = {
again: boolean;
element: string;
event: string;
};
/** Type for questions about the parser. */
type QuestionsType = {
again: boolean;
description: string;
eventsSelected: EventType[];
name: string;
isOfficial: boolean;
};
/** Type for events in the parser. */
type ParserEventType = {
event: string;
handler: string;
type: string;
varName: string;
};
/*
* ------------------------------------------------------------------------------
* Classes and dependencies.
* ------------------------------------------------------------------------------
*/
const events: { [key: string]: string } = {
'element::': 'ElementFound',
'fetch::end::*': 'FetchEnd',
'fetch::end::html': 'FetchEnd',
'fetch::error': 'FetchError',
'fetch::start': 'FetchStart',
'scan::end': 'ScanEnd',
'scan::start': 'ScanStart',
'traverse::down': 'TraverseDown',
'traverse::end': 'TraverseEnd',
'traverse::start': 'TraverseStart',
'traverse::up': 'TraverseUp'
};
/**
* Convert a string to a capitalized one.
* i.e.: my-awesome parser -> MyAwesomeParser
*/
const capitalize = (str: string): string => {
if (str === '*') {
return '';
}
const splittedText: string[] = normalize(str, ' ').split(' ');
const result = splittedText.reduce((total, text) => {
/* istanbul ignore else */
if (!text) {
return total;
}
return `${total}${text.charAt(0).toUpperCase()}${text.slice(1)}`;
}, '');
return result;
};
class NewParser {
public name: string;
public normalizedName: string;
public capitalizedName: string;
public description: hbs.SafeString;
public events: ParserEventType[] = [];
public eventTypes: string[];
public processDir: string;
public destination: string;
public packageMain: string;
public packageName: string;
public official: boolean;
public isParser: boolean = true;
public constructor(parserData: QuestionsType) {
this.name = parserData.name;
this.normalizedName = normalize(parserData.name, '-');
this.capitalizedName = capitalize(parserData.name);
this.description = escapeSafeString(parserData.description);
// Using `Set` to avoid duplicates.
const eventTypesSet: Set<string> = new Set();
parserData.eventsSelected.forEach((event: EventType) => {
const isElement = event.event === 'element::';
const type: string = events[event.event];
const eventSplit = event.event.split('::');
const handler: string = `on${capitalize(eventSplit[0])}${capitalize(isElement ? event.element : eventSplit[1])}${eventSplit[2] ? capitalize(eventSplit[2]) : ''}`;
const varName: string = `${type.charAt(0).toLowerCase()}${type.substr(1)}`;
this.events.push({
event: isElement ? event.event + event.element : event.event,
handler,
type,
varName
});
eventTypesSet.add(type);
});
this.official = parserData.isOfficial;
const prefix = this.official ? '@hint/' : 'hint-';
this.packageMain = `dist/src/index.js`; // package.json#main
this.packageName = `${prefix}parser-${this.normalizedName}`; // package.json#name
this.destination = path.join(process.cwd(), `parser-${this.normalizedName}`);
this.processDir = process.cwd();
// Handlebars doesn't support 'Set' by default.
this.eventTypes = [...eventTypesSet];
}
}
/*
* ------------------------------------------------------------------------------
* Constants and Private functions.
* ------------------------------------------------------------------------------
*/
const mkdirpAsync = promisify(mkdirp) as (dir: string) => Promise<void>;
const eventList: string[] = Object.keys(events);
const TEMPLATE_PATH: string = './templates';
const SHARED_TEMPLATE_PATH = './shared-templates';
/* istanbul ignore next */
/** Configure questions depending on what we need. */
const questions = (repeat: boolean = false) => {
return [
{
default: 'newParser',
message: `What's the name of this new parser?`,
name: 'name',
type: 'input',
when() {
return !repeat;
}
},
{
default(answers: inquirer.Answers) {
return `Description for ${answers.name}`;
},
message(answers: inquirer.Answers) {
return `What's the description of this new parser '${answers.name}'?`;
},
name: 'description',
type: 'input',
when() {
return !repeat;
}
},
{
choices: eventList,
default: 'fetch::end::*',
message: 'Please select an event to subscribe in the parser',
name: 'event',
type: 'list',
when() {
return repeat;
}
},
{
default: 'div',
message: `What's the element for the event?`,
name: 'element',
type: 'input',
when(answers: inquirer.Answers) {
return answers.event === 'element::';
}
},
{
default: true,
message: 'Want to add more events (yes)?',
name: 'again',
type: 'confirm',
when() {
return repeat;
}
}
];
};
const copyFiles = async (data: NewParser) => {
const files = path.join(__dirname, 'files');
const noOfficialFiles = path.join(__dirname, 'no-official-files');
logger.log(`Creating new hint in ${data.destination}`);
if (!data.official) {
await fs.copy(noOfficialFiles, data.destination);
}
await fs.copy(files, data.destination);
logger.log('Files copied');
};
const generateFiles = async (data: NewParser) => {
const files = [
{
destination: path.join(data.destination, 'src', `${data.normalizedName}.ts`),
path: path.join(__dirname, TEMPLATE_PATH, 'parser.hbs')
},
{
destination: path.join(data.destination, 'tests', `${data.normalizedName}.ts`),
path: path.join(__dirname, TEMPLATE_PATH, 'tests.hbs')
},
{
destination: path.join(data.destination, 'src', `index.ts`),
path: path.join(__dirname, TEMPLATE_PATH, 'index.hbs')
},
{
destination: path.join(data.destination, 'README.md'),
path: path.join(__dirname, TEMPLATE_PATH, 'doc.hbs')
},
{
destination: path.join(data.destination, 'tsconfig.json'),
path: path.join(__dirname, SHARED_TEMPLATE_PATH, 'tsconfig.json.hbs')
},
{
destination: path.join(data.destination, 'package.json'),
path: path.join(__dirname, SHARED_TEMPLATE_PATH, 'package.hbs')
}
];
if (!data.official) {
files.push({
destination: path.join(data.destination, '.hintrc'),
path: path.join(__dirname, SHARED_TEMPLATE_PATH, 'config.hbs')
});
}
for (const file of files) {
const { destination: dest, path: filePath } = file;
const fileContent = await compileTemplate(filePath, data);
await mkdirpAsync(path.dirname(dest));
await writeFileAsync(dest, fileContent);
}
};
/*
* ------------------------------------------------------------------------------
* Public functions.
* ------------------------------------------------------------------------------
*/
/** Generate a new parser. */
export default async (): Promise<boolean> => {
const results: QuestionsType = (await inquirer.prompt(questions()) as QuestionsType);
const eventsSelected: EventType[] = [];
results.isOfficial = await isOfficial();
if (!results.name) {
throw new Error(`Name can't be empty`);
}
const askEvents = async () => {
const event: EventType = (await inquirer.prompt(questions(true)) as EventType);
if (event.event !== 'element::') {
eventList.splice(eventList.indexOf(event.event), 1);
eventsSelected.push(event);
} else {
const elementExists = eventsSelected.find((ev) => {
return ev.element === event.element;
});
if (!elementExists) {
eventsSelected.push(event);
}
}
if (event.again) {
await askEvents();
}
return;
};
await askEvents();
results.eventsSelected = eventsSelected;
const parserData = new NewParser(results);
await copyFiles(parserData);
await generateFiles(parserData);
logger.log(`
New parser created in ${parserData.destination}
--------------------------------------
---- How to use ----
--------------------------------------`);
if (parserData.official) {
logger.log(`1. Run 'yarn' to install the dependencies.
2. Go to the folder '${parserData.destination}'.
3. Run 'yarn build' to build the project.
4. Go to the folder 'packages/hint'.
5. Add your parser to '.hintrc'.
6. Run 'yarn hint https://YourUrl' to analyze your site.`);
} else {
logger.log(`1. Go to the folder '${parserData.destination}'.
2. Run 'npm run init' to install all the dependencies and build the project.
3. Run 'npm run hint -- https://YourUrl' to analyze you site.`);
}
trackEvent('new-parser');
await sendPendingData();
return true;
};