//// 
// Helpers for working with projects in the CLI.

import { readdirSync, type Dirent } from "fs"
import { sessionGet } from "./session"
import { appDir } from "./webapp"

export function projectName(): string {
  const state = sessionGet()
  if (!state) throw "no state"

  const project = state.project
  if (!project) throw "no project loaded"

  return project
}

export function projectDir(): string {
  const root = appDir(projectName())
  if (!root) throw "error loading project"

  return root
}

export function projectFiles(): Dirent[] {
  return readdirSync(projectDir(), { recursive: true, withFileTypes: true })
}