Created
November 11, 2016 13:07
-
-
Save birjj/0b0857402dd079cca2f1beebc98a8315 to your computer and use it in GitHub Desktop.
chokidar/#552
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var watchPath = "test/"; | |
| var found = (function(){ | |
| var hist = {}, | |
| done = false, | |
| timer; | |
| return function found(path){ | |
| if (done) { | |
| console.log("Already checked. Please rerun."); | |
| return; | |
| } | |
| if (hist[path]) { | |
| console.log("Found path multiple times: "+path); | |
| return; | |
| } | |
| hist[path] = true; | |
| clearTimeout(timer); | |
| timer = setTimeout(function(){ | |
| done = true; | |
| check(hist); | |
| }, 500); | |
| }; | |
| })(); | |
| function check(files) { | |
| var fs = require("fs"), | |
| path = require("path"); | |
| var badFiles = 0, | |
| total = 0; | |
| walk(watchPath, function(file){ | |
| ++total; | |
| if (!files[file]) { | |
| console.log("File without add event: ",file); | |
| ++badFiles; | |
| } | |
| }); | |
| console.log("Add events seen:",Object.keys(files).length); | |
| console.log("Files found:",total); | |
| console.log("Files without add event:",badFiles); | |
| } | |
| // http://stackoverflow.com/questions/2727167/getting-all-filenames-in-a-directory-with-node-js | |
| function walk(searchPath, cb) { | |
| var fs = require("fs"), | |
| path = require("path"); | |
| fs.readdirSync(searchPath).forEach(function(name){ | |
| var filePath = path.join(searchPath, name); | |
| var stat = fs.statSync(filePath); | |
| if (stat.isFile()) { | |
| cb(filePath); | |
| } else { | |
| walk(filePath, cb); | |
| } | |
| }); | |
| } | |
| require('chokidar').watch(watchPath, { | |
| ignoreInitial: true | |
| }).on('add', function(path) { | |
| found(path); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment