Watch a file for changes
npm install watch-file-safe> Watch a file for changes.
Provides a simplified wrapper around chokidar for watching a file.
``sh`
yarn add watch-file-safe
`sh`
npm install watch-file-safe
ts
import watchFile from "watch-file-safe";const watcher = watchFile("/path/to/file.txt");
watcher.onReady(() => {
console.log(
Ready);
}).onAdd((path: string) => {
console.log(Added ${path});
}).onChange((path: string) => {
console.log(Changed ${path});
}).onRemove((path: string) => {
console.log(Removed ${path});
});// To stop watching:
watcher.stop();
`$3
`ts
import watchFile, { Watcher, EventCallback } from "watch-file-safe";function watchFile(path: string): Watcher;
type EventCallback = (path: string) => void;
type Watcher = {
onAdd: (cb: EventCallback) => Watcher;
onRemove: (cb: EventCallback) => Watcher;
onChange: (cb: EventCallback) => Watcher;
onReady: (cb: () => void) => Watcher;
stop: () => Promise;
}
``- chokidar: A neat wrapper around node.js fs.watch / fs.watchFile / fsevents.
- @bconnorwhite/bob: Bob is a toolkit for TypeScript projects
- fs-safe: A simple fs wrapper that doesn't throw
- watch-dir-safe: Watch a directory for changes
- read-file-safe: Read files without try catch
- write-file-safe: Write files, and parent directories if necessary
- remove-file-safe: Remove a file without try catch