Repository navigation
Expand file tree
/
Copy pathDesktopPreReadyFileSystem.ts
More file actions
65 lines (61 loc) · 1.99 KB
/
Copy pathDesktopPreReadyFileSystem.ts
File metadata and controls
65 lines (61 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// @effect-diagnostics nodeBuiltinImport:off -- Effect's Node FileSystem is async, and pre-ready startup must not yield before the Clerk bridge registers its privileged scheme.
import * as NodeFS from "node:fs";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as PlatformError from "effect/PlatformError";
const systemErrorTag = (cause: unknown): PlatformError.SystemErrorTag => {
switch ((cause as NodeJS.ErrnoException | undefined)?.code) {
case "EEXIST":
return "AlreadyExists";
case "ENOENT":
return "NotFound";
case "EACCES":
case "EPERM":
return "PermissionDenied";
default:
return "Unknown";
}
};
const syncFs = <A>(method: string, path: string, run: () => A) =>
Effect.try({
try: run,
catch: (cause) =>
PlatformError.systemError({
_tag: systemErrorTag(cause),
module: "FileSystem",
method,
pathOrDescriptor: path,
cause,
}),
});
/** @public */
export const make = FileSystem.makeNoop({
exists: (path) =>
syncFs("exists", path, () => {
NodeFS.accessSync(path);
return true;
}).pipe(
Effect.catchIf(
(error) => error.reason._tag === "NotFound",
() => Effect.succeed(false),
),
),
readFileString: (path) => syncFs("readFileString", path, () => NodeFS.readFileSync(path, "utf8")),
makeDirectory: (path, options) =>
syncFs("makeDirectory", path, () => {
NodeFS.mkdirSync(path, { recursive: options?.recursive ?? false });
}),
remove: (path, options) =>
syncFs("remove", path, () =>
NodeFS.rmSync(path, {
recursive: options?.recursive ?? false,
force: options?.force ?? false,
}),
),
writeFileString: (path, data, options) =>
syncFs("writeFileString", path, () =>
NodeFS.writeFileSync(path, data, { flag: options?.flag ?? "w" }),
),
});
export const layer = Layer.succeed(FileSystem.FileSystem, make);