import "reflect-metadata";
import { existsSync } from "node:fs";
import { resolve } from "node:path";
import { NestFactory } from "@nestjs/core";

function loadEnv() {
  const loadEnvFile = process.loadEnvFile as ((path?: string) => void) | undefined;
  if (!loadEnvFile) return;

  for (const envPath of [resolve(process.cwd(), ".env"), resolve(process.cwd(), "../.env"), resolve(process.cwd(), "../../.env")]) {
    if (existsSync(envPath)) {
      loadEnvFile(envPath);
      return;
    }
  }
}

loadEnv();

async function bootstrap() {
  const { AppModule } = await import("./app.module");
  const app = await NestFactory.createApplicationContext(AppModule);
  await app.init();
  // eslint-disable-next-line no-console
  console.log("worker started");
}

bootstrap();
