-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.js
51 lines (46 loc) · 1.65 KB
/
compiler.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
const dataProvider = require('./dataProvider');
const templateProvider = require('./templateProvider');
const handlebars = require('handlebars');
const fs = require('fs');
// get config
function config() {
return dataProvider.config;
}
function IfEndswith(string, array) {
for (let i = 0; i < array.length; i++) {
if (string.endsWith(array[i])) {
return true;
}
}
return false;
}
function compile(filename) {
console.log(" - Compiling -> "+filename)
// get data
const defaultData = dataProvider.defaultData();
const data = dataProvider.data(filename+'.json');
Object.assign(data, defaultData);
// get template
const template = templateProvider.template(filename+'.html');
// compile template
const compiled = handlebars.compile(template);
// write compiled data to public_html folder
fs.writeFileSync('public_html/'+filename+'.html', compiled(data));
}
function compileAll() {
console.log(" ---- Starting compilation of all pages ----")
// get all files in assets folder
const files = fs.readdirSync('assets');
// filter out files that don't end with .html and also those html files that don't have a corresponding json file
const filtered_files = files.filter(file => IfEndswith(file, ['.html']) && fs.existsSync('assets/'+file.slice(0,-5)+".json"));
// create public_html folder if it doesn't exist
if (!fs.existsSync('public_html')) {
fs.mkdirSync('public_html');
}
// compile each file
filtered_files.forEach(file => {
compile(file.slice(0, -5));
});
}
module.exports.compileAll = compileAll;
module.exports.config = config;