{"id":17223407,"url":"https://github.com/conduitry/cheap-watch","last_synced_at":"2025-08-21T10:31:01.374Z","repository":{"id":47238569,"uuid":"127806390","full_name":"Conduitry/cheap-watch","owner":"Conduitry","description":"If it works, why use something else? // Mirror of https://git.chor.date/Conduitry/cheap-watch","archived":false,"fork":false,"pushed_at":"2021-09-06T22:15:05.000Z","size":57,"stargazers_count":66,"open_issues_count":4,"forks_count":4,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-04-26T17:23:02.042Z","etag":null,"topics":["async","asynchronous","file","file-watcher","watch","watcher"],"latest_commit_sha":null,"homepage":"https://conduitry.dev/cheap-watch","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Conduitry.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-04-02T20:05:41.000Z","updated_at":"2024-04-26T17:23:02.043Z","dependencies_parsed_at":"2022-09-26T17:01:09.824Z","dependency_job_id":null,"html_url":"https://github.com/Conduitry/cheap-watch","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Conduitry%2Fcheap-watch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Conduitry%2Fcheap-watch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Conduitry%2Fcheap-watch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Conduitry%2Fcheap-watch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Conduitry","download_url":"https://codeload.github.com/Conduitry/cheap-watch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230507051,"owners_count":18236944,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","asynchronous","file","file-watcher","watch","watcher"],"created_at":"2024-10-15T04:08:15.779Z","updated_at":"2024-12-19T22:07:54.558Z","avatar_url":"https://github.com/Conduitry.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Cheap Watch: If it works, why use something else?\n\n[![npm version](https://img.shields.io/npm/v/cheap-watch.svg?style=flat-square)](https://www.npmjs.com/package/cheap-watch) [![Build Status](https://travis-ci.org/Conduitry/cheap-watch.svg?branch=master)](https://travis-ci.org/Conduitry/cheap-watch)\n\n**Cheap Watch** is a small, simple, dependency-free, cross-platform file system watcher for Node.js 8+.\n\n## Constructor\n\n### `new CheapWatch({ dir, filter, watch = true, debounce = 10 })`\n\n- `dir` - The directory whose contents to watch. It's recommended, though not required, for this to be an absolute path, say one returned by `path.resolve`.\n- `filter({ path, stats })` - _(optional)_ A function to decide whether a given file or directory should be watched. It's passed an object containing the file or directory's relative `path` and its `stats`. It should return `true` or `false` (or a `Promise` resolving to one of those). Returning `false` for a directory means that none of its contents will be watched.\n- `watch` - _(optional)_ Whether to actually watch the directory for changes. Defaults to `true`. If `false`, you can retrieve all of the files and directories within a given directory along with their initial `Stats` but changes will not be monitored.\n- `debounce` - _(optional)_ Length of timeout in milliseconds to use to debounce incoming events from `fs.watch`. Defaults to 10. Multiple events are often emitted for a single change, and events can also be emitted before `fs.stat` reports the changes. So we will wait until `debounce` milliseconds have passed since the last `fs.watch` event for a file or directory before handling it. The default of 10ms Works On My Machine.\n\n## Methods\n\n### `init()`\n\nInitialize the watcher, traverse the directory to find the initial files and directories, and set up watchers to look for changes.\n\nThis returns a `Promise` that resolves once the initial contents of the directory have been traversed and all of the watchers have been set up.\n\n### `close()`\n\nClose all `FSWatcher` instances, and stop watching for file changes.\n\n## Properties\n\n### `paths`\n\nA `Map` of the watched files and directories. Each key is a relative path from the `CheapWatch`'s `dir`, and each value is a `Stats` object for the file or directory. Paths are always separated by forward slashes, regardless of platform. This `Map` is kept up to date as files are changed on disk.\n\nYou can use `stats.isFile()` and `stats.isDirectory()` to determine whether something is a file or a directory.\n\n## Events\n\nA `CheapWatch` is an `EventEmitter`, and emits two events to report a new, updated, or deleted file or directory.\n\n### `+` `{ path, stats, isNew }`\n\nA `+` event is emitted whenever a watched file or directory is created or updated. It's emitted with an object containing a `path` string, a `stats` object, and an `isNew` boolean which will be `true` for newly created files and directories and `false` for updated ones.\n\n### `-` `{ path, stats }`\n\nA `-` event is emitted whenever a watched file or directory is deleted. It's emitted with an object containing a `path` string and a `stats` object. `stats` will be the most recent `Stats` collected for the file or directory before it was deleted.\n\n## Usage\n\n```javascript\nimport CheapWatch from 'cheap-watch';\n\nconst watch = new CheapWatch({ dir, /* ... */ });\n\nawait watch.init();\n\nfor (const [path, stats] of watch.paths) {\n\t/* ... */\n}\n\nwatch.on('+', ({ path, stats, isNew }) =\u003e { /* ... */ });\nwatch.on('-', ({ path, stats }) =\u003e { /* ... */ });\n```\n\n## License\n\n[MIT](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconduitry%2Fcheap-watch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fconduitry%2Fcheap-watch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fconduitry%2Fcheap-watch/lists"}