-
Notifications
You must be signed in to change notification settings - Fork 0
/
linker.js
102 lines (91 loc) · 3.15 KB
/
linker.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
let fs = require('fs');
let parser = require('./parser');
class Linker {
constructor(make) {
this.make = make;
this.homepage = '';
this.sections = {};
this.pages = [];
this.configs = {};
}
link() {
// Get the homepage
let home = this.make.homepage;
if(home) {
try {
let rawHome = fs.readFileSync(`./${home}`, 'utf8').toString();
this.homepage = rawHome;
}
catch (e) {
this.error(`Couldn't read the homepage. ${e}`);
}
}
let files = fs.readdirSync('./', 'utf8').toString();
for(let project of this.make.projects) {
if(!files.includes(project)) {
this.error(`Project "${project}" listed in make.json but no directory found`);
}
try {
let data = fs.readFileSync(`./${project}/config.json`, 'utf8').toString();
this.configs[project] = JSON.parse(data);
}
catch (e) {
this.error(`Couldn't read the config of ${project}. ${e}`);
}
}
this.mergePages();
return {
homepage: this.homepage,
pages: this.pages,
sections: this.sections,
};
}
mergePages() {
// We want pages in order we get them based on precedence set by make. Set so we get no dupes.
let pageOrder = new Set();
// Each key is a page and the value is the set of projects that correspond to it.
let pageProjects = {};
// Go through each project and get all its pages
for(let project of this.make.projects) {
// Get the pages in order
let projectsPages = this.configs[project].pageOrder;
// Add each page to the pages
for(let page of projectsPages) {
// Add it to the set, meaning no dupes. Yay!
pageOrder.add(page);
// Check if we've initialized the key/value
if(pageProjects[page]) {
pageProjects[page].push(project);
}
else {
pageProjects[page] = [project];
}
}
}
// For each page, in order
for(let page of pageOrder) {
this.mergePage(page, pageProjects[page]);
}
};
mergePage(page, projects) {
let pages = {};
for(let project of projects) {
try {
let data = fs.readFileSync(`./${project}/${page}.json`, 'utf8').toString();
pages[project] = Object.assign({
filename: page,
}, JSON.parse(data));
}
catch (e) {
this.error(`Page ${page} was listed in pageOrder of ${project} but we couldn't read '/${project}/${page}.json'. ${err}`);
}
}
let pageout = parser(this.make.projects, pages);
this.pages.push(pageout);
this.sections[page] = pageout[0].sections;
}
error(msg) {
throw new Error(`Linker error: ${msg}`);
};
}
module.exports = Linker;