fs.watchでファイルの変更監視できる。
import * as fs from "fs";
fs.watch("./", {persistent:true, recursive:false}, (eventType, filename)=>{
console.log(`${eventType}:${filename}`);
});
第1引数に監視するファイルまたはディレクトリ名を指定する。
第2引数にオプション。
- persistent ファイルの監視を続けるかどうかを指定:trueで続ける。falseだと1度変更されたら終了。
- recursive サブディレクトリも監視するかどうか。サブディレクトリも監視できるのは、macOSとWindowsのみ対応。
第3引数は、eventTypeとfilenameが渡される関数を指定する。eventTypeは"rename"か"change"でfilenameはイベントのきっかけになったファイル名。
コメント