forked from freeCodeCamp/python-coding-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-challenge-json.js
91 lines (78 loc) · 2.52 KB
/
generate-challenge-json.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
var fs = require('file-system');
function printList(list) {
list.forEach((v) => console.log(v));
}
let dir_list = fs.readdirSync('challenges');
//keep directories only
dir_list = dir_list.filter((val) => {
return fs.statSync(`challenges/${val}`).isDirectory();
});
// get the current challenge object, parse it, save the challenge list
const challenges_string = fs.readFileSync('./challenges.json', 'utf-8');
let challenges_json = JSON.parse(challenges_string);
let challenges_list = challenges_json.challenges;
// create a dictionary where challenges ids are keys
// and their index in the challenges_list is the val
let challenges_id_list = [];
challenges_list.forEach((chapter) => {
chapter.forEach((c) => {
challenges_id_list.push(c.id);
});
});
// loop through dir_list
dir_list.forEach((dir) => {
const path = `challenges/${dir}/lesson_settings.json`;
let data = fs.readFileSync(path, 'utf-8');
if (data.length === 0) return null;
const challenge = JSON.parse(data);
const {
id,
lesson_title,
repl,
chapter_number,
lesson_number
} = challenge;
const default_repl = "https://repl.it/student_embed/assignment/136137/d860e0ad34862a709895f54706dcf9af";
const lesson_obj = {
id,
"title": lesson_title,
"repl": repl.length === 0 ? default_repl : repl,
"completed": false,
"chapter": chapter_number,
"lesson": lesson_number
};
// attempt to autofill 'missing' challenges
//
// const blank_lesson_obj = {
// "id": null,
// "title": "NA",
// "repl": default_repl,
// "completed": false,
// "chapter": null,
// "lesson": null
// }
if (!challenges_id_list.includes(id)) {
let chapter = challenges_list[chapter_number];
// if ( lesson_number !== 1 && chapter.length < lesson_number ) {
// for (let i = 1; i < lesson_number; i++ ) {
// blank_lesson_obj.chapter = chapter_number;
// blank_lesson_obj.lesson = i;
// chapter.push(blank_lesson_obj);
// }
// }
let a = chapter.slice(0, lesson_number);
let b = chapter.slice(lesson_number + 1, chapter.length);
a.push(lesson_obj);
challenges_list[chapter_number] = a.concat(b);
challenges_id_list.push(id);
} else {
challenges_list[chapter_number][lesson_number - 1] = lesson_obj;
}
});
challenges_json.challenges = challenges_list;
challenges_json.last_edit = Date.now();
fs.writeFile('./challenges.json', JSON.stringify(challenges_json), (err) => {
if (err) throw err;
console.log('The file has been saved!');
printList(challenges_list);
});