Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

removeEmptyDirectory.ts

18 lines679 Bytes

1// @effect-diagnostics nodeBuiltinImport:off - Effect's FileSystem has no rmdir.
2import * as NodeFSP from "node:fs/promises";
3​
4import * as Effect from "effect/Effect";
5​
6/**
7 * Removes `path` only while it is an empty directory. Succeeds with false when
8 * anything else is there, including files written after the caller looked.
9 * Other failures, such as a permission error, stay errors.
10 */
11export const removeEmptyDirectory = (path: string) =>
12 Effect.tryPromise(() => NodeFSP.rmdir(path)).pipe(
13 Effect.as(true),
14 Effect.catchIf(
15 (error) => (error.cause as NodeJS.ErrnoException | undefined)?.code === "ENOTEMPTY",
16 () => Effect.succeed(false),
17 ),
18 );