-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
validate.js
526 lines (512 loc) · 14.8 KB
/
validate.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
import { join } from 'path';
import { readdir, access, constants, readFile } from 'fs/promises';
import { freeCodeCampConfig, getProjectConfig, ROOT } from './env.js';
import { logover } from './logger.js';
import { pluginEvents } from '../plugin/index.js';
const CURRICULUM_PATH = join(
ROOT,
freeCodeCampConfig.curriculum.locales.english
);
const CONFIG_PATH = join(ROOT, freeCodeCampConfig.config['projects.json']);
/**
* # Validate Curriculum
*
* The validation throws when it encounters something that could break the functionality of the server,
* and logs a warning for anti-patterns that do not cause panics.
*
* ## Errors
*
* ### Projects
*
* - Each project has an H1 heading
* - Each project has a project description
* - Each project is associated with a boilerplate
* - Each project has congruent lesson numbers
* - First lesson is 0
* - Each project ends in `--fcc-end--`
*
* ### Configs
*
* - All configs have `dashedName` and `id` fields
* - All configs have a matching project by the `dashedName` field
* - Any `currentLesson` field is >= 0
* - Any `currentLesson` field is < the number of lessons in the project
* - Any `isIntegrated` field is a boolean
* - Any `isPublic` field is a boolean
* - Any `runTestsOnWatch` field is a boolean
* - Any `isResetEnabled` field is a boolean
* - Any `seedEveryLesson` field is a boolean
* - Any `blockingTests` field is a boolean
* - Any `breakOnFailure` filed is a boolean
*
* ### Seeds
*
* - All seed lesson numbers are >= 0
*
* ## Warnings
*
* ### Projects
*
* - Each project lesson has a `--description--` heading
* - Each description is not empty
* - Each project lesson has a `--tests--` heading
* - Each project lesson tests section is not empty
*
* ### Configs
*
* - Each config has a `description` field
* - Each config has a `title` field
* - If `breakOnFailure` is true, `blockingTests` is also true
*
* ### Seeds
*
* - Seed already exists in lesson
* - No seed lesson number is greater than the number of lessons in the project - 1
* - Lesson numbers are ordered
*/
export async function validateCurriculum() {
const { version, config, curriculum } = freeCodeCampConfig;
if (!isSemver(version)) {
panic(
'Invalid `version` field in `freecodecamp.conf.json`',
version,
'`version` should be a semver string`'
);
}
if (!config?.['projects.json']) {
panic(
'Invalid `config["projects.json"]` field in `freecodecamp.conf.json`',
config['projects.json'],
'`projects.json` should be a string'
);
}
if (!config?.['state.json']) {
panic(
'Invalid `config["state.json"]` field in `freecodecamp.conf.json`',
config['state.json'],
'`state.json` should be a string'
);
}
if (!curriculum?.['locales']) {
panic(
'Invalid `curriculum["locales"]` field in `freecodecamp.conf.json`',
curriculum['locales'],
'`locales` should be an object'
);
}
if (!curriculum?.['locales']?.english) {
panic(
'Invalid `curriculum["locales"]["english"]` field in `freecodecamp.conf.json`',
curriculum['locales']['english'],
'`english` is required, and should be a string'
);
}
const { port, client, hotReload, tooling } = freeCodeCampConfig;
// `port` must be a u16
if (
port &&
(typeof port !== 'number' || port < 0 || port > 65535 || port % 1 !== 0)
) {
panic(
'Invalid `port` field in `freecodecamp.conf.json`',
port,
'`port` should be a u16'
);
}
if (client) {
const { assets, landing, static: stat } = client;
if (assets) {
const { header, favicon } = assets;
if (header && typeof header !== 'string') {
panic(
'Invalid `client.assets.header` field in `freecodecamp.conf.json`',
header,
'`header` should be a string'
);
}
if (favicon && typeof favicon !== 'string') {
panic(
'Invalid `client.assets.favicon` field in `freecodecamp.conf.json`',
favicon,
'`favicon` should be a string'
);
}
}
if (landing) {
for (const [key, value] of Object.entries(landing)) {
const { title, description, 'faq-link': link, 'faq-text': faq } = value;
if (title && typeof title !== 'string') {
panic(
`Invalid \`client.landing.${key}.title\` field in \`freecodecamp.conf.json\``,
title,
`\`title\` should be a string`
);
}
if (description && typeof description !== 'string') {
panic(
`Invalid \`client.landing.${key}.description\` field in \`freecodecamp.conf.json\``,
description,
`\`description\` should be a string`
);
}
if (link && typeof link !== 'string') {
panic(
`Invalid \`client.landing.${key}['faq-link']\` field in \`freecodecamp.conf.json\``,
link,
`\`faq-link\` should be a string`
);
}
if (faq && typeof faq !== 'string') {
panic(
`Invalid \`client.landing.${key}['faq-text']\` field in \`freecodecamp.conf.json\``,
faq,
`\`faq-text\` should be a string`
);
}
}
}
if (stat) {
if (typeof stat === 'string') {
if (typeof stat !== 'string') {
panic(
'Invalid `client.static` field in `freecodecamp.conf.json`',
stat,
'`static` should be a string or object'
);
}
} else if (typeof stat === 'object') {
for (const [route, dir] of Object.entries(stat)) {
if (typeof dir !== 'string') {
panic(
`Invalid \`client.static[${route}]\` field in \`freecodecamp.conf.json\``,
dir,
`static directory should be a string`
);
}
}
}
}
}
if (hotReload) {
const { ignore } = hotReload;
if (ignore) {
if (Array.isArray(hotReload.ignore)) {
for (const ignore of hotReload.ignore) {
if (typeof ignore !== 'string') {
panic(
'Invalid `hotReload.ignore` field in `freecodecamp.conf.json`',
ignore,
'`ignore` should be an array of strings'
);
}
}
} else {
panic(
'Invalid `hotReload.ignore` field in `freecodecamp.conf.json`',
hotReload.ignore,
'`ignore` should be an array of strings'
);
}
}
}
if (tooling) {
const { helpers, plugins } = tooling;
if (helpers && typeof helpers !== 'string') {
panic(
'Invalid `tooling.helpers` field in `freecodecamp.conf.json`',
helpers,
'`helpers` should be a string'
);
}
if (plugins && typeof plugins !== 'string') {
panic(
'Invalid `tooling.plugins` field in `freecodecamp.conf.json`',
plugins,
'`plugins` should be a string'
);
}
}
const projects = JSON.parse(
await readFile(
join(ROOT, freeCodeCampConfig.config['projects.json']),
'utf-8'
)
);
for (const projectConfig of projects) {
const { dashedName, id } = projectConfig;
if (!dashedName) {
panic(
'Invalid `dashedName` field in `projects.json`',
dashedName,
'`dashedName` is required'
);
}
if (
typeof port !== 'number' ||
port < 0 ||
port > 65535 ||
port % 1 !== 0
) {
panic('Invalid `id` field in `projects.json`', id, '`id` is required');
}
const projectPath = join(ROOT, dashedName);
try {
await access(projectPath, constants.F_OK);
} catch (e) {
panic(
`Project ${dashedName} does not have a directory in the workspace root`,
projectPath,
`Project should have a matching directory in the workspace root`
);
}
const {
isIntegrated,
isPublic,
runTestsOnWatch,
isResetEnabled,
seedEveryLesson,
blockingTests,
breakOnFailure
} = projectConfig;
if (!undeBoolNull(isIntegrated)) {
panic(
'Invalid `isIntegrated` field in `projects.json`',
isIntegrated,
'`isIntegrated` should be a boolean'
);
}
if (!undeBoolNull(isPublic)) {
panic(
'Invalid `isPublic` field in `projects.json`',
isPublic,
'`isPublic` should be a boolean'
);
}
if (!undeBoolNull(runTestsOnWatch)) {
panic(
'Invalid `runTestsOnWatch` field in `projects.json`',
runTestsOnWatch,
'`runTestsOnWatch` should be a boolean'
);
}
if (!undeBoolNull(isResetEnabled)) {
panic(
'Invalid `isResetEnabled` field in `projects.json`',
isResetEnabled,
'`isResetEnabled` should be a boolean'
);
}
if (!undeBoolNull(seedEveryLesson)) {
panic(
'Invalid `seedEveryLesson` field in `projects.json`',
seedEveryLesson,
'`seedEveryLesson` should be a boolean'
);
}
if (!undeBoolNull(blockingTests)) {
panic(
'Invalid `blockingTests` field in `projects.json`',
blockingTests,
'`blockingTests` should be a boolean'
);
}
if (!undeBoolNull(breakOnFailure)) {
panic(
'Invalid `breakOnFailure` field in `projects.json`',
breakOnFailure,
'`breakOnFailure` should be a boolean'
);
}
const projectMeta = await pluginEvents.getProjectMeta(dashedName);
const { title, description, tags = [], numberOfLessons } = projectMeta;
if (!title || typeof title !== 'string') {
panic(
'Invalid `title` field in `projects.json`',
title,
'`title` is required'
);
}
if (!description || typeof description !== 'string') {
panic(
'Invalid `description` field in `projects.json`',
description,
'`description` is required'
);
}
if (tags.length) {
for (const tag of tags) {
if (typeof tag !== 'string') {
panic(
'Invalid `tags` field in `projects.json`',
tag,
'`tags` should be an array of strings'
);
}
}
}
if (numberOfLessons < 1) {
panic(
'Invalid `numberOfLessons` field in `projects.json`',
numberOfLessons,
'`numberOfLessons` should be a positive integer'
);
}
for (let i = 0; i < numberOfLessons; i++) {
const lesson = await pluginEvents.getLesson(dashedName, i);
const {
description,
tests,
hints,
seed,
beforeAll,
afterAll,
beforeEach,
afterEach,
isForce,
meta
} = lesson;
if (!description) {
warn(
`Missing \`--description--\` heading in lesson ${i} of ${dashedName}`,
description,
'Lesson description should not be empty'
);
}
if (!tests.length) {
warn(
`Missing \`--tests--\` heading in lesson ${i} of ${dashedName}`,
tests,
'Lesson tests should not be empty'
);
} else {
for (const test of tests) {
if (test.length !== 2) {
panic(
`Invalid test in lesson ${i} of ${dashedName}`,
test,
'Test should be an array of two strings'
);
} else {
const [testText, testCode] = test;
if (typeof testText !== 'string') {
panic(
`Invalid test text in lesson ${i} of ${dashedName}`,
testText,
'Test text should be a string'
);
}
if (typeof testCode !== 'string') {
panic(
`Invalid test in lesson ${i} of ${dashedName}`,
testCode,
'Test should be a string'
);
}
}
}
}
if (seed) {
if (Array.isArray(seed)) {
for (const s of seed) {
if (
typeof s !== 'string' &&
typeof s.filePath !== 'string' &&
typeof s.fileSeed !== 'string'
) {
panic(
`Invalid seed in lesson ${i} of ${dashedName}`,
s,
'Seed should be a string or an object with `filePath` and `fileSeed`'
);
}
}
} else {
panic(
`Invalid seed in lesson ${i} of ${dashedName}`,
seed,
'Seed should be a an array of strings'
);
}
}
if (!undeBoolNull(isForce)) {
panic(
`Invalid isForce in lesson ${i} of ${dashedName}`,
isForce,
'isForce should be a boolean'
);
}
if (hints) {
if (Array.isArray(hints)) {
for (const hint of hints) {
if (typeof hint !== 'string') {
panic(
`Invalid hint in lesson ${i} of ${dashedName}`,
hint,
'Hint should be a string'
);
}
}
} else {
panic(
`Invalid hints in lesson ${i} of ${dashedName}`,
hints,
'Hints should be an array of strings'
);
}
}
if (beforeAll && typeof beforeAll !== 'string') {
panic(
`Invalid beforeAll in lesson ${i} of ${dashedName}`,
beforeAll,
'beforeAll should be a string'
);
}
if (afterAll && typeof afterAll !== 'string') {
panic(
`Invalid afterAll in lesson ${i} of ${dashedName}`,
afterAll,
'afterAll should be a string'
);
}
if (beforeEach && typeof beforeEach !== 'string') {
panic(
`Invalid beforeEach in lesson ${i} of ${dashedName}`,
beforeEach,
'beforeEach should be a string'
);
}
if (afterEach && typeof afterEach !== 'string') {
panic(
`Invalid afterEach in lesson ${i} of ${dashedName}`,
afterEach,
'afterEach should be a string'
);
}
if (meta?.watch && meta?.ignore) {
panic(
`Invalid meta in lesson ${i} of ${dashedName}`,
meta,
'Lesson should not have both `watch` and `ignore`'
);
}
}
}
logover.info('All curriculum files are valid');
}
function undeBoolNull(val) {
return val === undefined || val === null || typeof val === 'boolean';
}
function isSemver(val) {
return /^\d+\.\d+\.\d+$/.test(val);
}
function panic(message, value, expectation) {
logover.error(message);
console.log('Expected:', expectation);
console.log('Received:', value);
throw new Error(message);
}
function warn(message, value, expectation) {
logover.warn(message);
console.log('Expected:', expectation);
console.log('Received:', value);
}