fs-watch-tree
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -1,95 +0,3 @@ | ||
var path = require("path"); | ||
var fs = require("fs"); | ||
var wt = require("./walk-tree"); | ||
function createEvent(dirs, event, dir, fileName) { | ||
var fullPath = path.join(dir, fileName); | ||
var exists = dirs.some(function (d) { return d === fullPath; }); | ||
var statObj; | ||
function stat() { | ||
if (statObj) { return statObj; } | ||
if (!fullPath) { | ||
statObj = { isDirectory: function () { return false; } }; | ||
} else { | ||
try { | ||
statObj = fs.statSync(fullPath); | ||
} catch (e) { | ||
statObj = { | ||
isDirectory: function () { return false; }, | ||
deleted: true | ||
}; | ||
} | ||
} | ||
return statObj; | ||
} | ||
return { | ||
name: fullPath, | ||
isDirectory: function () { | ||
return stat().isDirectory(); | ||
}, | ||
isMkdir: function () { | ||
return this.isDirectory() && !exists; | ||
}, | ||
isDelete: function () { | ||
return !!stat().deleted; | ||
}, | ||
isModify: function () { | ||
return !this.isDelete() && !this.isMkdir(); | ||
} | ||
}; | ||
} | ||
function watch(state, dir, options, callback) { | ||
return fs.watch(dir, function (event, fileName) { | ||
var e = createEvent(state.dirs, event, dir, fileName); | ||
if (e.isDirectory() && e.isMkdir()) { | ||
addWatch(state, e.name, options, callback); | ||
} | ||
if (!wt.isExcluded(e.name, options.exclude) && | ||
typeof callback == "function") { | ||
callback(e); | ||
} | ||
}); | ||
} | ||
function addWatch(state, dir, options, callback) { | ||
state.dirs = state.dirs || []; | ||
state.dirs.push(dir); | ||
state.watches = state.watches || []; | ||
state.watches.push(watch(state, dir, options, callback)); | ||
} | ||
function watchTree(dir, options, callback) { | ||
if (arguments.length == 2 && typeof options == "function") { | ||
callback = options; | ||
options = {}; | ||
} | ||
var state = {}; | ||
options = options || {}; | ||
options.exclude = wt.excludeRegExes(options.exclude); | ||
addWatch(state, dir, options, callback); | ||
wt.walkTree(dir, options, function (err, dir) { | ||
if (err) return; | ||
addWatch(state, dir, options, callback); | ||
}); | ||
return { | ||
end: function () { | ||
state.watches.forEach(function (w) { w.close(); }); | ||
} | ||
}; | ||
} | ||
module.exports = { | ||
watchTree: watchTree | ||
}; | ||
module.exports = process.platform === "linux" ? | ||
require("./watch-tree-unix") : | ||
require("./watch-tree-generic"); |
{ | ||
"name": "fs-watch-tree", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Recursively watch directories for changes", | ||
@@ -15,2 +15,5 @@ "homepage": "http://busterjs.org/doc/fs-watch-tree", | ||
"url": "http://augustl.com" | ||
}, { | ||
"name": "Magnar Sveen", | ||
"email": "magnars@gmail.com" | ||
}], | ||
@@ -22,2 +25,5 @@ "main": "./lib/fs-watch-tree", | ||
}, | ||
"dependencies": { | ||
"when": "https://github.com/cujojs/when/tarball/1.0.2" | ||
}, | ||
"devDependencies": { | ||
@@ -24,0 +30,0 @@ "rimraf": "*" |
@@ -1,19 +0,11 @@ | ||
# watch-tree - Recursive directory watch (for Linux) # | ||
# fs-watch-tree - Recursive fs.watch # | ||
**watch-tree** is a small tool to watch directories for changes recursively. It | ||
uses [node-inotify](https://github.com/c4milo/node-inotify) to watch for | ||
changes, thus only works on Linux at the moment. Help adding support for | ||
[NodeJS-FSEvents](https://github.com/phidelta/NodeJS-FSEvents) for OSX would be | ||
appreciated. | ||
**fs-watch-tree** is a small tool to watch directories for changes recursively. | ||
It uses | ||
[fs-watch](http://nodejs.org/docs/latest/api/fs.html#fs_fs_watch_filename_options_listener) | ||
to watch for changes, thus should work on most platforms. | ||
Note that this library is likely a temporary one, as | ||
[Node will likely support](https://github.com/joyent/libuv/issues/68) recursive | ||
directory watches cross-platform through **libuv** sometime soon-ish. | ||
The API conciously does not expose any inotify internals directly in order to | ||
ease the addition of FSEvents support down the road. | ||
## Synopsis ## | ||
var watchTree = require("watch-tree").watchTree; | ||
var watchTree = require("fs-watch-tree").watchTree; | ||
@@ -34,7 +26,3 @@ var watch = watchTree("/home/christian", function (event) { | ||
Watches directory `dir` recursively for changes. Only a subset of the inotify | ||
events are supported; create file/diretory, modify file/directory and delete | ||
file/directory. Other events, such as access, is not currently supported. This | ||
limitation is simply the result of my YAGNI approach. Other events may be added | ||
later if necessary. | ||
Watches directory `dir` recursively for changes. | ||
@@ -83,18 +71,14 @@ The callback is called with an `event` object. The event is described below. | ||
The full (relative) path to the file/directory that changed. As far as I know, | ||
this will not be available with FSEvents, so it may go away. | ||
The full (relative) path to the file/directory that changed. | ||
### `isDirectory()` ### | ||
Returns true if the cause of the change was a directory. Same challenge as | ||
`name`. | ||
Returns true if the cause of the change was a directory. In some cases, | ||
e.g. when the directory was deleted, it's not possible to know if the | ||
source was a directory. In that case, this method returns false. | ||
### `isFile()` ### | ||
### `isMkdir()` ### | ||
Returns true if the cause of the change was a file. Same challenge as `name`. | ||
Returns true if the cause of the event was a newly created directory. | ||
### `isCreate()` ### | ||
Returns true if the cause of the event was a newly created file/directory. | ||
### `isDelete()` ### | ||
@@ -101,0 +85,0 @@ |
@@ -0,1 +1,2 @@ | ||
var when = require("when"); | ||
var path = require("path"); | ||
@@ -7,3 +8,3 @@ var fs = require("fs"); | ||
mktree: function mktree(tree, root) { | ||
mktreeSync: function mktreeSync(tree, root) { | ||
root = root || module.exports.ROOT; | ||
@@ -17,3 +18,3 @@ var file; | ||
fs.mkdirSync(file, "0755"); | ||
mktree(tree[prop], file); | ||
mktreeSync(tree[prop], file); | ||
} else { | ||
@@ -25,3 +26,26 @@ fs.writeFileSync(file, tree[prop], "utf-8"); | ||
return module.exports.ROOT; | ||
}, | ||
mktree: function mktree(tree, root) { | ||
root = root || module.exports.ROOT; | ||
var file, d; | ||
var promises = []; | ||
for (var prop in tree) { | ||
d = when.defer(); | ||
file = path.join(root, prop); | ||
promises.push(d.promise); | ||
if (typeof tree[prop] == "object") { | ||
fs.mkdir(file, "0755", function (prop, file, d) { | ||
mktree(tree[prop], file).then(d.resolve); | ||
}.bind(this, prop, file, d)); | ||
} else { | ||
fs.writeFile(file, tree[prop], "utf-8", d.resolve); | ||
} | ||
} | ||
return when.all(promises); | ||
} | ||
}; |
@@ -10,3 +10,3 @@ var buster = require("buster"); | ||
return function (done) { | ||
var root = helper.mktree(options.tree); | ||
var root = helper.mktreeSync(options.tree); | ||
@@ -22,3 +22,3 @@ var callback = this.spy(function () { | ||
options.expected.forEach(function (dir) { | ||
assert.calledWith(callback, null, root + dir); | ||
assert.calledWith(callback, null, path.join(root, dir)); | ||
}); | ||
@@ -37,3 +37,3 @@ | ||
buster.testCase("Walk tree", { | ||
buster.testCase("walk-tree", { | ||
setUp: function () { | ||
@@ -67,3 +67,3 @@ fs.mkdirSync(helper.ROOT, "0755"); | ||
expected: ["/projects", "/documents"], | ||
exclude: [helper.ROOT + "/music"], | ||
exclude: ["music"], | ||
tree: { | ||
@@ -116,3 +116,3 @@ projects: {}, | ||
"yields stat error to callback": function (done) { | ||
var root = helper.mktree({ "a": {} }); | ||
var root = helper.mktreeSync({ "a": {} }); | ||
this.stub(fs, "stat").yields({ message: "Oops" }); | ||
@@ -119,0 +119,0 @@ |
HTTP dependency
Supply chain riskContains a dependency which resolves to a remote HTTP URL which could be used to inject untrusted code and reduce overall package reliability.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
51806
26
1277
1
89
1
13
1