main.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2010-2011 Mikeal Rogers
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. var sys = require('util')
  15. , fs = require('fs')
  16. , path = require('path')
  17. , events = require('events')
  18. ;
  19. function walk (dir, options, callback) {
  20. if (!callback) {callback = options; options = {}}
  21. if (!callback.files) callback.files = {};
  22. if (!callback.pending) callback.pending = 0;
  23. callback.pending += 1;
  24. fs.stat(dir, function (err, stat) {
  25. if (err) return callback(err);
  26. callback.files[dir] = stat;
  27. fs.readdir(dir, function (err, files) {
  28. if (err) {
  29. if(err.code === 'EACCES' && options.ignoreUnreadableDir) return callback();
  30. return callback(err);
  31. }
  32. callback.pending -= 1;
  33. files.forEach(function (f, index) {
  34. f = path.join(dir, f);
  35. callback.pending += 1;
  36. fs.stat(f, function (err, stat) {
  37. var enoent = false
  38. , done = false;
  39. if (err) {
  40. if (err.code !== 'ENOENT' && (err.code !== 'EPERM' && options.ignoreNotPermitted)) {
  41. return callback(err);
  42. } else {
  43. enoent = true;
  44. }
  45. }
  46. callback.pending -= 1;
  47. done = callback.pending === 0;
  48. if (!enoent) {
  49. if (options.ignoreDotFiles && path.basename(f)[0] === '.') return done && callback(null, callback.files);
  50. if (options.filter && !options.filter(f, stat)) return done && callback(null, callback.files);
  51. callback.files[f] = stat;
  52. if (stat.isDirectory() && !(options.ignoreDirectoryPattern && options.ignoreDirectoryPattern.test(f))) walk(f, options, callback);
  53. done = callback.pending === 0;
  54. if (done) callback(null, callback.files);
  55. }
  56. })
  57. })
  58. if (callback.pending === 0) callback(null, callback.files);
  59. })
  60. if (callback.pending === 0) callback(null, callback.files);
  61. })
  62. }
  63. var watchedFiles = Object.create(null);
  64. exports.watchTree = function ( root, options, callback ) {
  65. if (!callback) {callback = options; options = {}}
  66. walk(root, options, function (err, files) {
  67. if (err) throw err;
  68. var fileWatcher = function (f) {
  69. fs.watchFile(f, options, function (c, p) {
  70. // Check if anything actually changed in stat
  71. if (files[f] && !files[f].isDirectory() && c.nlink !== 0 && files[f].mtime.getTime() == c.mtime.getTime()) return;
  72. files[f] = c;
  73. if (!files[f].isDirectory()) callback(f, c, p);
  74. else {
  75. fs.readdir(f, function (err, nfiles) {
  76. if (err) return;
  77. nfiles.forEach(function (b) {
  78. var file = path.join(f, b);
  79. if (!files[file] && (options.ignoreDotFiles !== true || b[0] != '.')) {
  80. fs.stat(file, function (err, stat) {
  81. if (options.filter && !options.filter(file, stat)) return;
  82. callback(file, stat, null);
  83. files[file] = stat;
  84. fileWatcher(file);
  85. })
  86. }
  87. })
  88. })
  89. }
  90. if (c.nlink === 0) {
  91. // unwatch removed files.
  92. delete files[f]
  93. fs.unwatchFile(f);
  94. }
  95. })
  96. }
  97. fileWatcher(root);
  98. for (var i in files) {
  99. fileWatcher(i);
  100. }
  101. watchedFiles[root] = files;
  102. callback(files, null, null);
  103. })
  104. }
  105. exports.unwatchTree = function (root) {
  106. if (!watchedFiles[root]) return;
  107. Object.keys(watchedFiles[root]).forEach(fs.unwatchFile);
  108. watchedFiles[root] = false;
  109. };
  110. exports.createMonitor = function (root, options, cb) {
  111. if (!cb) {cb = options; options = {}}
  112. var monitor = new events.EventEmitter();
  113. monitor.stop = exports.unwatchTree.bind(null, root);
  114. var prevFile = {file: null,action: null,stat: null};
  115. exports.watchTree(root, options, function (f, curr, prev) {
  116. // if not curr, prev, but f is an object
  117. if (typeof f == "object" && prev == null && curr === null) {
  118. monitor.files = f;
  119. return cb(monitor);
  120. }
  121. // if not prev and either prevFile.file is not f or prevFile.action is not created
  122. if (!prev) {
  123. if (prevFile.file != f || prevFile.action != "created") {
  124. prevFile = { file: f, action: "created", stat: curr };
  125. return monitor.emit("created", f, curr);
  126. }
  127. }
  128. // if curr.nlink is 0 and either prevFile.file is not f or prevFile.action is not removed
  129. if (curr) {
  130. if (curr.nlink === 0) {
  131. if (prevFile.file != f || prevFile.action != "removed") {
  132. prevFile = { file: f, action: "removed", stat: curr };
  133. return monitor.emit("removed", f, curr);
  134. }
  135. }
  136. }
  137. // if prevFile.file is null or prevFile.stat.mtime is not the same as curr.mtime
  138. if (prevFile.file === null) {
  139. return monitor.emit("changed", f, curr, prev);
  140. }
  141. // stat might return null, so catch errors
  142. try {
  143. if (prevFile.stat.mtime.getTime() !== curr.mtime.getTime()) {
  144. return monitor.emit("changed", f, curr, prev);
  145. }
  146. } catch(e) {
  147. return monitor.emit("changed", f, curr, prev);
  148. }
  149. })
  150. }
  151. exports.walk = walk;