nose-pluto/app/nose/bin/rm.ts
2025-09-29 19:08:04 -07:00

27 lines
691 B
TypeScript

// Remove a file.
import { unlinkSync, 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.isDirectory())
return { error: "Use `rmdir` to remove directory" }
target = file.name
break
}
if (!target)
return { error: `${path} not found` }
unlinkSync(join(root, path))
return `${path} removed`
}