From 30dde3349f834f2f7b73ff5435f55065241c4dc3 Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 28 Sep 2025 15:15:04 -0700 Subject: [PATCH] rm/rmdir/touch/mkdir --- app/nose/bin/mkdir.ts | 13 +++++++++++++ app/nose/bin/rm.ts | 24 ++++++++++++++++++++++++ app/nose/bin/rmdir.ts | 24 ++++++++++++++++++++++++ app/nose/bin/touch.ts | 12 ++++++++++++ app/src/project.ts | 5 +++++ 5 files changed, 78 insertions(+) create mode 100644 app/nose/bin/mkdir.ts create mode 100644 app/nose/bin/rm.ts create mode 100644 app/nose/bin/rmdir.ts create mode 100644 app/nose/bin/touch.ts diff --git a/app/nose/bin/mkdir.ts b/app/nose/bin/mkdir.ts new file mode 100644 index 0000000..83e19dd --- /dev/null +++ b/app/nose/bin/mkdir.ts @@ -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` +} \ No newline at end of file diff --git a/app/nose/bin/rm.ts b/app/nose/bin/rm.ts new file mode 100644 index 0000000..b49be23 --- /dev/null +++ b/app/nose/bin/rm.ts @@ -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` +} \ No newline at end of file diff --git a/app/nose/bin/rmdir.ts b/app/nose/bin/rmdir.ts new file mode 100644 index 0000000..480d030 --- /dev/null +++ b/app/nose/bin/rmdir.ts @@ -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` +} \ No newline at end of file diff --git a/app/nose/bin/touch.ts b/app/nose/bin/touch.ts new file mode 100644 index 0000000..404926a --- /dev/null +++ b/app/nose/bin/touch.ts @@ -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` +} \ No newline at end of file diff --git a/app/src/project.ts b/app/src/project.ts index 66fb9dc..91ce1ec 100644 --- a/app/src/project.ts +++ b/app/src/project.ts @@ -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 }) } \ No newline at end of file