LoginSignup
7

More than 5 years have passed since last update.

Windowsでのwatchによるファイル更新監視

Last updated at Posted at 2012-12-26

Windowsでwatchによるファイル更新監視がうまく行く時もあるけど行かない時もあるので、fs.watchFileを試してみた。

Node v0.9.3 + Win XP では動作を確認できました。

var fs = require('fs');
if (process.platform === 'win32') {
  // Windows用
  fs.watchFile(file, function (curr, prev) {/*   */ });
} else {
  fs.watch(file, function (eventName, filename) {/*   */ });
}

まぁ、コールバックの引数も違って使いづらいんだけど、とりあえず使える。

少し整理するとこんな感じ。fs.watchがちゃんと動いてくれる方がうれしいので、まぁ、たぶん、今のところの対策かな。

var watch = (process.platform === 'win32')?fs.watchFile:fs.watch
watch(file, function (arg1, arg2) {
  console.log('ファイルが更新されました');
});

あとは、fs.statSync(file).mtimeなどで更新時間を自前監視?

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7