🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

rescript-chokidar

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rescript-chokidar

ReScript bindings for chokidar

latest
Source
npmnpm
Version
0.1.2
Version published
Maintainers
1
Created
Source

rescript-chokidar

ReScript bindings for chokidar

Installation

npm i rescript-chokidar

In your bsconfig.json add it to bs-dependencies

{
  ...,
  "bs-dependencies": [..., "rescript-chokidar"],
}

Examples

// One-liner for current directory
Chokidar.watch(".")->Chokidar.on(#all((. event, path, _) => Js.log2(event, path)))->ignore
// Example of a more typical implementation structure

// Initialize watcher.
let watcher = Chokidar.watch(
  ~options=Chokidar.options(
    ~ignored=[Anymatch.regex(%re("/(^|[\/\\])\../"))],
    ~persistent=true,
    (),
  ),
  "file, dir, glob, or array",
)

// Add event listeners.
watcher
->Chokidar.on(#add((. path, _) => Js.log(`File ${path} has been added`)))
->Chokidar.on(#change((. path, _) => Js.log(`File ${path} has been changed`)))
->Chokidar.on(#unlink((. path) => Js.log(`File ${path} has been removed`)))
->ignore

// More possible events.
watcher
->Chokidar.on(#addDir((. path, _) => Js.log(`Directory ${path} has been added`)))
->Chokidar.on(#unlinkDir((. path) => Js.log(`Directory ${path} has been removed`)))
->Chokidar.on(#error((. error) => Js.log(j`Watcher error: $error`)))
->Chokidar.on(#ready(() => Js.log("Initial scan complete. Ready for changes")))
->Chokidar.on(
  #raw(
    (event, path, details) => {
      // internal
      Js.log4("Raw event info:", event, path, details)
    },
  ),
)
->ignore

// 'add', 'addDir' and 'change' events also receive stat() results as second
// argument when available: https://nodejs.org/api/fs.html#fs_class_fs_stats
@get external size: Chokidar.stats => int = "size"
watcher
->Chokidar.on(
  #change(
    (. path, stats) =>
      switch stats {
      | None => ()
      | Some(val) => Js.log(`File ${path} changed size to ${val->size->Js.Int.toString}`)
      },
  ),
)
->ignore

// Watch new files.
watcher->Chokidar.add("new-file")->ignore
watcher->Chokidar.addMany(["new-file-2", "new-file-3", "**/other-file*"])->ignore

// Get list of actual paths being watched on the filesystem
let watchedPaths = watcher->Chokidar.getWatched

// Un-watch some files.
watcher->Chokidar.unwatch("new-file*")->ignore

// Stop watching.
// The method is async!
watcher->Chokidar.close->Js.Promise.then_(() => {
  Js.log("closed")
  Js.Promise.resolve()
}, _)->ignore

// Full list of options.
// Do not use this example!
Chokidar.watch(
  ~options=Chokidar.options(
    ~persistent=true,
    ~ignored=[Anymatch.glob("*.txt")],
    ~ignoreInitial=false,
    ~followSymlinks=true,
    ~cwd=".",
    ~disableGlobbing=false,
    ~usePolling=false,
    ~interval=100,
    ~binaryInterval=300,
    ~alwaysStat=false,
    ~depth=99,
    ~awaitWriteFinish=Chokidar.awaitWriteFinishCustom(
      ~stabilityThreshold=2000,
      ~pollInterval=100,
      (),
    ),
    ~ignorePermissionErrors=false,

    // or a custom 'atomicity delay' (Chokidar.atomicCustom), in milliseconds (default 100)
    ~atomic=Chokidar.atomicOn,
    (),
  ),
  "file",
)->ignore

Caveats

  • A watcher extends EventEmmiter so it has more methods like removeAllListeners() etc. I didn't define them. If you need some of these methods, open an issue or a PR.

  • Some listeners take an instance of fs.Stats as an argument. Unfortunately, there're no official bindings that would define it. Maybe they'll appear at some point, but in meantime it's defined as an abstract type Chokidar.stats. You can define your own accesors, or use a library like rescript-nodejs with a converter defined as external convertStats: Chokidar.stats => NodeJs.Fs.Stats.t = "%identity"

Keywords

rescript

FAQs

Package last updated on 16 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts