forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxwatcher.ml
51 lines (42 loc) · 1.68 KB
/
xwatcher.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[@@@bs.config {no_export}]
open Xwatcher_util
(** start customized for bsb-watcher *)
let sourceDirs = Node.Path.join [|"lib"; "bs"; ".sourcedirs.json"|]
let lock = Xwatcher_util.makeLock ()
let events = Xwatcher_util.makeEventObj ()
let watchers : watcher array = [||]
let source_dirname =
Js.Option.getExn [%external __dirname]
let bsb_exe = "bsb.exe"
let bsb = Node.Path.join [|source_dirname; bsb_exe|]
let bsconfig = "bsconfig.json"
let rec onChange = fun eventType fileName ->
Js.log {j|Event $eventType $fileName|j} ;
events##push (eventType, fileName);
build bsb events lock (fun [@bs] () -> idle () )
and idle () =
let watchFiles = getWatchFiles sourceDirs in
watchers
|> Js.Vector.filterInPlace (fun [@bs] {dir ; watcher} ->
if dir = bsconfig || Js.Vector.memByRef dir watchFiles then true
else
begin
Js.log {j| $dir is no longer watched|j} ;
Node.Fs.Watch.close watcher ;
false
end
) ;
watchFiles |> Js.Array.forEach (fun dir ->
if not (Js.Array.some (fun {dir = watcher_dir} -> watcher_dir = dir) watchers) then
begin
Js.log {j|watching dir $dir now |j};
Js.Vector.pushBack (makeWatcher dir (fun [@bs] x y -> onChange x y )) watchers
end
)
let () =
Node.Process.putEnvVar "BS_VSCODE" "1";
Js.Vector.pushBack (makeWatcher bsconfig (fun [@bs] x y -> onChange x y)) watchers;
build bsb events lock (fun [@bs] () -> idle () )
(* local variables: *)
(* compile-command: "bscc -bs-package-output es6:jscomp/xwatcher -c xwatcher_util.mli xwatcher_util.ml xwatcher.ml && rollup --silent -f cjs xwatcher.js -o ../bin/bsb_watcher.future.js " *)
(* end: *)