The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - Effect's FileSystem has no rmdir. |
| 2 | import * as NodeFSP from "node:fs/promises"; |
| 3 | |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | |
| 7 | export class RemoveEmptyDirectoryError extends Schema.TaggedError<RemoveEmptyDirectoryError>()( |
| 8 | "RemoveEmptyDirectoryError", |
| 9 | { path: Schema.String, cause: Schema.Defect() }, |
| 10 | ) { |
| 11 | override get message(): string { |
| 12 | return `Could not remove the directory ${this.path}.`; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Removes `path` only while it is an empty directory. Succeeds with false when |
| 18 | * anything else is there, including files written after the caller looked. |
| 19 | * Other failures, such as a permission error, stay errors. |
| 20 | */ |
| 21 | export const removeEmptyDirectory = (path: string) => |
| 22 | Effect.tryPromise({ |
| 23 | try: () => NodeFSP.rmdir(path), |
| 24 | catch: (cause) => new RemoveEmptyDirectoryError({ path, cause }), |
| 25 | }).pipe( |
| 26 | Effect.as(true), |
| 27 | Effect.catchIf( |
| 28 | (error) => (error.cause as NodeJS.ErrnoException | undefined)?.code === "ENOTEMPTY", |
| 29 | () => Effect.succeed(false), |
| 30 | ), |
| 31 | ); |