// Remove a directory, and all its children.

import { rmdirSync, readdirSync } from "fs"
import { join } from "path"
import { projectDir } from "@/project"
import { sessionGet } from "@/session"

export default function (path: string) {
  let target = ""
  if (path.endsWith("/")) path = path.slice(0, path.length - 1)

  const root = sessionGet("cwd") || projectDir()
  for (const file of readdirSync(root, { withFileTypes: true }))
    if (file.name === path) {
      if (file.isFile())
        return { error: "Use `rm` to remove files" }

      target = file.name
      break
    }

  if (!target)
    return { error: `${path} not found` }

  rmdirSync(join(root, path), { recursive: true })
  return `${path} removed`
}