-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
159 lines (139 loc) · 3.89 KB
/
build.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const mustache = require("mustache");
const fs = require("fs");
const path = require("path");
const cheerio = require("cheerio");
/**
* Get a template file and return the contents
*
* @param {string} templateName the name of the template file
* @return {string} template text
*/
function getTemplate(templateName) {
return fs.readFileSync("commonsrc/" + templateName, "utf8");
}
/**
* Get the directory that a file is member of
*
* @param {string} fileName
* @return {string} parent directory
*/
function getDir(fileName) {
return path.dirname(fileName);
}
/**
* Mangle a template and a json object containing data to mangle and return the
* mangled text
*
* @param {string} template template string
* @param {json} mangle json containing mangle data
* @return {string} mangled template
*/
function doMangle(template, mangle) {
return mustache.render(template, mangle);
}
/**
*
* @param {string} string string to write
* @param {string} outFile file to save to
*/
function doWrite(string, outFile) {
fs.mkdir(getDir(outFile), { recursive: true }, function(error) {
return console.log(error);
});
fs.writeFile(outFile, string, function(error) {
return console.log(error);
});
}
/**
*
* @param {string} templateName
* @param {json} mangle
* @param {string} outFile
*/
async function doRemoteMangleAndWrite(templateName, mangle, outFile) {
const template = getTemplate(templateName);
doWrite(doMangle(template, mangle), outFile);
}
/**
*
* @param {string} file file containing page data
* @return {json} page with name, url, content, and scripts
*/
function getPageData(file) {
const data = fs.readFileSync("src/" + file, "utf8");
const sections = cheerio.load(data);
const page = {
pageName: sections("title").text(),
pageUrl: file.split(".")[0] + ".html",
pageContent: [],
pageScript: [],
inlineScript: [],
};
sections("section").each(function(index, element) {
page.pageContent.push({
htmlContent: sections(element).html(),
primary: index % 2 === 0,
});
});
sections("script").each(function(index, element) {
page.pageScript.push({ script: sections(element).html() });
});
sections("inline-script").each(function(index, element) {
page.inlineScript.push({ script: sections(element).html() });
});
return page;
}
const rawData = JSON.parse(fs.readFileSync("src/config.json"));
const files = rawData.files;
const base = rawData.base;
const pages = [];
for (let index = 0; index < files.length; index++) {
pages.push(getPageData(files[index]));
}
/**
* Populate navContent
*/
base.navContent = [];
for (let index = 0; index < pages.length; index++) {
base.navContent.push({
text: pages[index].pageName,
current: false,
link: pages[index].pageUrl,
});
}
/**
* Populate shortcuts (android home screen shortcuts )
*/
base.shortcut = [];
for (let index = 0; index < pages.length; index++) {
if (fs.existsSync("./images/shorticons/" + pages[index].pageUrl.replace(
".html", ".png"))) {
base.shortcut.push({
name: pages[index].pageName,
link: pages[index].pageUrl,
icon: pages[index].pageUrl.replace(".html", ".png"),
});
}
}
base.isShortcuts = base.shortcut.length > 0;
// Add pages to precache files
for (let index = 0; index < pages.length; index++) {
base.precacheFiles.push({
fileName: pages[index].pageUrl,
});
}
for (let index = 0; index < pages.length; index++) {
const page = JSON.parse(JSON.stringify(base)); // create a copy of base
page.navContent[index].current = true;
page.pageContent = pages[index].pageContent;
page.pageName = pages[index].pageName;
page.pageScript = pages[index].pageScript;
page.inlineScript = pages[index].inlineScript;
page.stylesNamespace = (index % 4);
// write the page
doRemoteMangleAndWrite("base.template.html", page, pages[index].pageUrl);
}
// Manifest
doRemoteMangleAndWrite("manifest.template.json", base, "manifest.json");
// Service Worker
doRemoteMangleAndWrite("sw.template.js", base, "sw.js");