simple-watcher
Advanced tools
Comparing version 0.1.0 to 0.1.1
59
index.js
@@ -12,17 +12,16 @@ 'use strict' | ||
fs.watch(workingDir, options, (event, fileName) => { | ||
let w = fs.watch(workingDir, options, (event, fileName) => { | ||
callback(path.join(workingDir, fileName)) | ||
}) | ||
w.on('error', (e) => { | ||
w.close() | ||
}) | ||
} | ||
// Attach watchers recursively. | ||
// This code is synchronous in order to be able tell when it actuall ends. | ||
let watchFolderFallback = (parent, callback) => { | ||
// Skip if not a directory. | ||
if (!fs.statSync(parent).isDirectory()) { | ||
return | ||
} | ||
fs.stat(parent, (err, stats) => { | ||
if (err || !stats.isDirectory()) { | ||
// This code is synchronous to be able to tell when it actually finishes. | ||
try { | ||
// Skip if not a directory. | ||
if (!fs.statSync(parent).isDirectory()) { | ||
return | ||
@@ -34,13 +33,9 @@ } | ||
// Iterate over list of children. | ||
fs.readdir(parent, (err, children) => { | ||
if (err) { | ||
return | ||
} | ||
children.forEach((child) => { | ||
child = path.resolve(parent, child) | ||
watchFolderFallback(child, callback) | ||
}) | ||
fs.readdirSync(parent).forEach((child) => { | ||
child = path.resolve(parent, child) | ||
watchFolderFallback(child, callback) | ||
}) | ||
}) | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
@@ -51,9 +46,27 @@ | ||
let cache = {} | ||
if (PLATFORMS.indexOf(process.platform) !== -1) { | ||
watchFolder(workingDir, true, callback) | ||
} else { | ||
watchFolderFallback(workingDir, callback) | ||
return watchFolder(workingDir, true, callback) | ||
} | ||
watchFolderFallback(workingDir, (localPath) => { | ||
fs.stat(localPath, (err, stat) => { | ||
// Delete cache entry. | ||
if (err) { | ||
delete cache[localPath] | ||
return | ||
} | ||
// Add new handler for new directory and save in cache. | ||
if (stat.isDirectory() && !cache[localPath]) { | ||
cache[localPath] = true | ||
watchFolder(localPath, false, callback) | ||
} | ||
}) | ||
callback(localPath) | ||
}) | ||
} | ||
module.exports = watch |
{ | ||
"name": "simple-watcher", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "\"A simple recursive directory watcher.\"", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -40,3 +40,3 @@ # Simple Watcher | ||
let now = Date.now() | ||
if (filePath === last.fileName && now - last.timestamp < delta) { | ||
if (filePath === last.filePath && now - last.timestamp < delta) { | ||
return | ||
@@ -43,0 +43,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5328
59