Created
January 23, 2021 19:59
-
-
Save fal-works/8ef5d612f8518e3d137125dd1f87d4e7 to your computer and use it in GitHub Desktop.
Usage example of chokidar.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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