postbuild-server.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /**
  2. * Post-build script for server compilation.
  3. *
  4. * tspc compiles both `src/` and `config/` (TypeScript files under config/),
  5. * so the output directory (`transpiled/`) mirrors the source tree structure
  6. * (e.g. `transpiled/src/`, `transpiled/config/`).
  7. *
  8. * Setting `rootDir: "src"` and `outDir: "dist"` in tsconfig would eliminate this script,
  9. * but that would break once `config/` is included in the compilation.
  10. *
  11. * This script:
  12. * 1. Extracts `transpiled/src/` into `dist/`
  13. * 2. Copies compiled `transpiled/config/` files into `config/` so that
  14. * relative imports from `dist/` (e.g. `../../../config/logger/config.dev`)
  15. * resolve correctly at runtime.
  16. */
  17. import { cpSync, existsSync, readdirSync, renameSync, rmSync } from 'node:fs';
  18. const TRANSPILED_DIR = 'transpiled';
  19. const DIST_DIR = 'dist';
  20. const SRC_SUBDIR = `${TRANSPILED_DIR}/src`;
  21. const CONFIG_SUBDIR = `${TRANSPILED_DIR}/config`;
  22. // List transpiled contents for debugging
  23. // biome-ignore lint/suspicious/noConsole: This is a build script, console output is expected.
  24. console.log('Listing files under transpiled:');
  25. // biome-ignore lint/suspicious/noConsole: This is a build script, console output is expected.
  26. console.log(readdirSync(TRANSPILED_DIR).join('\n'));
  27. // Remove old dist
  28. rmSync(DIST_DIR, { recursive: true, force: true });
  29. // Move transpiled/src -> dist
  30. renameSync(SRC_SUBDIR, DIST_DIR);
  31. // Copy compiled config files to app root config/ so runtime imports resolve
  32. if (existsSync(CONFIG_SUBDIR)) {
  33. cpSync(CONFIG_SUBDIR, 'config', { recursive: true, force: true });
  34. }
  35. // Remove leftover transpiled directory
  36. rmSync(TRANSPILED_DIR, { recursive: true, force: true });