Skip to content

Commit f596287

Browse files
committed
fix: deno writeFile honors open flags
1 parent 137f777 commit f596287

3 files changed

Lines changed: 20 additions & 11 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@effect/platform-deno": patch
3+
---
4+
5+
`DenoFileSystem.writeFile` honors open flags and normalizes already-existing-path errors.

packages/platform-deno/src/DenoFileSystem.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,20 @@ const watch = (
439439
Stream.unwrap
440440
)
441441

442-
const writeFile: FileSystem.FileSystem["writeFile"] = (path, data, options) => {
443-
const flag = options?.flag
444-
return tryPromise("writeFile", path, (signal) =>
445-
Deno.writeFile(path, data, {
446-
append: flag?.startsWith("a") ?? false,
447-
create: flag !== "r" && flag !== "r+",
448-
createNew: flag?.includes("x") ?? false,
449-
...(options?.mode === undefined ? {} : { mode: options.mode }),
450-
signal
451-
}))
452-
}
442+
const writeFile: FileSystem.FileSystem["writeFile"] = (path, data, options) =>
443+
open(path, { ...options, flag: options?.flag ?? "w" }).pipe(
444+
Effect.andThen((file) => data.length === 0 ? Effect.void : file.writeAll(data)),
445+
Effect.scoped,
446+
Effect.mapError((error) =>
447+
error.reason._tag === "BadArgument"
448+
? error
449+
: PlatformError.systemError({
450+
...error.reason,
451+
method: "writeFile",
452+
pathOrDescriptor: path
453+
})
454+
)
455+
)
453456

454457
const makeFileSystem = Effect.map(Effect.serviceOption(FileSystem.WatchBackend), (backend) =>
455458
FileSystem.make({

packages/platform-deno/src/internal/error.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const handleError = (
2020
tag = "PermissionDenied"
2121
break
2222
case "BadResource":
23+
case "AlreadyExists":
2324
case "InvalidData":
2425
case "TimedOut":
2526
case "UnexpectedEof":

0 commit comments

Comments
 (0)