rm/rmdir/touch/mkdir

This commit is contained in:
Chris Wanstrath 2025-09-28 15:15:04 -07:00
parent 9fe788a35f
commit 30dde3349f
5 changed files with 78 additions and 0 deletions

13
app/nose/bin/mkdir.ts Normal file
View File

@ -0,0 +1,13 @@
import { mkdirSync } from "fs"
import { join } from "path"
import { projectDir, projectFiles } from "@/project"
export default async function (path: string) {
if (path.endsWith("/")) path = path.slice(0, path.length - 1)
for (const file of projectFiles())
if (file.name === path) throw `${path} exists`
mkdirSync(join(projectDir(), path), { recursive: true })
return `${path} created`
}

24
app/nose/bin/rm.ts Normal file
View File

@ -0,0 +1,24 @@
import { unlinkSync } from "fs"
import { join } from "path"
import { projectDir, projectFiles } from "@/project"
export default function (path: string) {
let target = ""
if (path.endsWith("/")) path = path.slice(0, path.length - 1)
for (const file of projectFiles()) {
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(projectDir(), path))
return `${path} removed`
}

24
app/nose/bin/rmdir.ts Normal file
View File

@ -0,0 +1,24 @@
import { rmdirSync } from "fs"
import { join } from "path"
import { projectDir, projectFiles } from "@/project"
export default function (path: string) {
let target = ""
if (path.endsWith("/")) path = path.slice(0, path.length - 1)
for (const file of projectFiles()) {
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(projectDir(), path), { recursive: true })
return `${path} removed`
}

12
app/nose/bin/touch.ts Normal file
View File

@ -0,0 +1,12 @@
import { join } from "path"
import { projectDir, projectFiles } from "@/project"
export default async function (path: string) {
if (path.endsWith("/")) path = path.slice(0, path.length - 1)
for (const file of projectFiles())
if (file.name === path) throw `${path} exists`
await Bun.write(join(projectDir(), path), "")
return `${path} created`
}

View File

@ -1,6 +1,7 @@
////
// Helpers for working with projects in the CLI.
import { readdirSync, type Dirent } from "fs"
import { getState } from "./session"
import { appDir } from "./webapp"
@ -19,4 +20,8 @@ export function projectDir(): string {
if (!root) throw "error loading project"
return root
}
export function projectFiles(): Dirent[] {
return readdirSync(projectDir(), { withFileTypes: true })
}