-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathindex.js
84 lines (69 loc) · 2.19 KB
/
index.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
const yaml = require("js-yaml");
const Validator = require("./Validator");
const fs = require("fs");
const path = require("path");
const filesNeedFixing = [];
function readFile(loc) {
return fs.readFileSync(path.join(__dirname, loc)).toString();
}
function listArr(arr) {
for (let i = 0; i < arr.length; i++) console.log(`${i + 1}.`, arr[i]);
}
function printErrors(id, errs) {
if (errs.school.length !== 0) {
filesNeedFixing.push(`${id}/school.yml`);
console.log(`\nThe following errors were found in ${id}/school.yml:`);
listArr(errs.school);
}
if (errs.schedule.length !== 0) {
filesNeedFixing.push(`${id}/schedule.yml`);
console.log(`\nThe following errors were found in ${id}/schedule.yml:`);
listArr(errs.schedule);
}
}
function loadSchool(id) {
let school = yaml.safeLoad(readFile(`./data/${id}/school.yml`));
let schedule = yaml.safeLoad(readFile(`./data/${id}/schedule.yml`));
let validator = new Validator(school, schedule);
if (validator.hasErrors()) {
printErrors(id, validator.getErrors());
errorsExist = true;
}
return {
school: JSON.stringify(validator.getCleaned().school),
schedule: JSON.stringify(validator.getCleaned().schedule),
periods: JSON.stringify(validator.getCleaned().school.periods),
};
}
const directory = yaml.safeLoad(readFile("./data/directory.yml"));
const obj = {};
const validateAll =
process.argv.includes("-ca") || process.argv.includes("--check-all");
for (let key in directory) {
let folder = directory[key].folder ? directory[key].folder : key;
if (typeof directory[key].live === "boolean" && !directory[key].live) {
// doesn't add it but still checks for errors iff validateAll
if (validateAll) loadSchool(folder);
continue;
}
obj[key] = {
name: directory[key].name,
...loadSchool(folder),
};
}
const schools = [];
for (let key in obj) {
if (!obj.hasOwnProperty(key)) continue;
schools.push({
n: obj[key].name,
id: key,
});
}
obj.schools = schools;
if (filesNeedFixing.length !== 0) {
console.log("\n\nValidation failed! The following files need to be fixed:");
listArr(filesNeedFixing);
console.log("\n");
throw "Invalid schedule";
}
module.exports = obj;