-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
build.js
50 lines (41 loc) · 1.42 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
const fs = require('fs');
const path = require('path');
const ejs = require('ejs');
const srcDir = path.join(__dirname, 'html');
const distDir = path.join(__dirname, 'static');
// Ensure the dist directory exists
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir);
}
// Function to compile an EJS file to HTML
function compileFile(file) {
const filePath = path.join(srcDir, file);
let outputFilePath;
if (file === 'index.ejs') {
outputFilePath = path.join(distDir, 'index.html');
} else {
const fileNameWithoutExt = path.basename(file, '.ejs');
const outputDir = path.join(distDir, fileNameWithoutExt);
// Ensure the output directory exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
outputFilePath = path.join(outputDir, 'index.html');
}
ejs.renderFile(filePath, {}, (err, str) => {
if (err) {
console.error(`Error compiling ${filePath}:`, err);
return;
}
fs.writeFileSync(outputFilePath, str);
console.log(`Compiled ${file} to ${outputFilePath}`);
});
}
// Read all .ejs files from the src directory and compile them
fs.readdirSync(srcDir).forEach(file => {
// Obtain the file name
const fileName = path.basename(file);
if (path.extname(file) === '.ejs' && !fileName.startsWith('include-')) {
compileFile(file);
}
});