npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rescript-chokidar

v0.1.2

Published

ReScript bindings for chokidar

Downloads

12

Readme

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"