-
Notifications
You must be signed in to change notification settings - Fork 6
/
chapters-obj.js
45 lines (42 loc) · 1.22 KB
/
chapters-obj.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
var fs = require('fs');
var path = require('path');
//
// {
// chapterName: {
// users: [ userObject, otherUserObject]
// events: []
// }
// }
//
// Synchronously create the resources object that contains
// the hierarchy of the folders with at the core level it contains
// arrays of the file paths to the resources
//
var chaptersDir = path.join(__dirname, 'api', 'chapters');
var chapters = fs.readdirSync(chaptersDir);
module.exports = chapters.reduce(function (acc, chapter) {
var chapterDir = path.join(chaptersDir, chapter);
if (!fs.statSync(chapterDir).isDirectory()) {
return acc;
}
acc[chapter] = fs.readdirSync(chapterDir).reduce(function (paths, resource) {
var resourceDir = path.join(chapterDir, resource);
if (!fs.statSync(resourceDir).isDirectory()) {
return paths;
}
paths[resource] = fs.readdirSync(resourceDir).map(function(file) {
if (!/.json$/.test(file)) {
return null;
}
var json;
//
// LETS MAKE SURE WE HAVE VALID JSON JUST TO BE SURE GUYS
//
try { json = require(path.join(resourceDir, file)) }
catch (ex) { json = null }
return json;
}).filter(Boolean);
return paths;
}, {});
return acc;
}, {});