-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
57 lines (47 loc) · 1.67 KB
/
build.js
File metadata and controls
57 lines (47 loc) · 1.67 KB
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
const nunjucks = require('nunjucks');
const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
const beautify = require('js-beautify').html;
const srcDir = path.join(__dirname, 'source');
const outDir = path.join(__dirname, 'public');
// Configure nunjucks environment
nunjucks.configure(srcDir, {
autoescape: true,
noCache: true,
trimBlocks: true,
lstripBlocks: true
});
function buildFile(file) {
if (!file.endsWith('.njk')) return;
if (path.basename(file).startsWith('_')) return;
const relativePath = path.relative(srcDir, file);
const outFile = path.join(outDir, relativePath.replace(/\.njk$/, '.html'));
console.log(`🛠 Rendering ${relativePath} → ${path.relative(outDir, outFile)}`);
const html = nunjucks.render(relativePath, {
siteName: "Captain Harris" // global variable
});
const formattedHtml = beautify(html, {
indent_with_tabs: true,
indent_size: 1,
preserve_newlines: true,
max_preserve_newlines: 1
});
fs.mkdirSync(path.dirname(outFile), { recursive: true });
fs.writeFileSync(outFile, formattedHtml, 'utf8');
}
// Initial build of all njk files
function buildAll(dir = srcDir) {
fs.readdirSync(dir, { withFileTypes: true }).forEach(dirent => {
const fullPath = path.join(dir, dirent.name);
if (dirent.isDirectory()) {
buildAll(fullPath);
} else {
if (fullPath.endsWith('.njk')) buildFile(fullPath);
}
});
}
// Watch for changes
chokidar.watch(srcDir).on('change', buildFile).on('add', buildFile);
buildAll();
console.log('👀 Watching source .njk files for changes...');