1

ディレクトリを再帰的に監視しようとしていますが、名前空間の問題に遭遇しました。

私のコードは次のようになります。

for (i in files) {
    var file = path.join(process.cwd(), files[i]);
    fs.lstat(file, function(err, stats) {
        if (err) {
            throw err;
        } else {
            if (stats.isDirectory()) {
                // Watch the directory and traverse the child file.
                fs.watch(file);
                recursiveWatch(file);
            }   
        }   
    }); 
}

stat'd された最後のディレクトリのみを見ているようです。問題は、lstat コールバックが終了する前にループが終了することだと思います。したがって、lstat コールバックが呼び出されるたびに、 file = . これに対処するにはどうすればよいですか?ありがとうございました!

4

2 に答える 2

2

次の使用を検討することをお勧めします:(ES5であり、それfilesArrayファイル名であると想定)

files.forEach(function(file) {
  file = path.join(process.cwd(), file);
  fs.lstat(file, function(err, stats) {
    if (err) {
      throw err;
    } else {
      if (stats.isDirectory()) {
        // Watch the directory and traverse the child file.
        fs.watch(file);
        recursiveWatch(file);
      }
    }
  });
});
于 2012-04-28T04:05:25.550 に答える