Skip to content

Commit

Permalink
Use `node:fs/promises instead
Browse files Browse the repository at this point in the history
Signed-off-by: Sora Morimoto <sora@morimoto.io>
  • Loading branch information
smorimoto committed Dec 26, 2024
1 parent 45bdb8f commit 50560f1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
36 changes: 30 additions & 6 deletions src-bindings/node/node.ml
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,47 @@ module Path = struct
;;
end

module Constants = struct
include [%js: val r_ok : Ojs.t [@@js.global "constants.R_OK"]]

type t = R_OK

let t_to_js = function
| R_OK -> r_ok
;;
end

module Fs = struct
module ReadFileOptions = struct
include Interface.Make ()
include [%js: val create : encoding:string -> t [@@js.builder]]
end

include
[%js:
val readDir : string -> string list Promise.t [@@js.global "fs.readDir"]
val access : string -> mode:Constants.t -> unit Promise.t [@@js.global "fs.access"]
val readdir : string -> string list Promise.t [@@js.global "fs.readdir"]

val readFile : string -> encoding:string -> string Promise.t
[@@js.global "fs.readFile"]
val readFile : string -> options:ReadFileOptions.t -> string Promise.t
[@@js.global "fs.readFile"]]

val exists : string -> bool Promise.t [@@js.global "fs.exists"]]
let exists path =
access path ~mode:Constants.R_OK
|> Promise.then_
~fulfilled:(fun _ -> Promise.return true)
~rejected:(fun _ -> Promise.return false)
;;

let readDir path =
readDir path
readdir path
|> Promise.then_ ~fulfilled:Promise.Result.return ~rejected:(fun error ->
Promise.return (Error (JsError.message error)))
;;

let readFile = readFile ~encoding:"utf8"
let readFile =
let options = ReadFileOptions.create ~encoding:"utf8" in
readFile ~options
;;
end

module Net = struct
Expand Down
6 changes: 6 additions & 0 deletions src-bindings/node/node.mli
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ module Os : sig
val homedir : unit -> string
end

module Constants : sig
type t = R_OK

val t_to_js : t -> Ojs.t
end

module Fs : sig
val readDir : string -> (string list, string) result Promise.t
val readFile : string -> string Promise.t
Expand Down
9 changes: 2 additions & 7 deletions src-bindings/node/node_stub.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const fs = require("node:fs");
const { promisify } = require("node:util");
joo_global_object.constants = require("node:constants");

joo_global_object.fs = {
readDir: promisify(fs.readdir),
readFile: promisify(fs.readFile),
exists: promisify(fs.exists),
};
joo_global_object.fs = require("node:fs/promises");

joo_global_object.child_process = require("node:child_process");

Expand Down

0 comments on commit 50560f1

Please sign in to comment.