forked from k0tet/k0tet.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
45 lines (37 loc) · 1.39 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
const fs = require('fs-extra');
const klaw = require('klaw');
const path = require('path');
const templateTag = 'build';
const processFile = function (item) {
if (!item.stats.isDirectory()) {
var filePath = path.relative('src', item.path);
if (path.extname(item.path) === '.html') {
fs.outputFileSync('dist/' + filePath, applyTemplate(item.path));
console.log(filePath + ' copied and template applied');
} else {
fs.copySync(item.path, 'dist/' + path.relative('src', item.path));
console.log(filePath + ' copied');
}
}
};
var template = fs.readFileSync('template.html').toString();
var templateRegex = new RegExp('<!--\\s*' + templateTag + ':([^\\s]+)\\s*-->', 'g');
fs.emptyDirSync('dist');
klaw('src')
.on('data', processFile)
.on('end', () => console.log("done"))
function applyTemplate(filePath) {
var content = fs.readFileSync(filePath).toString();
var mergedContent = template.replace(templateRegex, function (match, templateTagName) {
var find = content.match(generateContentRegex(templateTagName));
return find ? find[1] : match;
} )
return mergedContent;
}
function generateContentRegex(templateTagName) {
return new RegExp(
'<!--\\s*' + templateTag + ':' + templateTagName + '\\s*-->' +
'((.|[\\r\\n])+)' +
'<!--\\s*\\/' + templateTag + ':' + templateTagName + '\\s*-->'
);
}