运行下面的代码时,只有当我使用我的 ide、TextEditor.app 或 vim 手动编辑和保存 tmp.txt 时才会触发手表。
它不是通过写入流或手动 shell 输出重定向(键入 echo "test" > /path/to/tmp.txt")的方法。
虽然如果我看文件本身,而不是它的目录名,那么它就可以工作。
var fs, Path, file, watchPath, w;
fs = require('fs' );
Path = require('path');
file = __dirname + '/tmp.txt';
watchPath = Path.dirname(file); // changing this to just file makes it trigger
w = fs.watch ( watchPath, function (e,f) {
console.log("will not get here by itself");
w.close();
});
fs.writeFileSync(file,"test","utf-8");
fs.createWriteStream(file, {
flags:'w',
mode: 0777
} )
.end('the_date="'+new Date+'";' ); // another method fails as well
setTimeout (function () {
fs.writeFileSync(file,"test","utf-8");
},500); // as does this one
// child_process exec and spawn fail the same way with or without timeout
所以问题是:为什么?以及如何从节点脚本以编程方式触发此事件?
谢谢!