Comparing version 0.2.4 to 0.2.5
@@ -0,1 +1,7 @@ | ||
0.2.5 / 2012-08-21 | ||
------------------ | ||
* Allows call FsTools.walk() with pathname pointing file. | ||
0.2.4 / 2012-08-19 | ||
@@ -2,0 +8,0 @@ ------------------ |
@@ -52,3 +52,3 @@ /** | ||
// NOTICE: It walks through single dimension of file system - it won't go into | ||
// found sub-directories. See `walk_recursive` for this puprpose. | ||
// found sub-directories. See `walk_deep` for this puprpose. | ||
// | ||
@@ -73,2 +73,3 @@ // Example: | ||
// }); | ||
// | ||
function walk_flat(path, iterator, callback) { | ||
@@ -93,2 +94,35 @@ fs.readdir(path, function (err, files) { | ||
// walk_deep(path, match, iterator, callback) -> void | ||
// - path (String): Path to iterate through | ||
// - match (Function): Path test whenever iterator should be executed or not | ||
// - iterator (Function): Will be fired on each element within `path` | ||
// - callback (Function): Will be fired once all files were processed | ||
// | ||
// Walks through given `path` with all it's nested childs calling | ||
// `iterator(path, callback)` for each found file. | ||
// | ||
function walk_deep(path, match, iterator, callback) { | ||
walk_flat(path_normalize(path), function (path, next) { | ||
fs.lstat(path, function (err, stats) { | ||
if (err) { | ||
next(err); | ||
return; | ||
} | ||
if (stats.isDirectory()) { | ||
walk_deep(path, match, iterator, next); | ||
return; | ||
} | ||
if (match(path)) { | ||
iterator(path, stats, next); | ||
return; | ||
} | ||
next(); | ||
}); | ||
}, callback); | ||
} | ||
function copy_file(src, dst, callback) { | ||
@@ -120,2 +154,6 @@ var ifd, ofd; | ||
* | ||
* If `path` points a file, iterator will be called against it (respecting | ||
* pattern if it was given). | ||
* | ||
* | ||
* ##### Iterator | ||
@@ -207,22 +245,21 @@ * | ||
walk_flat(path_normalize(path), function (path, next) { | ||
fs.lstat(path, function (err, stats) { | ||
if (err) { | ||
next(err); | ||
return; | ||
} | ||
path = path_normalize(path); | ||
fs.lstat(path, function (err, stats) { | ||
if (err) { | ||
callback('ENOENT' === err.code ? null : err); | ||
return; | ||
} | ||
if (stats.isDirectory()) { | ||
fstools.walk(path, pattern, iterator, next); | ||
return; | ||
} | ||
if (stats.isDirectory()) { | ||
walk_deep(path, match, iterator, callback); | ||
return; | ||
} | ||
if (match(path)) { | ||
iterator(path, stats, next); | ||
return; | ||
} | ||
if (match(path)) { | ||
iterator(path, stats, callback); | ||
return; | ||
} | ||
next(); | ||
}); | ||
}, callback); | ||
callback(); | ||
}); | ||
}; | ||
@@ -411,2 +448,3 @@ | ||
* | ||
* | ||
* ##### Example | ||
@@ -521,3 +559,3 @@ * | ||
**/ | ||
module.exports.move = function move(source, destination, callback) { | ||
fstools.move = function move(source, destination, callback) { | ||
fs.lstat(source, function (err, stats) { | ||
@@ -554,3 +592,3 @@ if (err) { | ||
/** | ||
* FsTools.tmpdir([template = /tmp/fstools.XXXXXX]) | ||
* FsTools.tmpdir([template = '/tmp/fstools.XXXXXX']) -> String | ||
* - template (String): Temporary directory pattern. | ||
@@ -569,3 +607,3 @@ * | ||
**/ | ||
module.exports.tmpdir = function tmpdir(template) { | ||
fstools.tmpdir = function tmpdir(template) { | ||
var match = (template || '/tmp/fstools.XXXXXX').match(/^(.*?)(X{3,})(.*?)$/), | ||
@@ -572,0 +610,0 @@ attempts, length, random, pathname; |
{ | ||
"name" : "fs-tools", | ||
"version" : "0.2.4", | ||
"version" : "0.2.5", | ||
"description" : "fs helper utilities (walk, copy, mkdir -p)", | ||
@@ -5,0 +5,0 @@ "keywords" : ["fs", "file", "utils"], |
@@ -12,3 +12,4 @@ fs-tools | ||
Recurcively scan files by regex pattern & apply iterator to each. Iterator | ||
applied only to files, not to directories. | ||
applied only to files, not to directories. If given path is a file, iterator | ||
will be called against it (if pattern allows it). | ||
@@ -28,7 +29,17 @@ | ||
Copy file. Not optimized for big sourses (read all to memory at once). | ||
Copy file. | ||
### move(src, dst, callback) | ||
Move file. | ||
### tmpdir([template]) | ||
Returns unique directory (at the moment of request) pathbname. | ||
## License | ||
View the [LICENSE](https://github.com/nodeca/fs-tools/blob/master/LICENSE) file (MIT). |
@@ -50,3 +50,3 @@ 'use strict'; | ||
}, | ||
'makes shallow copy of src': function (err, result) { | ||
'makes exact copy of src': function (err, result) { | ||
Assert.ok(!err, 'Has no error'); | ||
@@ -53,0 +53,0 @@ Assert.equal(result.total, 20); |
@@ -61,4 +61,4 @@ 'use strict'; | ||
Assert.pathNotExists = function pathNotExists(path, msg) { | ||
msg = msg || "Doe not expect path '" + path + "' to exist."; | ||
msg = msg || "Does not expect path '" + path + "' to exist."; | ||
Assert.ok(!Fs.existsSync(path), msg); | ||
}; |
@@ -77,3 +77,49 @@ 'use strict'; | ||
} | ||
} | ||
}, | ||
'walking through the file': { | ||
topic: function () { | ||
var callback = this.callback, result; | ||
result = { total: 0, files: 0, symlinks: 0 }; | ||
FsTools.walk(SANDBOX + '/file', function (path, stats, next) { | ||
result.total += 1; | ||
if (stats.isFile()) { | ||
result.files += 1; | ||
} | ||
if (stats.isSymbolicLink()) { | ||
result.symlinks += 1; | ||
} | ||
next(); | ||
}, function (err) { | ||
callback(err, result); | ||
}); | ||
}, | ||
'calls iterator on exactly one file': function (err, result) { | ||
Assert.ok(!err, 'Has no errors'); | ||
Assert.equal(result.total, 1); | ||
Assert.equal(result.files, 1); | ||
Assert.equal(result.symlinks, 0); | ||
} | ||
}, | ||
'walking through the file with pattern': { | ||
topic: function () { | ||
var callback = this.callback, result = 0; | ||
FsTools.walk(SANDBOX + '/file', /link$/, function (path, stats, next) { | ||
result++; | ||
next(); | ||
}, function (err) { | ||
callback(err, result); | ||
}); | ||
}, | ||
'respects given pattern': function (err, result) { | ||
Assert.ok(!err, 'Has no errors'); | ||
Assert.equal(result, 0); | ||
} | ||
}, | ||
}).export(module); |
Sorry, the diff of this file is not supported yet
43110
25
999
44
10
2