【问题标题】:Watch a folder for changes using node.js, and print file paths when they are changed使用 node.js 监视文件夹的更改,并在更改时打印文件路径
【发布时间】:2012-11-21 14:45:42
【问题描述】:

我正在尝试编写一个 node.js 脚本来监视文件目录中的更改,然后打印更改的文件。如何修改此脚本以使其监视目录(而不是单个文件),并在更改目录中的文件时打印它们的名称?

var fs = require('fs'),
    sys = require('sys');
var file = '/home/anderson/Desktop/fractal.png'; //this watches a file, but I want to watch a directory instead
fs.watchFile(file, function(curr, prev) {
    alert("File was modified."); //is there some way to print the names of the files in the directory as they are modified?
});

【问题讨论】:

  • 我想知道这是否相关:stackoverflow.com/questions/12063266/…(不过我不熟悉 gruntjs - 我希望有其他解决方案。)
  • 也许像 node-inotify-plusplus 这样的东西会有用:stackoverflow.com/questions/5877263/…
  • 你运行的是什么操作系统?这很重要,因为 Unix/Linux 使用 inotify,OSX 使用 FSWatch 和我不知道 Windoze,但我相信 Google 可以告诉你,查看文件的低级机制有很大不同。
  • 虽然我最初在我的项目中使用了普通的node-inotify,但我最终切换到node-inotify-plusplus,因为谁不喜欢抽象? =)

标签: node.js


【解决方案1】:

试试Chokidar:

var chokidar = require('chokidar');

var watcher = chokidar.watch('file or dir', {ignored: /^\./, persistent: true});

watcher
  .on('add', function(path) {console.log('File', path, 'has been added');})
  .on('change', function(path) {console.log('File', path, 'has been changed');})
  .on('unlink', function(path) {console.log('File', path, 'has been removed');})
  .on('error', function(error) {console.error('Error happened', error);})

Chokidar 解决了仅使用 fs 观看文件的一些跨平台问题。

【讨论】:

  • 它是否处理子文件夹?
  • 一个问题 - 当我复制一个大文件时。添加事件会立即触发,然后随着文件复制的进行,我会收到数百个更改事件。有什么方法可以在文件复制结束时仅触发一个事件?
  • @Curious101,你试过加awaitWriteFinish: true吗?默认为假。
  • 我想问一件事,它是否值得在大型文件系统上使用,你可以说我想监控/home/terabyte。好吧不是递归的
  • 我可以使用这个 Create React App 吗?当然不在生产中。
【解决方案2】:

为什么不直接使用旧的fs.watch?它非常简单。

fs.watch('/path/to/folder', (eventType, filename) => {
console.log(eventType);
// could be either 'rename' or 'change'. new file event and delete
// also generally emit 'rename'
console.log(filename);
})

有关选项参数的更多信息和详细信息,请参阅Node fs Docs

【讨论】:

  • 警告注意,我已经在我的 Mac 上测试过了,这段代码只检测到文件夹级别而不是任何子目录的更改,所以请务必添加选项以递归方式观看第二个参数;请参阅上面链接的文档
  • 在@OzzyTheGiant 的注释中添加了注释:递归选项仅在 macOS 和 Windows 上受支持。
  • Node 的 fs.watch() 有点笨拙,您必须构建自己的去抖动实现。
  • @ThomasJayRush 一种机制,允许在事件和动作发生之间传递一定的时间,因此如果事件触发两次,则动作只会被调用一次。这是一个工程术语,其中物理按钮会触发电流,但按钮会“弹跳”一次或多次,从而在电流中产生多个尖峰,而只需要一个尖峰——这也可以应用于编程——尤其是 JS。 medium.com/@jamischarles/what-is-debouncing-2505c0648ff1
  • 请使用 chokidar,因为此 api 跨平台不一致。
【解决方案3】:

试试hound:

hound = require('hound')

// Create a directory tree watcher.
watcher = hound.watch('/tmp')

// Create a file watcher.
watcher = hound.watch('/tmp/file.txt')

// Add callbacks for file and directory events.  The change event only applies
// to files.
watcher.on('create', function(file, stats) {
  console.log(file + ' was created')
})
watcher.on('change', function(file, stats) {
  console.log(file + ' was changed')
})
watcher.on('delete', function(file) {
  console.log(file + ' was deleted')
})

// Unwatch specific files or directories.
watcher.unwatch('/tmp/another_file')

// Unwatch all watched files and directories.
watcher.clear()

文件一旦改变就会执行

【讨论】:

    猜你喜欢
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多