simple-watcher
Advanced tools
Comparing version 1.0.5 to 2.0.0
70
index.js
@@ -6,9 +6,8 @@ 'use strict' | ||
const TOLERANCE = 200 | ||
const PLATFORMS = ['win32', 'darwin'] | ||
const INTERVAL = 300 | ||
// OS watcher. | ||
let watchFolder = (workingDir, recursive, tolerance, callback) => { | ||
let watchFolder = (workingDir, recursive, callback) => { | ||
let options = { persistent: true, recursive: recursive } | ||
let last = { filePath: null, timestamp: 0 } | ||
@@ -19,21 +18,3 @@ let w = fs.watch(workingDir, options, (event, fileName) => { | ||
let filePath = fileName ? path.join(workingDir, fileName) : workingDir | ||
if (!tolerance) { | ||
return callback(filePath) | ||
} | ||
fs.stat(filePath, (err, stat) => { | ||
// If error, the file was likely deleted. | ||
let timestamp = err ? 0 : (new Date(stat.mtime)).getTime() | ||
let ready = err || timestamp - last.timestamp >= tolerance | ||
let fileMatches = filePath === last.filePath | ||
last.filePath = filePath | ||
last.timestamp = timestamp | ||
if (fileMatches && !ready) { | ||
return | ||
} | ||
callback(filePath) | ||
}) | ||
callback(filePath) | ||
}) | ||
@@ -46,3 +27,4 @@ | ||
let watchFolderFallback = (parent, tolerance, callback) => { | ||
// Recursive fallback handler. | ||
let watchFolderFallback = (parent, callback) => { | ||
// This code is synchronous to be able to tell when it actually finishes. | ||
@@ -55,3 +37,3 @@ try { | ||
watchFolder(parent, false, tolerance, callback) | ||
watchFolder(parent, false, callback) | ||
@@ -61,3 +43,3 @@ // Iterate over list of children. | ||
child = path.resolve(parent, child) | ||
watchFolderFallback(child, tolerance, callback) | ||
watchFolderFallback(child, callback) | ||
}) | ||
@@ -69,13 +51,8 @@ } catch (err) { | ||
let watch = (workingDir, callback, tolerance) => { | ||
let watch = (workingDir, callback) => { | ||
workingDir = path.resolve(workingDir) | ||
// Set the default tolerance value. | ||
tolerance = tolerance === undefined ? TOLERANCE : tolerance | ||
// Enable tolerance only for Windows. | ||
tolerance = process.platform === 'win32' ? tolerance : 0 | ||
// Use recursive flag if natively available. | ||
if (PLATFORMS.indexOf(process.platform) !== -1) { | ||
return watchFolder(workingDir, true, tolerance, callback) | ||
return watchFolder(workingDir, true, callback) | ||
} | ||
@@ -85,3 +62,3 @@ | ||
let cache = {} | ||
watchFolderFallback(workingDir, tolerance, (localPath) => { | ||
watchFolderFallback(workingDir, (localPath) => { | ||
fs.stat(localPath, (err, stat) => { | ||
@@ -97,3 +74,3 @@ // Delete cache entry. | ||
cache[localPath] = true | ||
watchFolder(localPath, false, tolerance, callback) | ||
watchFolder(localPath, false, callback) | ||
} | ||
@@ -106,2 +83,25 @@ }) | ||
module.exports = watch | ||
let main = (workingDir, callback, interval) => { | ||
interval = interval !== undefined ? interval : INTERVAL | ||
console.log(1) | ||
// Enqueue items on change. | ||
let queue = [] | ||
watch(workingDir, (filePath) => { | ||
queue.push(filePath) | ||
}) | ||
// Start the interval. | ||
setInterval(() => { | ||
// Dequeue paths and store them as dictionary keys. | ||
let dict = {} | ||
while (queue.length) { | ||
dict[queue.pop()] = true | ||
} | ||
// Run callback with unique paths. | ||
let unique = Object.keys(dict) | ||
unique.length && callback(unique) | ||
}, interval) | ||
} | ||
module.exports = main |
{ | ||
"name": "simple-watcher", | ||
"version": "1.0.5", | ||
"version": "2.0.0", | ||
"description": "\"A simple recursive directory watcher.\"", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,4 +5,4 @@ 'use strict' | ||
watch(process.argv[2], (fileName) => { | ||
console.log(`Changed: ${fileName}`) | ||
watch(process.argv[2], (files) => { | ||
console.log(files) | ||
}) |
86
6026
30