2012-12-04 17 views
33

Tôi đang cố gắng viết tập lệnh node.js để theo dõi các thay đổi trong thư mục tệp và sau đó in các tệp được thay đổi . Làm thế nào tôi có thể sửa đổi kịch bản này để nó xem một thư mục (thay vì một tệp riêng lẻ) và in tên của các tệp trong thư mục khi chúng được thay đổi?Xem thư mục cho các thay đổi bằng cách sử dụng node.js và đường dẫn tệp in khi chúng được thay đổi

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? 
}); 
+0

Tôi tự hỏi nếu điều này là có liên quan: http://stackoverflow.com/questions/12063266/how-do-you-watch-multiple -files-but-only-run-task-on-change-file-in-grunt-js (Tôi không quen với gruntjs, mặc dù - Tôi hy vọng có những giải pháp khác.) –

+0

Có lẽ một cái gì đó giống như nút-inotify-plusplus sẽ hữu ích: http://stackoverflow.com/questions/5877263/monitoring-directory-for-changes-potential-high-memory –

+0

Bạn đang chạy hệ điều hành nào? Điều này quan trọng vì các cơ chế cấp thấp để xem một tệp rất khác nhau khi Unix/Linux sử dụng 'inotify', OSX sử dụng' FSWatch' và tôi không có ý tưởng về Windoze nhưng tôi chắc chắn Google có thể cho bạn biết. – srquinn

Trả lời

65

Hãy thử 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 giải quyết một số vấn đề crossplatform với xem file chỉ sử dụng fs.

+1

Ứng dụng có xử lý các thư mục con không? –

+0

có! nó xử lý –

9

thử 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() 

Nó sẽ thực hiện một lần tập tin đã thay đổi

5

Tại sao không chỉ cần sử dụng cũ fs.watch? Nó khá đơn giản.

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); 
}) 

Để biết thêm thông tin chi tiết và về các tùy chọn param, xem Node fs Docs

+0

Lưu ý cảnh báo, tôi đã thử nghiệm điều này trên mac của tôi và mã này chỉ phát hiện thấy cấp thư mục đó chứ không phải bất kỳ thư mục con nào thay đổi, vì vậy hãy đảm bảo thêm tùy chọn để xem đệ quy làm tham số thứ hai; xem tài liệu được liên kết ở trên – OzzyTheGiant

+0

Đã thêm Ghi chú vào Ghi chú của @ OzzyTheGiant: Tùy chọn đệ quy chỉ được hỗ trợ trên macOS và Windows. –

Các vấn đề liên quan