simple-watcher
Advanced tools
Comparing version 0.1.2 to 1.0.0
33
index.js
@@ -6,13 +6,26 @@ 'use strict' | ||
const TOLERANCE = 200 | ||
const PLATFORMS = ['win32', 'darwin'] | ||
// OS watcher. | ||
let watchFolder = (workingDir, recursive, callback) => { | ||
let watchFolder = (workingDir, recursive, tolerance, callback) => { | ||
let options = { persistent: true, recursive: recursive } | ||
let last = { filePath: null, timestamp: null } | ||
let w = fs.watch(workingDir, options, (event, fileName) => { | ||
// On Windows it may actually be empty. | ||
if (fileName) { | ||
callback(path.join(workingDir, fileName)) | ||
// On Windows fileName may actually be empty. | ||
let filePath = fileName ? path.join(workingDir, fileName) : workingDir | ||
// Eliminate double reporting. | ||
if (tolerance) { | ||
let now = Date.now() | ||
if (filePath === last.filePath && now - last.timestamp < tolerance) { | ||
return | ||
} | ||
last.filePath = filePath | ||
last.timestamp = now | ||
} | ||
callback(filePath) | ||
}) | ||
@@ -45,11 +58,15 @@ | ||
let watch = (workingDir, callback) => { | ||
let watch = (workingDir, callback, tolerance = TOLERANCE) => { | ||
workingDir = path.resolve(workingDir) | ||
let cache = {} | ||
// 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, callback) | ||
return watchFolder(workingDir, true, tolerance, callback) | ||
} | ||
// Attach handlers for each folder recursively. | ||
let cache = {} | ||
watchFolderFallback(workingDir, (localPath) => { | ||
@@ -66,3 +83,3 @@ fs.stat(localPath, (err, stat) => { | ||
cache[localPath] = true | ||
watchFolder(localPath, false, callback) | ||
watchFolder(localPath, false, tolerance, callback) | ||
} | ||
@@ -69,0 +86,0 @@ }) |
{ | ||
"name": "simple-watcher", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"description": "\"A simple recursive directory watcher.\"", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -13,12 +13,16 @@ # Simple Watcher | ||
* Leverages the `recursive` options on OS X and Windows; uses a fallback for other platforms. | ||
* Does not care about Windows reporting multiple changes for one file (a simple workaround for this below). | ||
* Takes care of WinAPI's `ReadDirectoryChangesW` [double reporting](http://stackoverflow.com/questions/14036449/c-winapi-readdirectorychangesw-receiving-double-notifications). | ||
## Usage | ||
Basic example: | ||
```JavaScript | ||
const watch = require('simple-watcher') | ||
/** | ||
* Recursively watches for directory changes. | ||
* @param {string} workingDir - Directory to watch. | ||
* @param {function} callback - Triggered on change. | ||
* @param {number} tolerance - Interval in which multiple changes to the same file | ||
* on Windows will be treated as one; default is 200ms. | ||
*/ | ||
watch('/path/to/directory', (filePath) => { | ||
@@ -28,25 +32,1 @@ console.log(`Changed: ${filePath}`) | ||
``` | ||
WinAPI's `ReadDirectoryChangesW` double reporting fix: | ||
```JavaScript | ||
const watch = require('simple-watcher') | ||
let last = { filePath: null, timestamp: null } | ||
let delta = 300 // Adjust if needed. | ||
watch('/path/to/directory', (filePath) => { | ||
// Skip if the last change within the delta time was the same. | ||
let now = Date.now() | ||
if (filePath === last.filePath && now - last.timestamp < delta) { | ||
return | ||
} | ||
// Save the change. | ||
last.filePath = filePath | ||
last.timestamp = now | ||
console.log(`Changed: ${filePath}`) | ||
}) | ||
``` |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
5847
76
0
31