#! /usr/bin/env bun
import { fileURLToPath } from "url";
import { version } from "./package.json";
import { Hono } from "hono";
import { serveStatic } from "hono/bun";
import { consola } from "consola";
import "@colors/colors";
import {
  intro,
  outro,
  confirm,
  select,
  spinner,
  isCancel,
  cancel,
  text,
} from "@clack/prompts";
import fs from "fs-extra";
import path from "path";
import color from "picocolors";
import { program } from "commander";
const ROOT_DIR = process.cwd();

var ROOT_PROJECT;

program.name("@libv/studio").version(version, "-v");

// start studio
program
  .command("start")
  .option("-p, --port <port>", "Port to run studio")
  .action((args, options) => {
    try {
      const app = new Hono();
      const port = Number(options.port || 5555);
      app.use("/*", serveStatic({ root: "./app" }));
      Bun.serve({
        fetch: app.fetch,
        port: port,
      });
      console.log(`Studio URL : http://localhost:${port}`.gray);
    } catch (err) {
      consola.error(err?.message);
    }
  });

program.parse();

process.on("SIGTERM", (signal) => {
  console.log(`Process ${process.pid} received a SIGTERM signal`);
  process.exit(0);
});

process.on("SIGINT", (signal) => {
  console.log(`Studio stopped successfully`);
  process.exit(0);
});

process.on("uncaughtException", (err) => {
  console.log(`Uncaught Exception: ${err.message}`);
  process.exit(1);
});
