Skip to content

Instantly share code, notes, and snippets.

@fal-works
Created January 23, 2021 19:59
Show Gist options
  • Save fal-works/8ef5d612f8518e3d137125dd1f87d4e7 to your computer and use it in GitHub Desktop.
Save fal-works/8ef5d612f8518e3d137125dd1f87d4e7 to your computer and use it in GitHub Desktop.
Usage example of chokidar.
import * as chokidar from "chokidar";
/**
* Creates a watcher.
*
* @param {object} options
* @param {string | readonly string[]} options.paths Paths or Glob patterns to watch.
* @param {(path: string) => void} options.onChange
* @param {() => void} [options.onExit]
*/
const watch = (options) => {
const watcher = chokidar.watch(options.paths);
const { onChange, onExit } = options;
watcher.on("ready", () => {
console.log("Start watching...");
watcher.on("change", (path) => onChange(path));
process.on("SIGINT", () => process.exit(0));
process.on("exit", () => console.log("Stop watching."));
if (onExit) process.on("exit", onExit);
});
return watcher;
};
export { watch };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment