@jsnchn/buntastic 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +29 -40
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsnchn/buntastic",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "A simple static site generator built with Bun",
5
5
  "type": "module",
6
6
  "bin": {
package/src/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { mkdir, writeFile, readFile, copyFile, exists } from "fs/promises";
2
- import { watch as fsWatch } from "fs/promises";
2
+ import { watch as fsWatch } from "node:fs";
3
+ import { spawn } from "node:child_process";
3
4
  import { join, relative, dirname, extname } from "path";
4
5
  import { Glob } from "bun";
5
6
 
@@ -294,48 +295,36 @@ async function dev(): Promise<void> {
294
295
 
295
296
  await build(false);
296
297
 
297
- const watcher1 = fsWatch(CONTENT_DIR, { recursive: true });
298
- const watcher2 = fsWatch(LAYOUTS_DIR, { recursive: true });
299
- const watcher3 = fsWatch(PUBLIC_DIR, { recursive: true });
298
+ let buildRunning = false;
299
+ let debounceTimer: ReturnType<typeof setTimeout> | undefined;
300
300
 
301
- const watchers = [watcher1, watcher2, watcher3];
301
+ function runBuild() {
302
+ if (buildRunning) return;
303
+ buildRunning = true;
304
+ console.log("Rebuilding...");
305
+ const proc = spawn("bun", ["run", "build"], {
306
+ stdio: "inherit",
307
+ env: { ...process.env, NODE_ENV: "development" },
308
+ });
309
+ proc.on("exit", () => {
310
+ buildRunning = false;
311
+ });
312
+ }
302
313
 
303
- let isBuilding = false;
304
- let debounceTimer: ReturnType<typeof setTimeout> | undefined;
305
- const rebuild = () => {
306
- clearTimeout(debounceTimer);
307
- debounceTimer = setTimeout(async () => {
308
- if (isBuilding) return;
309
- isBuilding = true;
310
- console.log("Rebuilding...");
311
- try {
312
- await build(false);
313
- } finally {
314
- isBuilding = false;
315
- }
316
- }, 100);
317
- };
314
+ function scheduleBuild() {
315
+ if (debounceTimer) clearTimeout(debounceTimer);
316
+ debounceTimer = setTimeout(runBuild, 100);
317
+ }
318
318
 
319
- (async () => {
320
- for (const watcher of watchers) {
321
- (async () => {
322
- for await (const event of watcher) {
323
- const filename = event.filename || "";
324
- const isPublicFile = filename.includes("public");
325
-
326
- if (isPublicFile) {
327
- const ext = filename.split(".").pop()?.toLowerCase();
328
- if (ext !== "css" && ext !== "js" && ext !== "ts") {
329
- continue;
330
- }
331
- }
332
-
333
- console.log(`[${event.eventType}] ${event.filename} - rebuilding...`);
334
- rebuild();
335
- }
336
- })();
337
- }
338
- })();
319
+ const watchDirs = [CONTENT_DIR, LAYOUTS_DIR, PUBLIC_DIR];
320
+ for (const dir of watchDirs) {
321
+ fsWatch(dir, { recursive: true }, (eventType, filename) => {
322
+ if (!filename) return;
323
+ if (filename.endsWith("~") || filename.startsWith(".")) return;
324
+ console.log(`[${eventType}] ${filename} - rebuilding...`);
325
+ scheduleBuild();
326
+ });
327
+ }
339
328
  }
340
329
 
341
330
  async function preview(): Promise<void> {