import { readdirSync } from "node:fs";
import path from "path";

const paths = [
  './src/functions'
];

const functionFiles = paths.map(path => readdirSync(path).map(file => path + "/" + file)).flat();

// for hot reload to work, we import all the files we want to build
const imports = functionFiles.forEach(file => require('./' + path.relative('src', file)));


console.log("functions to build:", functionFiles);

const bundledModules = ['zod_utilz'];

const externalModules = Object.keys(require('../package.json').dependencies)
    .filter(dep => !bundledModules.includes(dep));

console.log("Explicitly bundling dependencies", bundledModules);

// console.log("external modules: ", externalModules);

console.log("Building functions...");

await Bun.build({
  entrypoints: functionFiles,
  outdir: './build/functions',
  packages: 'bundle',
  target: 'node',
  external: externalModules,
  root: './src/functions',
  format: 'cjs',
});
