File: onchange.ml

package info (click to toggle)
ocaml-luv 0.5.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,504 kB
  • sloc: ml: 11,130; makefile: 6,223; sh: 4,592; ansic: 1,517; python: 38
file content (30 lines) | stat: -rw-r--r-- 938 bytes parent folder | download | duplicates (2)
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
let () =
  match Array.to_list Sys.argv with
  | [] | [_] | [_; _] ->
    Printf.eprintf "Usage: onchange <command> <file1> [file2]..."

  | _::command::files ->
    files |> List.iter begin fun target ->
      let watcher = Luv.FS_event.init () |> Result.get_ok in

      Luv.FS_event.start ~recursive:true watcher target begin function
        | Error e ->
          Printf.eprintf
            "Error watching %s: %s\n" target (Luv.Error.strerror e);
          ignore (Luv.FS_event.stop watcher);
          Luv.Handle.close watcher ignore

        | Ok (file, events) ->
          if List.mem `RENAME events then
            prerr_string "renamed ";
          if List.mem `CHANGE events then
            prerr_string "changed ";
          prerr_endline file;

          let exit_status = Sys.command command in
          if exit_status <> 0 then
            Stdlib.exit exit_status
      end
    end;

  ignore (Luv.Loop.run () : bool)