-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic-site-generator.js
executable file
·115 lines (90 loc) · 3.26 KB
/
static-site-generator.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
103
104
105
106
107
108
109
110
111
112
113
114
115
var fs = require("fs");
var path = require("path");
var handlebars = require("handlebars");
var copydir = require("copy-dir");
var config = require("./config.json");
var site = require("./site.json");
var src = "./" + config.src;
var dest = "./" + config.dest;
makeDirIfNotExist(dest);
makeDirIfNotExist(path.join(dest, "css"));
copydir.sync(path.join(src, "css"), path.join(dest, "css"));
makeDirIfNotExist(path.join(dest, "img"));
copydir.sync(path.join(src, "img"), path.join(dest, "img"));
makeDirIfNotExist(path.join(dest, "warpzone"));
copydir.sync(path.join(src, "pages/warpzone"), path.join(dest, "warpzone"));
let data = {};
data.site = site;
addPageData(data);
function addPageData(data) {
var newData = data;
newData.site.pages.forEach((page) => {
if (page.showInNav){
if (page.file){
var pageName = page.file.replace(".html", "").replace("/", "").toLowerCase();
var jsonPath = path.join(src, "pages", pageName + ".json");
if (fs.existsSync(jsonPath)){
newData[pageName] = require("./" + jsonPath);
}
} else{
page.file = page.url;
}
}
});
newData["games"].sort(function(a, b) {
return new Date(b.released) - new Date(a.released);
});
return newData;
}
data["games"].sort(function(a, b) {
return new Date(b.released) - new Date(a.released);
});
fs.writeFileSync("./data.json", JSON.stringify(data, null, 2));
data.site.pages.forEach((page) => {
if (page.file && !page.file.includes('http')) {
var pageName = page.file.replace(".html", "").toLowerCase();
var fileName = page.file;
createPage(pageName, data, path.join(dest, fileName));
}
});
function insertPartials(templateName) {
var completeTemplate = fs.readFileSync(path.join(src, config.templatesDirectory, templateName + ".hbs"), "utf8");
// Read all partial files from the partialsDirectory
const partialFiles = fs.readdirSync(path.join(src, config.partialsDirectory)).filter(file => file.endsWith(".hbs"));
// Register each partial automatically
partialFiles.forEach((partialFile) => {
try {
var partialName = partialFile.replace(".hbs", ""); // Remove the .hbs extension to get the partial name
var partialTemplate = fs.readFileSync(path.join(src, config.partialsDirectory, partialFile), "utf8");
handlebars.registerPartial(partialName, partialTemplate); // Register the partial
console.log(`Registered partial: ${partialName}`); // Debugging log
} catch (error) {
console.error(`Error loading partial: ${partialFile} - ${error.message}`);
}
});
return completeTemplate;
}
function createPage(templateName, data, outputFileName) {
var html = renderFromExternalTemplate(insertPartials(templateName), data);
fs.writeFileSync(outputFileName, html);
}
function renderFromExternalTemplate(template, data){
var template = handlebars.compile(template);
return template(data);
}
function truncate(string, length){
if (string.length > length)
return string.substring(0, length)+'...';
else
return string;
};
function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path+'/'+file).isDirectory();
});
}
function makeDirIfNotExist(filePath) {
if (!fs.existsSync(filePath)){
fs.mkdirSync(filePath);
}
}