normalize tabs

This commit is contained in:
millermedeiros 2012-01-14 13:05:01 -02:00
parent 5a6372fe46
commit 29cb04590b

View file

@ -20,14 +20,14 @@ var fs = require("fs");
* Asynchronous version. :\ * Asynchronous version. :\
*/ */
exports.rmdirSyncRecursive = function(path, failSilent) { exports.rmdirSyncRecursive = function(path, failSilent) {
var files; var files;
try { try {
files = fs.readdirSync(path); files = fs.readdirSync(path);
} catch (err) { } catch (err) {
if(failSilent) return; if(failSilent) return;
throw new Error(err.message); throw new Error(err.message);
} }
/* Loop through and delete everything in the sub-tree after checking it */ /* Loop through and delete everything in the sub-tree after checking it */
for(var i = 0; i < files.length; i++) { 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. * Note: Directories should be passed to this function without a trailing slash.
*/ */
exports.copyDirSyncRecursive = function(sourceDir, newDirLocation) { 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! * blow any existing directory away!
*/ */
try { try {
@ -65,13 +65,13 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation) {
} catch(e) { } } catch(e) { }
/* Create the directory where all our junk is moving to; read the mode of the source directory and mirror it */ /* 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); fs.mkdirSync(newDirLocation, checkDir.mode);
var files = fs.readdirSync(sourceDir); var files = fs.readdirSync(sourceDir);
for(var i = 0; i < files.length; i++) { for(var i = 0; i < files.length; i++) {
var currFile = fs.statSync(sourceDir + "/" + files[i]); var currFile = fs.statSync(sourceDir + "/" + files[i]);
if(currFile.isDirectory()) { if(currFile.isDirectory()) {
/* Create a new directory in our copied version... */ /* Create a new directory in our copied version... */
@ -85,8 +85,8 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation) {
} else { } else {
/* At this point, we've hit a file actually worth copying... so copy it on over. */ /* At this point, we've hit a file actually worth copying... so copy it on over. */
var contents = fs.readFileSync(sourceDir + "/" + files[i]); 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) { exports.chmodSyncRecursive = function(sourceDir, filemode) {
var files = fs.readdirSync(sourceDir); var files = fs.readdirSync(sourceDir);
for(var i = 0; i < files.length; i++) { for(var i = 0; i < files.length; i++) {
var currFile = fs.statSync(sourceDir + "/" + files[i]); var currFile = fs.statSync(sourceDir + "/" + files[i]);
if(currFile.isDirectory()) { if(currFile.isDirectory()) {
/* ...and recursion this thing right on back. */ /* ...and recursion this thing right on back. */
@ -109,7 +109,7 @@ exports.chmodSyncRecursive = function(sourceDir, filemode) {
} else { } else {
/* At this point, we've hit a file actually worth copying... so copy it on over. */ /* At this point, we've hit a file actually worth copying... so copy it on over. */
fs.chmod(sourceDir + "/" + files[i], filemode); fs.chmod(sourceDir + "/" + files[i], filemode);
} }
} }
/* Finally, chmod the parent directory */ /* Finally, chmod the parent directory */
@ -127,8 +127,8 @@ exports.chmodSyncRecursive = function(sourceDir, filemode) {
exports.chownSyncRecursive = function(sourceDir, uid, gid) { exports.chownSyncRecursive = function(sourceDir, uid, gid) {
var files = fs.readdirSync(sourceDir); var files = fs.readdirSync(sourceDir);
for(var i = 0; i < files.length; i++) { for(var i = 0; i < files.length; i++) {
var currFile = fs.statSync(sourceDir + "/" + files[i]); var currFile = fs.statSync(sourceDir + "/" + files[i]);
if(currFile.isDirectory()) { if(currFile.isDirectory()) {
/* ...and recursion this thing right on back. */ /* ...and recursion this thing right on back. */
@ -136,7 +136,7 @@ exports.chownSyncRecursive = function(sourceDir, uid, gid) {
} else { } else {
/* At this point, we've hit a file actually worth chowning... so own it. */ /* At this point, we've hit a file actually worth chowning... so own it. */
fs.chownSync(sourceDir + "/" + files[i], uid, gid); fs.chownSync(sourceDir + "/" + files[i], uid, gid);
} }
} }
/* Finally, chown the parent directory */ /* Finally, chown the parent directory */
@ -220,64 +220,64 @@ exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, clbk) {
}; };
var mkdirSyncRecursive = function(path, mode) { var mkdirSyncRecursive = function(path, mode) {
var self = this; var self = this;
try { try {
fs.mkdirSync(path, mode); fs.mkdirSync(path, mode);
} catch(err) { } catch(err) {
if(err.code == "ENOENT") { if(err.code == "ENOENT") {
var slashIdx = path.lastIndexOf("/"); var slashIdx = path.lastIndexOf("/");
if(slashIdx > 0) { if(slashIdx > 0) {
var parentPath = path.substring(0, slashIdx); var parentPath = path.substring(0, slashIdx);
mkdirSyncRecursive(parentPath, mode); mkdirSyncRecursive(parentPath, mode);
mkdirSyncRecursive(path, mode); mkdirSyncRecursive(path, mode);
} else { } else {
throw err; throw err;
} }
} else if(err.code == "EEXIST") { } else if(err.code == "EEXIST") {
return; return;
} else { } else {
throw err; throw err;
} }
} }
}; };
exports.mkdirSyncRecursive = mkdirSyncRecursive; exports.mkdirSyncRecursive = mkdirSyncRecursive;
exports.LineReader = function(filename, bufferSize) { exports.LineReader = function(filename, bufferSize) {
this.bufferSize = bufferSize || 8192; this.bufferSize = bufferSize || 8192;
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) {
var res = fs.readSync(this.fd, this.bufferSize, position, "ascii"); var res = fs.readSync(this.fd, this.bufferSize, position, "ascii");
this.buffer += res[0]; this.buffer += res[0];
if(res[1] === 0) return -1; if(res[1] === 0) return -1;
this.currentPosition = position + res[1]; this.currentPosition = position + res[1];
return this.currentPosition; return this.currentPosition;
}, },
hasNextLine: function() { hasNextLine: function() {
while(this.buffer.indexOf('\n') === -1) { while(this.buffer.indexOf('\n') === -1) {
this.getBufferAndSetCurrentPosition(this.currentPosition); this.getBufferAndSetCurrentPosition(this.currentPosition);
if(this.currentPosition === -1) return false; if(this.currentPosition === -1) return false;
} }
if(this.buffer.indexOf("\n") > -1) return true; if(this.buffer.indexOf("\n") > -1) return true;
return false; return false;
}, },
getNextLine: function() { getNextLine: function() {
var lineEnd = this.buffer.indexOf("\n"), var lineEnd = this.buffer.indexOf("\n"),
result = this.buffer.substring(0, lineEnd); result = this.buffer.substring(0, lineEnd);
this.buffer = this.buffer.substring(result.length + 1, this.buffer.length); this.buffer = this.buffer.substring(result.length + 1, this.buffer.length);
return result; return result;
} }
}; };
// vim: et ts=4 sw=4 // vim: et ts=4 sw=4