fs.watch(filename[, options][, listener])
filename<string> | <Buffer> | <URL>options<string> | <Object>persistent<boolean> 指示进程是否应在监视文件时继续运行。默认值:true。recursive<boolean> 指示是否应监视所有子目录,还是仅监视当前目录。当指定目录时适用,并且仅在支持的平台上生效(参见 注意事项)。默认值:false。encoding<string> 指定传递给监听器的文件名所使用的字符编码。默认值:'utf8'。signal<AbortSignal> 允许使用 AbortSignal 关闭监听器。
listener<Function> | <undefined> 默认:undefined- 返回:<fs.FSWatcher>
监视 filename 的更改,其中 filename 可以是文件或目录。
🌐 Watch for changes on filename, where filename is either a file or a
directory.
第二个参数是可选的。如果将 options 提供为字符串,它指定 encoding。否则,options 应作为对象传递。
🌐 The second argument is optional. If options is provided as a string, it
specifies the encoding. Otherwise options should be passed as an object.
监听器回调函数接收两个参数 (eventType, filename)。eventType 可以是 'rename' 或 'change',而 filename 是触发该事件的文件名。
🌐 The listener callback gets two arguments (eventType, filename). eventType
is either 'rename' or 'change', and filename is the name of the file
which triggered the event.
在大多数平台上,每当目录中出现或消失一个文件名时,都会触发 'rename'。
🌐 On most platforms, 'rename' is emitted whenever a filename appears or
disappears in the directory.
监听器回调附加到由 <fs.FSWatcher> 触发的 'change' 事件,但这与 eventType 的 'change' 值并不相同。
🌐 The listener callback is attached to the 'change' event fired by
<fs.FSWatcher>, but it is not the same thing as the 'change' value of
eventType.
如果传入 signal,中止相应的 AbortController 将关闭返回的 <fs.FSWatcher>。
🌐 If a signal is passed, aborting the corresponding AbortController will close
the returned <fs.FSWatcher>.