-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
59 lines (50 loc) · 1.66 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
import fs from "fs-extra";
import path from "path";
import { fileURLToPath } from "url";
import { parse } from "./parser.js";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const deployDir = path.join(__dirname, "deploy");
const srcDir = path.join(__dirname, "src");
async function compileHtmlFile(filePath) {
try {
const originalHtml = fs.readFileSync(filePath, "utf-8");
const transformedHtml = await parse(originalHtml);
const relativePath = path.relative(srcDir, filePath);
const outputPath = path.join(deployDir, relativePath);
await fs.ensureDir(path.dirname(outputPath));
await fs.writeFile(outputPath, transformedHtml, "utf-8");
console.log(`Compiled ${filePath} -> ${outputPath}`);
} catch (err) {
console.error(`Error compiling ${filePath}:`, err);
}
}
async function compileAllHtml() {
const pattern = `${srcDir}/**/*.html`;
const files = await import('glob').then(glob => glob.sync(pattern, {
ignore: ["**/components/**"]
}));
for (const file of files) {
await compileHtmlFile(file);
}
}
async function copyStatic() {
const staticDir = path.join(__dirname, "static");
if (await fs.pathExists(staticDir)) {
await fs.copy(staticDir, deployDir, {
recursive: true
});
console.log(`Copied static folder to ${deployDir}`);
} else {
console.log("No static folder found.");
}
}
function cleanDeploy() {
return fs.emptyDir(deployDir);
}
await (async () => {
await cleanDeploy();
await compileAllHtml();
await copyStatic();
})().catch(err => {
console.error(err);
});