-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
61 lines (55 loc) · 1.83 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
const fs = require("fs");
const path = require("path");
const Handlebars = require("handlebars");
const config = require("./build.config");
function buildHTML(filename, data) {
const source = fs.readFileSync(filename, "utf8").toString();
const template = Handlebars.compile(source);
const output = template(data);
return output;
}
function copyIfNewer(relativePath, srcDir, destDIr) {
const destPath = path.join(destDIr, relativePath);
const srcPath = path.join(srcDir, relativePath);
if (fs.existsSync(destPath)) {
const destMT = fs.statSync(destPath).mtime;
const srcMT = fs.statSync(srcPath).mtime;
if (srcMT <= destMT) {
console.log(destPath, "alreay exist");
return;
}
}
fs.mkdirSync(path.dirname(destPath), { recursive: true });
fs.copyFileSync(srcPath, destPath);
console.log("copied", destPath);
}
function listFileRecursive(Directory, oFiles) {
fs.readdirSync(Directory).forEach((File) => {
const Absolute = path.join(Directory, File);
if (fs.statSync(Absolute).isDirectory())
return listFileRecursive(Absolute, oFiles);
else {
oFiles.push(Absolute);
}
});
}
async function main() {
// handlebars render html
for (const file of config.files) {
const html = buildHTML(path.join(config.src, file.path), file.variables);
const outfilePath = path.join(config.output, file.path)
fs.mkdirSync(path.dirname(outfilePath), {recursive:true})
fs.writeFile(outfilePath, html, function (err) {
if (err) return console.log(err);
console.log(`${file.path} generated.`);
});
}
// copy static files to dist
const static_files = [];
listFileRecursive(config.static, static_files);
for (const file of static_files) {
const relativePath = path.relative(config.static, file);
copyIfNewer(relativePath, config.static, config.output);
}
}
main();