-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildtool.mjs
182 lines (162 loc) · 4.81 KB
/
buildtool.mjs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { exec } from "child_process";
import chokidar from "chokidar";
import path from "path";
import fs from "fs";
import chalk from "chalk";
import { Command } from "commander";
import dotenv from "dotenv";
dotenv.config();
const SRC_JS = path.resolve("src/js");
const TEMPLATES = path.resolve("templates/**/*.html");
const CONTENT = path.resolve("content/**/*");
const STATIC_JS = path.resolve("static/js");
const program = new Command();
function runCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (err, stdout, stderr) => {
if (err) {
console.error(chalk.red(`Error executing ${command}: ${stderr}`));
reject(err);
} else {
console.log(chalk.green(stdout));
resolve(stdout);
}
});
});
}
async function compressJS(dir) {
console.log(chalk.blue(`Compressing JavaScript files in ${dir}...`));
const entryPoint = path.join(SRC_JS, dir, "index.mjs");
const outputFile = path.join(STATIC_JS, `${dir}.js`);
try {
const startTime = Date.now();
await runCommand(
`npx esbuild ${entryPoint} --bundle --minify --outfile=${outputFile}`
);
const endTime = Date.now();
const duration = endTime - startTime;
console.log(chalk.green(`Compressed JS output to ${outputFile}`));
console.log(chalk.green(`Done in ${duration} ms`));
} catch (error) {
console.error(chalk.red(`Failed to compress JS for ${dir}`, error));
}
}
async function grimoireBuild() {
console.log(chalk.blue("Building CSS with Grimoire..."));
try {
const startTime = Date.now();
await runCommand("gcssjs build");
const endTime = Date.now();
const duration = endTime - startTime;
console.log(chalk.green("Grimoire CSS build complete"));
console.log(chalk.green(`Done in ${duration} ms`));
} catch (error) {
console.error(chalk.red("Failed to build Grimoire CSS", error));
}
}
async function zolaBuild() {
console.log(chalk.blue("Building site with Zola..."));
try {
const startTime = Date.now();
await runCommand("zola build");
const endTime = Date.now();
const duration = endTime - startTime;
console.log(chalk.green("Zola build complete"));
console.log(chalk.green(`Done in ${duration} ms`));
} catch (error) {
console.error(chalk.red("Failed to build site with Zola", error));
}
}
async function generateCacheList() {
console.log(chalk.blue("Generating cache list..."));
try {
const startTime = Date.now();
await runCommand("node generate-cache-list.js");
const endTime = Date.now();
const duration = endTime - startTime;
console.log(chalk.green("Cache list generation complete"));
console.log(chalk.green(`Done in ${duration} ms`));
} catch (error) {
console.error(chalk.red("Failed to generate cache list", error));
}
}
function getDirectories(srcPath) {
return fs
.readdirSync(srcPath)
.filter((file) => fs.statSync(path.join(srcPath, file)).isDirectory());
}
async function buildAll() {
const dirs = getDirectories(SRC_JS);
for (const dir of dirs) {
await compressJS(dir);
}
await grimoireBuild();
await zolaBuild();
await generateCacheList();
console.log(
chalk.cyan(`Full build complete [${new Date().toLocaleTimeString()}]`)
);
}
async function handleChange(filePath) {
console.log(chalk.yellow(`File changed: ${filePath}`));
try {
if (filePath.startsWith(SRC_JS)) {
const dir = path.basename(path.dirname(filePath));
await compressJS(dir);
} else {
await grimoireBuild();
}
console.log(
chalk.cyan(`Rebuild complete [${new Date().toLocaleTimeString()}]`)
);
} catch (error) {
console.error(chalk.red(`Error handling change for ${filePath}`, error));
}
}
function startWatcher() {
const watcher = chokidar.watch([SRC_JS, TEMPLATES, CONTENT], {
ignored: /(^|[\/\\])\../, // ignore dotfiles
persistent: true,
});
console.log(chalk.magenta("Watching for JavaScript and Grimoire changes..."));
watcher
.on("change", handleChange)
.on("error", (error) =>
console.error(chalk.red(`Watcher error: ${error}`))
);
}
program
.command("dev")
.description("Run the development watcher")
.action(() => {
startWatcher();
});
program
.command("build")
.description("Build the entire project")
.action(async () => {
await buildAll();
});
program
.command("buildGrimoire")
.description("Build only Grimoire CSS")
.action(async () => {
await grimoireBuild();
});
program
.command("buildJS")
.description("Build only JavaScript")
.action(async () => {
const dirs = getDirectories(SRC_JS);
for (const dir of dirs) {
await compressJS(dir);
}
});
program
.command("buildZola")
.description("Build only Zola")
.action(async () => {
await zolaBuild();
await generateCacheList();
});
program.parse(process.argv);