文件系统事件
概念 Jump to heading
- 使用 Deno.watchFs 来监视文件系统事件。
- 结果可能会因操作系统而异。
示例 Jump to heading
要在当前目录中轮询文件系统事件:
watcher.ts
const watcher = Deno.watchFs(".");
for await (const event of watcher) {
console.log(">>>> event", event);
// 示例事件: { kind: "create", paths: [ "/home/alice/deno/foo.txt" ] }
}
运行命令:
deno run --allow-read watcher.ts
现在尝试在与 watcher.ts
相同的目录中添加、删除和修改文件。
请注意,事件的具体顺序可能会因操作系统而异。此功能根据平台使用不同的系统调用:
- Linux: inotify
- macOS: FSEvents
- Windows: ReadDirectoryChangesW