2020-09-12 08:03:18 -04:00
|
|
|
# File system events
|
2020-05-09 21:09:42 -04:00
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
## Concepts
|
|
|
|
|
|
|
|
- Use [Deno.watchFs](https://doc.deno.land/builtin/stable#Deno.watchFs) to watch
|
|
|
|
for file system events
|
|
|
|
- Results may vary between operating systems
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
|
|
|
To poll for file system events in the current directory:
|
2020-05-09 21:09:42 -04:00
|
|
|
|
|
|
|
```ts
|
2020-09-12 08:03:18 -04:00
|
|
|
/**
|
|
|
|
* watcher.ts
|
|
|
|
*/
|
|
|
|
const watcher = Deno.watchFs(".");
|
2020-05-09 21:09:42 -04:00
|
|
|
for await (const event of watcher) {
|
|
|
|
console.log(">>>> event", event);
|
2020-09-12 08:03:18 -04:00
|
|
|
// Example event: { kind: "create", paths: [ "/home/alice/deno/foo.txt" ] }
|
2020-05-09 21:09:42 -04:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
Run with:
|
|
|
|
|
|
|
|
```shell
|
|
|
|
deno run --allow-read watcher.ts
|
|
|
|
```
|
|
|
|
|
|
|
|
Now try adding, removing and modifying files in the same directory as
|
|
|
|
`watcher.ts`.
|
|
|
|
|
2020-05-09 21:09:42 -04:00
|
|
|
Note that the exact ordering of the events can vary between operating systems.
|
|
|
|
This feature uses different syscalls depending on the platform:
|
|
|
|
|
2020-09-12 08:03:18 -04:00
|
|
|
- Linux: [inotify](https://man7.org/linux/man-pages/man7/inotify.7.html)
|
|
|
|
- macOS:
|
|
|
|
[FSEvents](https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/FSEvents_ProgGuide/Introduction/Introduction.html)
|
|
|
|
- Windows:
|
|
|
|
[ReadDirectoryChangesW](https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-readdirectorychangesw)
|