From 29cb04590b2b77d14eb9f3a6c5ebf4d740f06804 Mon Sep 17 00:00:00 2001 From: millermedeiros Date: Sat, 14 Jan 2012 13:05:01 -0200 Subject: [PATCH 1/2] normalize tabs --- lib/wrench.js | 126 +++++++++++++++++++++++++------------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/lib/wrench.js b/lib/wrench.js index d499b1f..877c2a6 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -20,14 +20,14 @@ var fs = require("fs"); * Asynchronous version. :\ */ exports.rmdirSyncRecursive = function(path, failSilent) { - var files; - - try { - files = fs.readdirSync(path); + var files; + + try { + files = fs.readdirSync(path); } catch (err) { - if(failSilent) return; - throw new Error(err.message); - } + if(failSilent) return; + throw new Error(err.message); + } /* Loop through and delete everything in the sub-tree after checking it */ for(var i = 0; i < files.length; i++) { @@ -57,7 +57,7 @@ exports.rmdirSyncRecursive = function(path, failSilent) { * Note: Directories should be passed to this function without a trailing slash. */ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation) { - /* Copying over something is... tricky. The user should know what they're doing at this point, so... + /* Copying over something is... tricky. The user should know what they're doing at this point, so... * blow any existing directory away! */ try { @@ -65,13 +65,13 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation) { } catch(e) { } /* Create the directory where all our junk is moving to; read the mode of the source directory and mirror it */ - var checkDir = fs.statSync(sourceDir); + var checkDir = fs.statSync(sourceDir); fs.mkdirSync(newDirLocation, checkDir.mode); var files = fs.readdirSync(sourceDir); - for(var i = 0; i < files.length; i++) { - var currFile = fs.statSync(sourceDir + "/" + files[i]); + for(var i = 0; i < files.length; i++) { + var currFile = fs.statSync(sourceDir + "/" + files[i]); if(currFile.isDirectory()) { /* Create a new directory in our copied version... */ @@ -85,8 +85,8 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation) { } else { /* At this point, we've hit a file actually worth copying... so copy it on over. */ var contents = fs.readFileSync(sourceDir + "/" + files[i]); - fs.writeFileSync(newDirLocation + "/" + files[i], contents); - } + fs.writeFileSync(newDirLocation + "/" + files[i], contents); + } } }; @@ -100,8 +100,8 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation) { exports.chmodSyncRecursive = function(sourceDir, filemode) { var files = fs.readdirSync(sourceDir); - for(var i = 0; i < files.length; i++) { - var currFile = fs.statSync(sourceDir + "/" + files[i]); + for(var i = 0; i < files.length; i++) { + var currFile = fs.statSync(sourceDir + "/" + files[i]); if(currFile.isDirectory()) { /* ...and recursion this thing right on back. */ @@ -109,7 +109,7 @@ exports.chmodSyncRecursive = function(sourceDir, filemode) { } else { /* At this point, we've hit a file actually worth copying... so copy it on over. */ fs.chmod(sourceDir + "/" + files[i], filemode); - } + } } /* Finally, chmod the parent directory */ @@ -127,8 +127,8 @@ exports.chmodSyncRecursive = function(sourceDir, filemode) { exports.chownSyncRecursive = function(sourceDir, uid, gid) { var files = fs.readdirSync(sourceDir); - for(var i = 0; i < files.length; i++) { - var currFile = fs.statSync(sourceDir + "/" + files[i]); + for(var i = 0; i < files.length; i++) { + var currFile = fs.statSync(sourceDir + "/" + files[i]); if(currFile.isDirectory()) { /* ...and recursion this thing right on back. */ @@ -136,7 +136,7 @@ exports.chownSyncRecursive = function(sourceDir, uid, gid) { } else { /* At this point, we've hit a file actually worth chowning... so own it. */ fs.chownSync(sourceDir + "/" + files[i], uid, gid); - } + } } /* Finally, chown the parent directory */ @@ -220,64 +220,64 @@ exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, clbk) { }; var mkdirSyncRecursive = function(path, mode) { - var self = this; + var self = this; - try { - fs.mkdirSync(path, mode); - } catch(err) { - if(err.code == "ENOENT") { - var slashIdx = path.lastIndexOf("/"); - if(slashIdx > 0) { - var parentPath = path.substring(0, slashIdx); - mkdirSyncRecursive(parentPath, mode); - mkdirSyncRecursive(path, mode); - } else { - throw err; - } - } else if(err.code == "EEXIST") { - return; - } else { - throw err; - } - } + try { + fs.mkdirSync(path, mode); + } catch(err) { + if(err.code == "ENOENT") { + var slashIdx = path.lastIndexOf("/"); + if(slashIdx > 0) { + var parentPath = path.substring(0, slashIdx); + mkdirSyncRecursive(parentPath, mode); + mkdirSyncRecursive(path, mode); + } else { + throw err; + } + } else if(err.code == "EEXIST") { + return; + } else { + throw err; + } + } }; exports.mkdirSyncRecursive = mkdirSyncRecursive; exports.LineReader = function(filename, bufferSize) { - this.bufferSize = bufferSize || 8192; - this.buffer = ""; - this.fd = fs.openSync(filename, "r"); - this.currentPosition = 0; + this.bufferSize = bufferSize || 8192; + this.buffer = ""; + this.fd = fs.openSync(filename, "r"); + this.currentPosition = 0; } exports.LineReader.prototype = { - getBufferAndSetCurrentPosition: function(position) { - var res = fs.readSync(this.fd, this.bufferSize, position, "ascii"); + getBufferAndSetCurrentPosition: function(position) { + var res = fs.readSync(this.fd, this.bufferSize, position, "ascii"); - this.buffer += res[0]; - if(res[1] === 0) return -1; + this.buffer += res[0]; + if(res[1] === 0) return -1; - this.currentPosition = position + res[1]; - return this.currentPosition; - }, + this.currentPosition = position + res[1]; + return this.currentPosition; + }, - hasNextLine: function() { - while(this.buffer.indexOf('\n') === -1) { - this.getBufferAndSetCurrentPosition(this.currentPosition); - if(this.currentPosition === -1) return false; - } + hasNextLine: function() { + while(this.buffer.indexOf('\n') === -1) { + this.getBufferAndSetCurrentPosition(this.currentPosition); + if(this.currentPosition === -1) return false; + } - if(this.buffer.indexOf("\n") > -1) return true; - return false; - }, + if(this.buffer.indexOf("\n") > -1) return true; + return false; + }, - getNextLine: function() { - var lineEnd = this.buffer.indexOf("\n"), - result = this.buffer.substring(0, lineEnd); + getNextLine: function() { + var lineEnd = this.buffer.indexOf("\n"), + result = this.buffer.substring(0, lineEnd); - this.buffer = this.buffer.substring(result.length + 1, this.buffer.length); - return result; - } + this.buffer = this.buffer.substring(result.length + 1, this.buffer.length); + return result; + } }; // vim: et ts=4 sw=4 -- 2.39.5 From 139ed486f13235417718ebd2e7c6582b665b6cf5 Mon Sep 17 00:00:00 2001 From: millermedeiros Date: Sat, 14 Jan 2012 14:03:40 -0200 Subject: [PATCH 2/2] add readdirSyncRecursive and the test runner file. --- lib/wrench.js | 47 ++++++++++++++++++++++++++++++++-- readme.md | 3 +++ tests/mkdirSyncRecursive.js | 2 +- tests/readdir/bar.txt | 0 tests/readdir/foo/bar/ipsum.js | 0 tests/readdir/foo/dolor.md | 0 tests/readdir/foo/lorem.txt | 0 tests/readdirSyncRecursive.js | 30 ++++++++++++++++++++++ tests/runner.js | 7 +++++ 9 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 tests/readdir/bar.txt create mode 100644 tests/readdir/foo/bar/ipsum.js create mode 100644 tests/readdir/foo/dolor.md create mode 100644 tests/readdir/foo/lorem.txt create mode 100644 tests/readdirSyncRecursive.js create mode 100644 tests/runner.js diff --git a/lib/wrench.js b/lib/wrench.js index 877c2a6..c54e63f 100644 --- a/lib/wrench.js +++ b/lib/wrench.js @@ -11,7 +11,50 @@ * ~ 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); * @@ -248,7 +291,7 @@ exports.LineReader = function(filename, bufferSize) { this.buffer = ""; this.fd = fs.openSync(filename, "r"); this.currentPosition = 0; -} +}; exports.LineReader.prototype = { getBufferAndSetCurrentPosition: function(position) { diff --git a/readme.md b/readme.md index e59ec82..e821b19 100644 --- a/readme.md +++ b/readme.md @@ -25,6 +25,9 @@ wrench.mkdirSyncRecursive(dir, 0777); // Recursively delete the entire sub-tree of a directory, then kill the directory wrench.rmdirSyncRecursive('my_directory_name', failSilently); +// Recursively read directories contents. +wrench.readdirSyncRecursive('my_directory_name'); + // Recursively chmod the entire sub-tree of a directory wrench.chmodSyncRecursive('my_directory_name', 0755); diff --git a/tests/mkdirSyncRecursive.js b/tests/mkdirSyncRecursive.js index 8136495..361d892 100644 --- a/tests/mkdirSyncRecursive.js +++ b/tests/mkdirSyncRecursive.js @@ -4,7 +4,7 @@ var wrench = require('wrench'); var path = require('path'); module.exports = testCase({ - testMkdirSyncRecursive: function(test) { + test_mkdirSyncRecursive: function(test) { var dir = __dirname + '/_tmp/foo/bar'; test.equals(path.existsSync(dir), false, 'Dir shouldn\'t exist - clean it up manually?'); diff --git a/tests/readdir/bar.txt b/tests/readdir/bar.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/readdir/foo/bar/ipsum.js b/tests/readdir/foo/bar/ipsum.js new file mode 100644 index 0000000..e69de29 diff --git a/tests/readdir/foo/dolor.md b/tests/readdir/foo/dolor.md new file mode 100644 index 0000000..e69de29 diff --git a/tests/readdir/foo/lorem.txt b/tests/readdir/foo/lorem.txt new file mode 100644 index 0000000..e69de29 diff --git a/tests/readdirSyncRecursive.js b/tests/readdirSyncRecursive.js new file mode 100644 index 0000000..6e862f2 --- /dev/null +++ b/tests/readdirSyncRecursive.js @@ -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 diff --git a/tests/runner.js b/tests/runner.js new file mode 100644 index 0000000..b353d1f --- /dev/null +++ b/tests/runner.js @@ -0,0 +1,7 @@ +// `nodeunit tests/runner` +// will run all the tests + +module.exports = { + group_mkdirSyncRecursive : require('./mkdirSyncRecursive'), + group_readdirSyncRecursive : require('./readdirSyncRecursive') +}; -- 2.39.5