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 | /** |
| 2 | * Deno implementation of Effect's `FileSystem` service. |
| 3 | * |
| 4 | * @stability unstable |
| 5 | * @since 4.0.0 |
| 6 | */ |
| 7 | import { copy as denoCopy, walk } from "@std/fs" |
| 8 | import { relative } from "@std/path" |
| 9 | import * as ByteSize from "effect/ByteSize" |
| 10 | import * as Effect from "effect/Effect" |
| 11 | import * as FileSystem from "effect/FileSystem" |
| 12 | import * as Layer from "effect/Layer" |
| 13 | import * as Option from "effect/Option" |
| 14 | import * as PlatformError from "effect/PlatformError" |
| 15 | import * as Stream from "effect/Stream" |
| 16 | import { handleError } from "./internal/error.ts" |
| 17 | |
| 18 | const tryPromise = <A>( |
| 19 | method: string, |
| 20 | pathOrDescriptor: string | number | undefined, |
| 21 | evaluate: (signal: AbortSignal) => PromiseLike<A> |
| 22 | ) => |
| 23 | Effect.tryPromise({ |
| 24 | try: evaluate, |
| 25 | catch: handleError("FileSystem", method, pathOrDescriptor) |
| 26 | }) |
| 27 | |
| 28 | const close = (file: Deno.FsFile, method: string, pathOrDescriptor: string | number) => |
| 29 | Effect.orDie(Effect.try({ |
| 30 | try: () => file.close(), |
| 31 | catch: handleError("FileSystem", method, pathOrDescriptor) |
| 32 | })) |
| 33 | |
| 34 | const collectAsyncIterable = <A>( |
| 35 | method: string, |
| 36 | pathOrDescriptor: string | number | undefined, |
| 37 | evaluate: () => AsyncIterable<A> |
| 38 | ) => |
| 39 | Effect.tryPromise({ |
| 40 | try: async () => { |
| 41 | const values = new Array<A>() |
| 42 | for await (const value of evaluate()) { |
| 43 | values.push(value) |
| 44 | } |
| 45 | return values |
| 46 | }, |
| 47 | catch: handleError("FileSystem", method, pathOrDescriptor) |
| 48 | }) |
| 49 | |
| 50 | const access: FileSystem.FileSystem["access"] = (path, options) => { |
| 51 | if (!options?.readable && !options?.writable) { |
| 52 | return Effect.asVoid(tryPromise("access", path, () => Deno.stat(path))) |
| 53 | } |
| 54 | return Effect.acquireUseRelease( |
| 55 | tryPromise("access", path, () => |
| 56 | Deno.open(path, { |
| 57 | ...(options.readable ? { read: true } : {}), |
| 58 | ...(options.writable ? { write: true } : {}) |
| 59 | })), |
| 60 | () => Effect.void, |
| 61 | (file) => close(file, "access", path) |
| 62 | ) |
| 63 | } |
| 64 | |
| 65 | const copy: FileSystem.FileSystem["copy"] = (fromPath, toPath, options) => |
| 66 | tryPromise("copy", fromPath, () => |
| 67 | denoCopy(fromPath, toPath, { |
| 68 | overwrite: options?.overwrite ?? false, |
| 69 | preserveTimestamps: options?.preserveTimestamps ?? false |
| 70 | })) |
| 71 | |
| 72 | const copyFile: FileSystem.FileSystem["copyFile"] = (fromPath, toPath) => |
| 73 | tryPromise("copyFile", fromPath, () => Deno.copyFile(fromPath, toPath)) |
| 74 | |
| 75 | const chmod: FileSystem.FileSystem["chmod"] = (path, mode) => tryPromise("chmod", path, () => Deno.chmod(path, mode)) |
| 76 | |
| 77 | const chown: FileSystem.FileSystem["chown"] = (path, uid, gid) => |
| 78 | tryPromise("chown", path, () => Deno.chown(path, uid, gid)) |
| 79 | |
| 80 | const link: FileSystem.FileSystem["link"] = (existingPath, newPath) => |
| 81 | tryPromise("link", existingPath, () => Deno.link(existingPath, newPath)) |
| 82 | |
| 83 | const makeDirectory: FileSystem.FileSystem["makeDirectory"] = (path, options) => |
| 84 | tryPromise("makeDirectory", path, () => |
| 85 | Deno.mkdir(path, { |
| 86 | recursive: options?.recursive ?? false, |
| 87 | ...(options?.mode === undefined ? {} : { mode: options.mode }) |
| 88 | })) |
| 89 | |
| 90 | const makeTempDirectoryFactory = (method: string): FileSystem.FileSystem["makeTempDirectory"] => (options) => |
| 91 | tryPromise(method, options?.directory, () => |
| 92 | Deno.makeTempDir({ |
| 93 | ...(options?.directory === undefined ? {} : { dir: options.directory }), |
| 94 | ...(options?.prefix === undefined ? {} : { prefix: options.prefix }) |
| 95 | })) |
| 96 | |
| 97 | const makeTempDirectory = makeTempDirectoryFactory("makeTempDirectory") |
| 98 | |
| 99 | const removeFactory = (method: string): FileSystem.FileSystem["remove"] => (path, options) => { |
| 100 | const effect = tryPromise(method, path, () => Deno.remove(path, { recursive: options?.recursive ?? false })) |
| 101 | return options?.force |
| 102 | ? Effect.catchTag( |
| 103 | effect, |
| 104 | "PlatformError", |
| 105 | (error) => error.reason._tag === "NotFound" ? Effect.void : Effect.fail(error) |
| 106 | ) |
| 107 | : effect |
| 108 | } |
| 109 | |
| 110 | const remove = removeFactory("remove") |
| 111 | |
| 112 | const makeTempDirectoryScoped: FileSystem.FileSystem["makeTempDirectoryScoped"] = (options) => |
| 113 | Effect.acquireRelease( |
| 114 | makeTempDirectoryFactory("makeTempDirectoryScoped")(options), |
| 115 | (directory) => Effect.orDie(removeFactory("makeTempDirectoryScoped")(directory, { recursive: true, force: true })) |
| 116 | ) |
| 117 | |
| 118 | const openOptions = (flag: FileSystem.OpenFlag = "r", mode?: number): Deno.OpenOptions => { |
| 119 | const modeOption = mode === undefined ? {} : { mode } |
| 120 | switch (flag) { |
| 121 | case "r": |
| 122 | return { read: true, ...modeOption } |
| 123 | case "r+": |
| 124 | return { read: true, write: true, ...modeOption } |
| 125 | case "w": |
| 126 | return { write: true, create: true, truncate: true, ...modeOption } |
| 127 | case "wx": |
| 128 | return { write: true, createNew: true, ...modeOption } |
| 129 | case "w+": |
| 130 | return { read: true, write: true, create: true, truncate: true, ...modeOption } |
| 131 | case "wx+": |
| 132 | return { read: true, write: true, createNew: true, ...modeOption } |
| 133 | case "a": |
| 134 | return { append: true, create: true, ...modeOption } |
| 135 | case "ax": |
| 136 | return { append: true, createNew: true, ...modeOption } |
| 137 | case "a+": |
| 138 | return { read: true, append: true, create: true, ...modeOption } |
| 139 | case "ax+": |
| 140 | return { read: true, append: true, createNew: true, ...modeOption } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // `Deno.DirEntry` only reports the first three flags |
| 145 | const fileType = (info: Deno.DirEntry | Deno.FileInfo): FileSystem.File.Type => { |
| 146 | const flags: Partial<Deno.FileInfo> = info |
| 147 | return info.isFile ? |
| 148 | "File" : |
| 149 | info.isDirectory ? |
| 150 | "Directory" : |
| 151 | info.isSymlink ? |
| 152 | "SymbolicLink" : |
| 153 | flags.isBlockDevice ? |
| 154 | "BlockDevice" : |
| 155 | flags.isCharDevice ? |
| 156 | "CharacterDevice" : |
| 157 | flags.isFifo ? |
| 158 | "FIFO" : |
| 159 | flags.isSocket ? |
| 160 | "Socket" : |
| 161 | "Unknown" |
| 162 | } |
| 163 | |
| 164 | const makeFileInfo = (info: Deno.FileInfo): FileSystem.File.Info => ({ |
| 165 | type: fileType(info), |
| 166 | mtime: Option.fromNullishOr(info.mtime), |
| 167 | atime: Option.fromNullishOr(info.atime), |
| 168 | birthtime: Option.fromNullishOr(info.birthtime), |
| 169 | dev: BigInt(info.dev), |
| 170 | rdev: Option.map(Option.fromNullishOr(info.rdev), BigInt), |
| 171 | ino: Option.map(Option.fromNullishOr(info.ino), BigInt), |
| 172 | mode: info.mode ?? 0, |
| 173 | nlink: Option.map(Option.fromNullishOr(info.nlink), BigInt), |
| 174 | uid: Option.fromNullishOr(info.uid), |
| 175 | gid: Option.fromNullishOr(info.gid), |
| 176 | size: ByteSize.bytes(BigInt(info.size)), |
| 177 | blksize: Option.map(Option.fromNullishOr(info.blksize), (size) => ByteSize.bytes(BigInt(size))), |
| 178 | blocks: Option.map(Option.fromNullishOr(info.blocks), BigInt) |
| 179 | }) |
| 180 | |
| 181 | /** |
| 182 | * Deno lacks positional reads, so `native` serializes seek + I/O. Cancelled |
| 183 | * entries are skipped; started I/O holds the queue until it settles, even |
| 184 | * after interruption. Cursor operations must not run concurrently. |
| 185 | */ |
| 186 | class FileImpl implements FileSystem.File { |
| 187 | readonly [FileSystem.FileTypeId]: typeof FileSystem.FileTypeId = FileSystem.FileTypeId |
| 188 | private readonly file: Deno.FsFile |
| 189 | private readonly append: boolean |
| 190 | private position = BigInt(0) |
| 191 | private nativePosition: bigint | undefined = undefined |
| 192 | private nativeQueue: Promise<unknown> = Promise.resolve() |
| 193 | |
| 194 | constructor( |
| 195 | file: Deno.FsFile, |
| 196 | append: boolean |
| 197 | ) { |
| 198 | this.file = file |
| 199 | this.append = append |
| 200 | } |
| 201 | |
| 202 | get stat() { |
| 203 | return Effect.map( |
| 204 | tryPromise("stat", undefined, () => this.file.stat()), |
| 205 | makeFileInfo |
| 206 | ) |
| 207 | } |
| 208 | |
| 209 | get sync() { |
| 210 | return tryPromise("sync", undefined, () => this.file.sync()) |
| 211 | } |
| 212 | |
| 213 | seek(offset: bigint, from: FileSystem.SeekMode) { |
| 214 | return Effect.suspend(() => { |
| 215 | const position = from === "start" ? offset : this.position + offset |
| 216 | if (position < BigInt(0)) { |
| 217 | return Effect.fail(PlatformError.badArgument({ |
| 218 | module: "FileSystem", |
| 219 | method: "seek", |
| 220 | description: "Cannot seek before the start of the file" |
| 221 | })) |
| 222 | } |
| 223 | this.position = position |
| 224 | return Effect.succeed(position) |
| 225 | }) |
| 226 | } |
| 227 | |
| 228 | private native<A>(signal: AbortSignal, evaluate: () => Promise<A>): Promise<A> { |
| 229 | const result = this.nativeQueue.then(() => signal.aborted ? Promise.reject(signal.reason) : evaluate()) |
| 230 | this.nativeQueue = result.catch(() => undefined) |
| 231 | return result |
| 232 | } |
| 233 | |
| 234 | private readChunk(method: string, buffer: Uint8Array, explicit: bigint | undefined) { |
| 235 | return Effect.suspend(() => { |
| 236 | if (explicit !== undefined && explicit < BigInt(0)) { |
| 237 | return Effect.fail(PlatformError.badArgument({ |
| 238 | module: "FileSystem", |
| 239 | method, |
| 240 | description: "Cannot read before the start of the file" |
| 241 | })) |
| 242 | } |
| 243 | const position = explicit ?? this.position |
| 244 | return Effect.map( |
| 245 | tryPromise( |
| 246 | method, |
| 247 | undefined, |
| 248 | (signal) => |
| 249 | this.native(signal, async () => { |
| 250 | if (this.nativePosition !== position) { |
| 251 | this.file.seekSync(position, Deno.SeekMode.Start) |
| 252 | } |
| 253 | this.nativePosition = undefined |
| 254 | const bytesRead = (await this.file.read(buffer)) ?? 0 |
| 255 | this.nativePosition = position + BigInt(bytesRead) |
| 256 | return bytesRead |
| 257 | }) |
| 258 | ), |
| 259 | (bytesRead) => { |
| 260 | if (explicit === undefined) { |
| 261 | this.position = position + BigInt(bytesRead) |
| 262 | } |
| 263 | return bytesRead |
| 264 | } |
| 265 | ) |
| 266 | }) |
| 267 | } |
| 268 | |
| 269 | read(buffer: Uint8Array, options?: FileSystem.File.ReadOptions) { |
| 270 | return this.readChunk("read", buffer, options?.position) |
| 271 | } |
| 272 | |
| 273 | readAlloc(size: number, options?: FileSystem.File.ReadOptions) { |
| 274 | return Effect.suspend(() => { |
| 275 | try { |
| 276 | if (!Number.isInteger(size) || size < 0) { |
| 277 | throw new RangeError("size must be a non-negative integer") |
| 278 | } |
| 279 | const buffer = new Uint8Array(size) |
| 280 | return Effect.map(this.readChunk("readAlloc", buffer, options?.position), (bytesRead) => { |
| 281 | if (bytesRead === 0) { |
| 282 | return Option.none() |
| 283 | } |
| 284 | return Option.some(bytesRead === size ? buffer : buffer.subarray(0, bytesRead)) |
| 285 | }) |
| 286 | } catch (cause) { |
| 287 | return Effect.fail(PlatformError.badArgument({ |
| 288 | module: "FileSystem", |
| 289 | method: "readAlloc", |
| 290 | description: `Could not allocate a buffer of size ${size}`, |
| 291 | cause |
| 292 | })) |
| 293 | } |
| 294 | }) |
| 295 | } |
| 296 | |
| 297 | truncate(length?: number) { |
| 298 | const size = BigInt(length ?? 0) |
| 299 | return Effect.map( |
| 300 | tryPromise("truncate", undefined, () => this.file.truncate(Number(size))), |
| 301 | () => { |
| 302 | if (!this.append && this.position > size) { |
| 303 | this.position = size |
| 304 | } |
| 305 | } |
| 306 | ) |
| 307 | } |
| 308 | |
| 309 | private writeChunk(method: string, buffer: Uint8Array) { |
| 310 | return Effect.suspend(() => { |
| 311 | const position = this.position |
| 312 | return Effect.map( |
| 313 | tryPromise( |
| 314 | method, |
| 315 | undefined, |
| 316 | (signal) => |
| 317 | this.native(signal, async () => { |
| 318 | if (!this.append && this.nativePosition !== position) { |
| 319 | this.file.seekSync(position, Deno.SeekMode.Start) |
| 320 | } |
| 321 | this.nativePosition = undefined |
| 322 | const bytesWritten = await this.file.write(buffer) |
| 323 | if (!this.append) { |
| 324 | this.nativePosition = position + BigInt(bytesWritten) |
| 325 | } |
| 326 | return bytesWritten |
| 327 | }) |
| 328 | ), |
| 329 | (bytesWritten) => { |
| 330 | if (!this.append) { |
| 331 | this.position = position + BigInt(bytesWritten) |
| 332 | } |
| 333 | return bytesWritten |
| 334 | } |
| 335 | ) |
| 336 | }) |
| 337 | } |
| 338 | |
| 339 | write(buffer: Uint8Array) { |
| 340 | return this.writeChunk("write", buffer) |
| 341 | } |
| 342 | |
| 343 | private writeAllChunk(buffer: Uint8Array): Effect.Effect<void, PlatformError.PlatformError> { |
| 344 | return Effect.flatMap(this.writeChunk("writeAll", buffer), (bytesWritten) => { |
| 345 | if (bytesWritten === 0) { |
| 346 | return Effect.fail(PlatformError.systemError({ |
| 347 | module: "FileSystem", |
| 348 | method: "writeAll", |
| 349 | _tag: "WriteZero", |
| 350 | description: "write returned 0 bytes written" |
| 351 | })) |
| 352 | } |
| 353 | return bytesWritten < buffer.length |
| 354 | ? this.writeAllChunk(buffer.subarray(bytesWritten)) |
| 355 | : Effect.void |
| 356 | }) |
| 357 | } |
| 358 | |
| 359 | writeAll(buffer: Uint8Array) { |
| 360 | return buffer.length === 0 ? Effect.void : this.writeAllChunk(buffer) |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | const open: FileSystem.FileSystem["open"] = (path, options) => { |
| 365 | if (options?.noFollow === true) { |
| 366 | return Effect.fail(PlatformError.badArgument({ |
| 367 | module: "FileSystem", |
| 368 | method: "open", |
| 369 | description: "noFollow is not supported by Deno" |
| 370 | })) |
| 371 | } |
| 372 | const append = options?.flag?.startsWith("a") ?? false |
| 373 | return Effect.map( |
| 374 | Effect.acquireRelease( |
| 375 | tryPromise("open", path, () => Deno.open(path, openOptions(options?.flag, options?.mode))), |
| 376 | (file) => close(file, "open", path) |
| 377 | ), |
| 378 | (file) => new FileImpl(file, append) |
| 379 | ) |
| 380 | } |
| 381 | |
| 382 | const makeTempFileFactory = (method: string): FileSystem.FileSystem["makeTempFile"] => (options) => |
| 383 | tryPromise(method, options?.directory, () => |
| 384 | Deno.makeTempFile({ |
| 385 | ...(options?.directory === undefined ? {} : { dir: options.directory }), |
| 386 | ...(options?.prefix === undefined ? {} : { prefix: options.prefix }), |
| 387 | ...(options?.suffix === undefined ? {} : { suffix: options.suffix }) |
| 388 | })) |
| 389 | |
| 390 | const makeTempFile = makeTempFileFactory("makeTempFile") |
| 391 | |
| 392 | const makeTempFileScoped: FileSystem.FileSystem["makeTempFileScoped"] = (options) => |
| 393 | Effect.acquireRelease( |
| 394 | makeTempFileFactory("makeTempFileScoped")(options), |
| 395 | (file) => Effect.orDie(removeFactory("makeTempFileScoped")(file, { force: true })) |
| 396 | ) |
| 397 | |
| 398 | const readDirectory: FileSystem.FileSystem["readDirectory"] = (path, options) => { |
| 399 | if (options?.recursive) { |
| 400 | return Effect.map( |
| 401 | collectAsyncIterable("readDirectory", path, () => walk(path)), |
| 402 | (entries) => entries.map((entry) => relative(path, entry.path)).filter(Boolean) |
| 403 | ) |
| 404 | } |
| 405 | return Effect.map( |
| 406 | collectAsyncIterable("readDirectory", path, () => Deno.readDir(path)), |
| 407 | (entries) => entries.map((entry) => entry.name) |
| 408 | ) |
| 409 | } |
| 410 | |
| 411 | const readDirectoryEntries = (path: string) => |
| 412 | Effect.map( |
| 413 | collectAsyncIterable("readDirectoryEntries", path, () => Deno.readDir(path)), |
| 414 | (entries) => entries.map((entry): FileSystem.DirectoryEntry => ({ name: entry.name, type: fileType(entry) })) |
| 415 | ) |
| 416 | |
| 417 | const readFile: FileSystem.FileSystem["readFile"] = (path) => |
| 418 | tryPromise("readFile", path, (signal) => Deno.readFile(path, { signal })) |
| 419 | |
| 420 | const readLink: FileSystem.FileSystem["readLink"] = (path) => tryPromise("readLink", path, () => Deno.readLink(path)) |
| 421 | |
| 422 | const realPath: FileSystem.FileSystem["realPath"] = (path) => tryPromise("realPath", path, () => Deno.realPath(path)) |
| 423 | |
| 424 | const rename: FileSystem.FileSystem["rename"] = (oldPath, newPath) => |
| 425 | tryPromise("rename", oldPath, () => Deno.rename(oldPath, newPath)) |
| 426 | |
| 427 | const lstat: FileSystem.FileSystem["lstat"] = (path) => |
| 428 | Effect.map( |
| 429 | tryPromise("lstat", path, () => Deno.lstat(path)), |
| 430 | makeFileInfo |
| 431 | ) |
| 432 | |
| 433 | const stat: FileSystem.FileSystem["stat"] = (path) => |
| 434 | Effect.map( |
| 435 | tryPromise("stat", path, () => Deno.stat(path)), |
| 436 | makeFileInfo |
| 437 | ) |
| 438 | |
| 439 | const symlink: FileSystem.FileSystem["symlink"] = (target, path) => |
| 440 | tryPromise("symlink", target, () => Deno.symlink(target, path)) |
| 441 | |
| 442 | const truncate: FileSystem.FileSystem["truncate"] = (path, length) => |
| 443 | tryPromise("truncate", path, () => Deno.truncate(path, length)) |
| 444 | |
| 445 | const utimes: FileSystem.FileSystem["utimes"] = (path, atime, mtime) => |
| 446 | tryPromise("utimes", path, () => Deno.utime(path, atime, mtime)) |
| 447 | |
| 448 | const watchNative = ( |
| 449 | path: string, |
| 450 | options?: FileSystem.WatchOptions |
| 451 | ): Stream.Stream<FileSystem.WatchEvent, PlatformError.PlatformError> => |
| 452 | Stream.unwrap( |
| 453 | Effect.map( |
| 454 | Effect.try({ |
| 455 | try: () => Deno.watchFs(path, { recursive: options?.recursive ?? false }), |
| 456 | catch: handleError("FileSystem", "watch", path) |
| 457 | }), |
| 458 | (watcher) => { |
| 459 | options?.onReady?.() |
| 460 | return Stream.fromAsyncIterable(watcher, handleError("FileSystem", "watch", path)).pipe( |
| 461 | Stream.flatMap((event): Stream.Stream<FileSystem.WatchEvent, PlatformError.PlatformError> => { |
| 462 | switch (event.kind) { |
| 463 | case "create": |
| 464 | return Stream.map(Stream.fromIterable(event.paths), (path) => ({ _tag: "Create" as const, path })) |
| 465 | case "modify": |
| 466 | return Stream.map(Stream.fromIterable(event.paths), (path) => ({ _tag: "Update" as const, path })) |
| 467 | case "remove": |
| 468 | return Stream.map(Stream.fromIterable(event.paths), (path) => ({ _tag: "Remove" as const, path })) |
| 469 | case "rename": |
| 470 | return Stream.mapEffect(Stream.fromIterable(event.paths), (path) => |
| 471 | Effect.match(stat(path), { |
| 472 | onFailure: () => ({ _tag: "Remove" as const, path }), |
| 473 | onSuccess: () => ({ _tag: "Create" as const, path }) |
| 474 | })) |
| 475 | default: |
| 476 | return Stream.empty |
| 477 | } |
| 478 | }) |
| 479 | ) |
| 480 | } |
| 481 | ) |
| 482 | ) |
| 483 | |
| 484 | const watch = ( |
| 485 | backend: Option.Option<FileSystem.WatchBackend["Service"]>, |
| 486 | path: string, |
| 487 | options?: FileSystem.WatchOptions |
| 488 | ) => |
| 489 | Effect.all([stat(path), Effect.serviceOption(FileSystem.WatchBackend)]).pipe( |
| 490 | Effect.map(([info, callerBackend]) => |
| 491 | Option.orElse(callerBackend, () => backend).pipe( |
| 492 | Option.flatMap((backend) => backend.register(path, info, options)), |
| 493 | Option.getOrElse(() => watchNative(path, options)) |
| 494 | ) |
| 495 | ), |
| 496 | Stream.unwrap |
| 497 | ) |
| 498 | |
| 499 | const writeFile: FileSystem.FileSystem["writeFile"] = (path, data, options) => { |
| 500 | const flag = options?.flag ?? "w" |
| 501 | if (options?.mode === undefined && (flag === "w" || flag === "wx" || flag === "a" || flag === "ax")) { |
| 502 | return tryPromise("writeFile", path, (signal) => |
| 503 | Deno.writeFile(path, data, { |
| 504 | append: flag.startsWith("a"), |
| 505 | createNew: flag.includes("x"), |
| 506 | signal |
| 507 | })) |
| 508 | } |
| 509 | return Effect.acquireUseRelease( |
| 510 | tryPromise("writeFile", path, () => Deno.open(path, openOptions(flag, options?.mode))), |
| 511 | (file) => |
| 512 | new FileImpl(file, flag.startsWith("a")).writeAll(data).pipe( |
| 513 | Effect.mapError((error) => |
| 514 | error.reason._tag !== "BadArgument" |
| 515 | ? PlatformError.systemError({ ...error.reason, method: "writeFile", pathOrDescriptor: path }) |
| 516 | : error |
| 517 | ) |
| 518 | ), |
| 519 | (file) => close(file, "writeFile", path) |
| 520 | ) |
| 521 | } |
| 522 | |
| 523 | const makeFileSystem = Effect.map(Effect.serviceOption(FileSystem.WatchBackend), (backend) => |
| 524 | FileSystem.make({ |
| 525 | access, |
| 526 | chmod, |
| 527 | chown, |
| 528 | copy, |
| 529 | copyFile, |
| 530 | link, |
| 531 | lstat, |
| 532 | makeDirectory, |
| 533 | makeTempDirectory, |
| 534 | makeTempDirectoryScoped, |
| 535 | makeTempFile, |
| 536 | makeTempFileScoped, |
| 537 | open, |
| 538 | readDirectory, |
| 539 | readDirectoryEntries, |
| 540 | readFile, |
| 541 | readLink, |
| 542 | realPath, |
| 543 | remove, |
| 544 | rename, |
| 545 | stat, |
| 546 | symlink, |
| 547 | truncate, |
| 548 | utimes, |
| 549 | watch(path, options) { |
| 550 | return watch(backend, path, options) |
| 551 | }, |
| 552 | writeFile |
| 553 | })) |
| 554 | |
| 555 | /** |
| 556 | * Provides the `FileSystem` service backed by Deno filesystem APIs. |
| 557 | * |
| 558 | * @stability unstable |
| 559 | * @category layers |
| 560 | * @since 4.0.0 |
| 561 | */ |
| 562 | export const layer: Layer.Layer<FileSystem.FileSystem> = Layer.effect(FileSystem.FileSystem)(makeFileSystem) |