add readdirSyncRecursive, normalize tabs, add tests/runner #11

Merged
millermedeiros merged 2 commits from master into master 2012-01-14 12:13:08 -08:00
Showing only changes of commit 29cb04590b - Show all commits

View file

@ -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