bug fixing

This commit is contained in:
defrex 2011-04-28 18:42:02 -04:00
parent e9991ed62c
commit 5b71715d88

View file

@ -1,5 +1,5 @@
/* wrench.js /* wrench.js
* *
* A collection of various utility functions I've found myself in need of * A collection of various utility functions I've found myself in need of
* for use with Node.js (http://nodejs.org/). This includes things like: * for use with Node.js (http://nodejs.org/). This includes things like:
* *
@ -16,7 +16,7 @@
var fs = require("fs"); var fs = require("fs");
/* wrench.rmdirSyncRecursive("directory_path"); /* wrench.rmdirSyncRecursive("directory_path");
* *
* Recursively dives through directories and obliterates everything about it. This is a * Recursively dives through directories and obliterates everything about it. This is a
* Sync-function, which blocks things until it's done. No idea why anybody would want an * Sync-function, which blocks things until it's done. No idea why anybody would want an
* Asynchronous version. :\ * Asynchronous version. :\
@ -28,7 +28,7 @@ exports.rmdirSyncRecursive = function(path) {
/* 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++) {
var currFile = fs.statSync(currDir + "/" + files[i]); var currFile = fs.statSync(currDir + "/" + files[i]);
if(currFile.isDirectory()) // Recursive function back to the beginning if(currFile.isDirectory()) // Recursive function back to the beginning
exports.rmdirSyncRecursive(currDir + "/" + files[i]); exports.rmdirSyncRecursive(currDir + "/" + files[i]);
@ -115,78 +115,73 @@ exports.chmodSyncRecursive = function(sourceDir, filemode) {
/* wrench.rmdirRecursive("directory_path", callback); /* wrench.rmdirRecursive("directory_path", callback);
* *
* Recursively dives through directories and obliterates everything about it. * Recursively dives through directories and obliterates everything about it.
*/ */
exports.rmdirRecursive = function rmdirRecursive(dir, clbk){ exports.rmdirRecursive = function rmdirRecursive(dir, clbk){
fs.readdir(dir, function(err, files){ fs.readdir(dir, function(err, files){
if (err) return clbk(); if (err) return clbk(err);
var called = 0; (function rmFile(err){
var done = function(){ if (err) return clbk(err);
if (called == files.length)
fs.rmdir(dir, clbk); var filename = files.shift();
else if (filename === null || typeof filename == 'undefined')
called++; return fs.rmdir(dir, clbk);
};
done(); var file = dir+'/'+filename;
files.forEach(function(f){ fs.stat(file, function(err, stat){
f = dir+f; if (err) return clbk(err);
fs.stat(f, function(err, stat){ if (stat.isDirectory())
if (err) return done(); rmdirRecursive(file, rmFile);
if (stat.isDirectory()) else
rmdirRecursive(f, done); fs.unlink(file, rmFile);
else
fs.unlink(f, done);
}); });
}); })();
}); });
}; };
/* wrench.copyDirRecursive("directory_to_copy", "new_location", callback); /* wrench.copyDirRecursive("directory_to_copy", "new_location", callback);
* *
* Recursively dives through a directory and moves all its files to a new * Recursively dives through a directory and moves all its files to a new
* location. * location.
* *
* 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.copyDirRecursive = function copyDirRecursive(srcDir, newDir, clbk) { exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, clbk) {
fs.stat(newDir, function(err, newDirStat){ fs.stat(newDir, function(err, newDirStat){
if (!err) return exports.rmdirRecursive(newDir, function(){ if (!err) return exports.rmdirRecursive(newDir, function(err){
copyDirRecursive(srcDir, newDir, clbk); copyDirRecursive(srcDir, newDir, clbk);
}); });
fs.stat(srcDir, function(err, srcDirStat){ fs.stat(srcDir, function(err, srcDirStat){
if (err) return clbk(err); if (err) return clbk(err);
fs.mkdir(newDir, srcDirStat.mode, function(err){ fs.mkdir(newDir, srcDirStat.mode, function(err){
if (err) return clbk(err); if (err) return clbk(err);
fs.readdir(srcDir, function(err, files){ fs.readdir(srcDir, function(err, files){
if (err) return clbk(err); if (err) return clbk(err);
var called = 0; (function copyFiles(err){
var done = function(err){
if (err) return clbk(err); if (err) return clbk(err);
if (called == files.length)
clbk(); var filename = files.shift();
else if (filename === null || typeof filename == 'undefined')
called++; return clbk();
};
done();
files.forEach(function(filename){
var file = srcDir+'/'+filename, var file = srcDir+'/'+filename,
newFile = newDir+'/'+filename, newFile = newDir+'/'+filename;
called = 0;
fs.stat(file, function(err, fileStat){ fs.stat(file, function(err, fileStat){
if (fileStat.isDirectory()) if (fileStat.isDirectory())
copyDirRecursive(file, newFile, done); copyDirRecursive(file, newFile, copyFiles);
else if (fileStat.isSymbolicLink()) else if (fileStat.isSymbolicLink())
fs.readlink(file, function(err, link){ fs.readlink(file, function(err, link){
fs.symlink(link, newFile, done); fs.symlink(link, newFile, copyFiles);
}); });
else else
fs.readFile(file, function(err, data){ fs.readFile(file, function(err, data){
fs.writeFile(newFile, data, done); fs.writeFile(newFile, data, copyFiles);
}); });
}); });
}); })();
}); });
}); });
}); });