summaryrefslogtreecommitdiffhomepage
path: root/packages/core
diff options
context:
space:
mode:
Diffstat (limited to 'packages/core')
-rw-r--r--packages/core/src/filesystem.ts8
-rw-r--r--packages/core/test/filesystem/filesystem.test.ts28
2 files changed, 36 insertions, 0 deletions
diff --git a/packages/core/src/filesystem.ts b/packages/core/src/filesystem.ts
index 44346be8f..8a1cc3a08 100644
--- a/packages/core/src/filesystem.ts
+++ b/packages/core/src/filesystem.ts
@@ -24,6 +24,7 @@ export namespace AppFileSystem {
readonly isDir: (path: string) => Effect.Effect<boolean>
readonly isFile: (path: string) => Effect.Effect<boolean>
readonly existsSafe: (path: string) => Effect.Effect<boolean>
+ readonly readFileStringSafe: (path: string) => Effect.Effect<string | undefined, Error>
readonly readJson: (path: string) => Effect.Effect<unknown, Error>
readonly writeJson: (path: string, data: unknown, mode?: number) => Effect.Effect<void, Error>
readonly ensureDir: (path: string) => Effect.Effect<void, Error>
@@ -47,6 +48,12 @@ export namespace AppFileSystem {
return yield* fs.exists(path).pipe(Effect.orElseSucceed(() => false))
})
+ const readFileStringSafe = Effect.fn("FileSystem.readFileStringSafe")(function* (path: string) {
+ return yield* fs
+ .readFileString(path)
+ .pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed(undefined)))
+ })
+
const isDir = Effect.fn("FileSystem.isDir")(function* (path: string) {
const info = yield* fs.stat(path).pipe(Effect.catch(() => Effect.void))
return info?.type === "Directory"
@@ -163,6 +170,7 @@ export namespace AppFileSystem {
return Service.of({
...fs,
existsSafe,
+ readFileStringSafe,
isDir,
isFile,
readDirectoryEntries,
diff --git a/packages/core/test/filesystem/filesystem.test.ts b/packages/core/test/filesystem/filesystem.test.ts
index b77f4e356..1d9405333 100644
--- a/packages/core/test/filesystem/filesystem.test.ts
+++ b/packages/core/test/filesystem/filesystem.test.ts
@@ -65,6 +65,34 @@ describe("AppFileSystem", () => {
)
})
+ describe("readFileStringSafe", () => {
+ it(
+ "returns file contents when file exists",
+ Effect.gen(function* () {
+ const fs = yield* AppFileSystem.Service
+ const filesys = yield* FileSystem.FileSystem
+ const tmp = yield* filesys.makeTempDirectoryScoped()
+ const file = path.join(tmp, "exists.txt")
+ yield* filesys.writeFileString(file, "hello")
+
+ const result = yield* fs.readFileStringSafe(file)
+ expect(result).toBe("hello")
+ }),
+ )
+
+ it(
+ "returns undefined for missing file (NotFound)",
+ Effect.gen(function* () {
+ const fs = yield* AppFileSystem.Service
+ const filesys = yield* FileSystem.FileSystem
+ const tmp = yield* filesys.makeTempDirectoryScoped()
+
+ const result = yield* fs.readFileStringSafe(path.join(tmp, "does-not-exist.txt"))
+ expect(result).toBeUndefined()
+ }),
+ )
+ })
+
describe("readJson / writeJson", () => {
it(
"round-trips JSON data",