// @bun
import {
build,
changelog,
clean,
cloud,
commit,
configure,
deploy,
dev,
dns,
domains,
fresh,
generate,
http,
install,
key,
lint,
list,
make,
migrate,
ports,
prepublish,
projects,
release,
seed,
setup,
test,
tinker,
upgrade,
version
} from "./chunk-a2931e584dac2d9f.js";
import"./chunk-970e033f764e29fc.js";

// src/cli.ts
import process from "process";
import {runAction} from "@stacksjs/actions";
import {cli as cli2, log} from "@stacksjs/cli";
import {Action} from "@stacksjs/enums";
import {path as p} from "@stacksjs/path";
import {fs} from "@stacksjs/storage";
import {ensureProjectIsInitialized} from "@stacksjs/utils";
async function main() {
  const buddy = cli2("buddy");
  setup(buddy);
  key(buddy);
  const isAppKeySet = await ensureProjectIsInitialized();
  if (isAppKeySet) {
    log.debug("Project is initialized");
  } else {
    log.info("Your `APP_KEY` is not yet set");
    log.info("Generating application key...");
    const result = await runAction(Action.KeyGenerate);
    if (result.isErr()) {
      log.error("Failed to set random application key.", result.error);
      process.exit();
    }
  }
  build(buddy);
  changelog(buddy);
  clean(buddy);
  cloud(buddy);
  commit(buddy);
  configure(buddy);
  dev(buddy);
  domains(buddy);
  deploy(buddy);
  dns(buddy);
  ports(buddy);
  projects(buddy);
  fresh(buddy);
  generate(buddy);
  http(buddy);
  install(buddy);
  lint(buddy);
  list(buddy);
  make(buddy);
  migrate(buddy);
  release(buddy);
  seed(buddy);
  setup(buddy);
  test(buddy);
  tinker(buddy);
  version(buddy);
  prepublish(buddy);
  upgrade(buddy);
  await dynamicImports(buddy);
  buddy.help();
  buddy.parse();
}
async function dynamicImports(buddy) {
  const commandsDir = p.appPath("Commands");
  const commandFiles = fs.readdirSync(commandsDir).filter((file) => file.endsWith(".ts"));
  for (const file of commandFiles) {
    const commandPath = `${commandsDir}/${file}`;
    const dynamicImport = await import(commandPath);
    if (typeof dynamicImport.default === "function")
      dynamicImport.default(buddy);
    else
      console.error(`Expected a default export function in ${file}, but got:`, dynamicImport.default);
  }
  const listenerImport = await import(p.listenersPath("Console.ts"));
  if (typeof listenerImport.default === "function")
    listenerImport.default(buddy);
}
process.on("uncaughtException", (error) => {
  log.debug("uncaughtException");
  log.error(error);
  process.exit(1);
});
process.on("unhandledRejection", (error) => {
  log.debug("unhandledRejection");
  log.error(error);
  process.exit(1);
});
await main();
