import { readdirSync, copyFileSync, mkdirSync, watch } from "fs";
import { join } from "path";

function copyAllFiles(srcDir: string, destDir: string) {
  const entries = readdirSync(srcDir, { withFileTypes: true });

  for (const entry of entries) {
    const srcPath = join(srcDir, entry.name);
    const destPath = join(destDir, entry.name);

    if (entry.isDirectory()) {
      mkdirSync(destPath, { recursive: true });
      copyAllFiles(srcPath, destPath); // recursive
    } else if (entry.isFile()) {
      copyFileSync(srcPath, destPath);
    }
  }
}
function build() {
  Bun.build({
    entrypoints: ["src/index.tsx"],
    minify: false,
    sourcemap: true,
    outdir: "dist/",
  })

  copyAllFiles("./public", "./dist");
}

const watcher = watch(import.meta.dir + "\\src", {recursive: true}, (event, filename) => {
  console.log(`detected ${event} in ${filename}`);
  build();
});

console.log("initial build");
build();