Skip to content

Commit

Permalink
Update bindings (#1698)
Browse files Browse the repository at this point in the history
* Use `node:fs/promises` instead

Signed-off-by: Sora Morimoto <[email protected]>

* No longer rely on `joo_global_object`

Signed-off-by: Sora Morimoto <[email protected]>

---------

Signed-off-by: Sora Morimoto <[email protected]>
  • Loading branch information
smorimoto authored Jan 1, 2025
1 parent c3f7bca commit 539244d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 26 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
type t = R_OK

include [%js: val r_ok : Ojs.t [@@js.global "constants.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
22 changes: 6 additions & 16 deletions src-bindings/node/node_stub.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
const fs = require("node:fs");
const { promisify } = require("node:util");

joo_global_object.fs = {
readDir: promisify(fs.readdir),
readFile: promisify(fs.readFile),
exists: promisify(fs.exists),
};

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

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

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

joo_global_object.net = require("node:net");
globalThis.child_process = require("node:child_process");
globalThis.constants = require("node:constants");
globalThis.fs = require("node:fs/promises");
globalThis.net = require("node:net");
globalThis.os = require("node:os");
globalThis.path = require("node:path");
4 changes: 2 additions & 2 deletions src-bindings/polka/polka_stub.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
joo_global_object.polka = require("polka");
joo_global_object.sirv = require("sirv");
globalThis.polka = require("polka");
globalThis.sirv = require("sirv");
2 changes: 1 addition & 1 deletion src-bindings/vscode/vscode_stub.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
joo_global_object.vscode = require("vscode");
globalThis.vscode = require("vscode");
Original file line number Diff line number Diff line change
@@ -1 +1 @@
joo_global_object.vscode_languageclient = require("vscode-languageclient");
globalThis.vscode_languageclient = require("vscode-languageclient");

0 comments on commit 539244d

Please sign in to comment.