add readdirSyncRecursive and the test runner file.
This commit is contained in:
parent
29cb04590b
commit
139ed486f1
9 changed files with 86 additions and 3 deletions
|
|
@ -11,7 +11,50 @@
|
||||||
* ~ Ryan McGrath (ryan [at] venodesigns.net)
|
* ~ Ryan McGrath (ryan [at] venodesigns.net)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var fs = require("fs");
|
var fs = require("fs"),
|
||||||
|
_path = require("path");
|
||||||
|
|
||||||
|
|
||||||
|
/* wrench.readdirSyncRecursive("directory_path");
|
||||||
|
*
|
||||||
|
* Recursively dives through directories and read the contents of all the
|
||||||
|
* children directories.
|
||||||
|
*/
|
||||||
|
exports.readdirSyncRecursive = function(baseDir) {
|
||||||
|
baseDir = baseDir.replace(/\/$/, '');
|
||||||
|
|
||||||
|
var readdirSyncRecursive = function(baseDir) {
|
||||||
|
var files = [],
|
||||||
|
curFiles,
|
||||||
|
nextDirs,
|
||||||
|
isDir = function(fname){
|
||||||
|
return fs.statSync( _path.join(baseDir, fname) ).isDirectory();
|
||||||
|
},
|
||||||
|
prependBaseDir = function(fname){
|
||||||
|
return _path.join(baseDir, fname);
|
||||||
|
};
|
||||||
|
|
||||||
|
curFiles = fs.readdirSync(baseDir);
|
||||||
|
nextDirs = curFiles.filter(isDir);
|
||||||
|
curFiles = curFiles.map(prependBaseDir);
|
||||||
|
|
||||||
|
files = files.concat( curFiles );
|
||||||
|
|
||||||
|
while (nextDirs.length) {
|
||||||
|
files = files.concat( readdirSyncRecursive( _path.join(baseDir, nextDirs.shift()) ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
return files;
|
||||||
|
};
|
||||||
|
|
||||||
|
// convert absolute paths to relative
|
||||||
|
var fileList = readdirSyncRecursive(baseDir).map(function(val){
|
||||||
|
return val.replace(baseDir + '/', '');
|
||||||
|
});
|
||||||
|
|
||||||
|
return fileList;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
/* wrench.rmdirSyncRecursive("directory_path", forceDelete, failSilent);
|
/* wrench.rmdirSyncRecursive("directory_path", forceDelete, failSilent);
|
||||||
*
|
*
|
||||||
|
|
@ -248,7 +291,7 @@ exports.LineReader = function(filename, bufferSize) {
|
||||||
this.buffer = "";
|
this.buffer = "";
|
||||||
this.fd = fs.openSync(filename, "r");
|
this.fd = fs.openSync(filename, "r");
|
||||||
this.currentPosition = 0;
|
this.currentPosition = 0;
|
||||||
}
|
};
|
||||||
|
|
||||||
exports.LineReader.prototype = {
|
exports.LineReader.prototype = {
|
||||||
getBufferAndSetCurrentPosition: function(position) {
|
getBufferAndSetCurrentPosition: function(position) {
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,9 @@ wrench.mkdirSyncRecursive(dir, 0777);
|
||||||
// Recursively delete the entire sub-tree of a directory, then kill the directory
|
// Recursively delete the entire sub-tree of a directory, then kill the directory
|
||||||
wrench.rmdirSyncRecursive('my_directory_name', failSilently);
|
wrench.rmdirSyncRecursive('my_directory_name', failSilently);
|
||||||
|
|
||||||
|
// Recursively read directories contents.
|
||||||
|
wrench.readdirSyncRecursive('my_directory_name');
|
||||||
|
|
||||||
// Recursively chmod the entire sub-tree of a directory
|
// Recursively chmod the entire sub-tree of a directory
|
||||||
wrench.chmodSyncRecursive('my_directory_name', 0755);
|
wrench.chmodSyncRecursive('my_directory_name', 0755);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ var wrench = require('wrench');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
|
||||||
module.exports = testCase({
|
module.exports = testCase({
|
||||||
testMkdirSyncRecursive: function(test) {
|
test_mkdirSyncRecursive: function(test) {
|
||||||
var dir = __dirname + '/_tmp/foo/bar';
|
var dir = __dirname + '/_tmp/foo/bar';
|
||||||
|
|
||||||
test.equals(path.existsSync(dir), false, 'Dir shouldn\'t exist - clean it up manually?');
|
test.equals(path.existsSync(dir), false, 'Dir shouldn\'t exist - clean it up manually?');
|
||||||
|
|
|
||||||
0
tests/readdir/bar.txt
Normal file
0
tests/readdir/bar.txt
Normal file
0
tests/readdir/foo/bar/ipsum.js
Normal file
0
tests/readdir/foo/bar/ipsum.js
Normal file
0
tests/readdir/foo/dolor.md
Normal file
0
tests/readdir/foo/dolor.md
Normal file
0
tests/readdir/foo/lorem.txt
Normal file
0
tests/readdir/foo/lorem.txt
Normal file
30
tests/readdirSyncRecursive.js
Normal file
30
tests/readdirSyncRecursive.js
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
var testCase = require('nodeunit').testCase;
|
||||||
|
var fs = require('fs');
|
||||||
|
var wrench = require('wrench');
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
|
module.exports = testCase({
|
||||||
|
test_readdirSyncRecursive: function(test) {
|
||||||
|
var dir = __dirname + '/readdir';
|
||||||
|
|
||||||
|
test.equals(path.existsSync(dir), true, 'Folders should exist');
|
||||||
|
|
||||||
|
var check = [
|
||||||
|
'bar.txt',
|
||||||
|
'foo',
|
||||||
|
'foo/bar',
|
||||||
|
'foo/dolor.md',
|
||||||
|
'foo/lorem.txt',
|
||||||
|
'foo/bar/ipsum.js'
|
||||||
|
];
|
||||||
|
|
||||||
|
var files = wrench.readdirSyncRecursive(dir);
|
||||||
|
|
||||||
|
test.equals(files.length, check.length, 'number of paths is correct');
|
||||||
|
test.deepEqual(files, check, 'list shows all files and folders');
|
||||||
|
|
||||||
|
test.done();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// vim: et ts=4 sw=4
|
||||||
7
tests/runner.js
Normal file
7
tests/runner.js
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
// `nodeunit tests/runner`
|
||||||
|
// will run all the tests
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
group_mkdirSyncRecursive : require('./mkdirSyncRecursive'),
|
||||||
|
group_readdirSyncRecursive : require('./readdirSyncRecursive')
|
||||||
|
};
|
||||||
Reference in a new issue