/ src / BunFile.res
BunFile.res
 1  // SPDX-License-Identifier: AGPL-3.0-or-later
 2  // SPDX-FileCopyrightText: 2025 Jonathan D.A. Jewell
 3  // BunFile - Bun.file() compatible interface
 4  
 5  type t = {path: string}
 6  
 7  let make = (path: string): t => {path: path}
 8  
 9  let text = async (file: t): string => {
10    await Deno.File.readTextFile(file.path)
11  }
12  
13  let json = async (file: t): 'a => {
14    let content = await text(file)
15    JSON.parseExn(content)
16  }
17  
18  let arrayBuffer = async (file: t): Js.TypedArray2.ArrayBuffer.t => {
19    let bytes = await Deno.File.readFile(file.path)
20    Js.TypedArray2.Uint8Array.buffer(bytes)
21  }
22  
23  let size = async (file: t): int => {
24    let stat = await Deno.Stat.stat(file.path)
25    stat.size
26  }
27  
28  let exists = async (file: t): bool => {
29    try {
30      let _ = await Deno.Stat.stat(file.path)
31      true
32    } catch {
33    | _ => false
34    }
35  }
36  
37  let name = (file: t): string => {
38    let parts = String.split(file.path, "/")
39    switch Array.at(parts, -1) {
40    | Some(n) => n
41    | None => file.path
42    }
43  }
44  
45  // Bun.file() equivalent
46  let file = make