Skip to content
This repository has been archived by the owner on Jun 19, 2019. It is now read-only.

Commit

Permalink
Switching to path.join instead of concatenation
Browse files Browse the repository at this point in the history
Made path parsing friendlier across platforms
  • Loading branch information
TehShrike committed Nov 30, 2012
1 parent 81ef6ee commit db84ed7
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions lib/wrench.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,16 +138,16 @@ exports.rmdirSyncRecursive = function(path, failSilent) {

/* Loop through and delete everything in the sub-tree after checking it */
for(var i = 0; i < files.length; i++) {
var currFile = fs.lstatSync(path + "/" + files[i]);
var currFile = fs.lstatSync(_path.join(path, files[i]));

if(currFile.isDirectory()) // Recursive function back to the beginning
exports.rmdirSyncRecursive(path + "/" + files[i]);
exports.rmdirSyncRecursive(_path.join(path, files[i]));

else if(currFile.isSymbolicLink()) // Unlink symlinks
fs.unlinkSync(path + "/" + files[i]);
fs.unlinkSync(_path.join(path, files[i]));

else // Assume it's a file - perhaps a try/catch belongs here?
fs.unlinkSync(path + "/" + files[i]);
fs.unlinkSync(_path.join(path, files[i]));
}

/* Now that we know everything in the sub-tree has been deleted, we can delete the main
Expand Down Expand Up @@ -190,7 +190,7 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
if (opts.excludeHiddenUnix && /^\./.test(files[i])) continue;
}

var currFile = fs.lstatSync(sourceDir + "/" + files[i]);
var currFile = fs.lstatSync(_path.join(sourceDir, files[i]));

var fCopyFile = function(srcFile, destFile) {
if(typeof opts !== 'undefined' && opts.preserveFiles && fs.existsSync(destFile)) return;
Expand All @@ -201,25 +201,25 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {

if(currFile.isDirectory()) {
/* recursion this thing right on back. */
exports.copyDirSyncRecursive(sourceDir + "/" + files[i], newDirLocation + "/" + files[i], opts);
exports.copyDirSyncRecursive(_path.join(sourceDir, files[i]), _path.join(newDirLocation, files[i]), opts);
} else if(currFile.isSymbolicLink()) {
var symlinkFull = fs.readlinkSync(sourceDir + "/" + files[i]);
var symlinkFull = fs.readlinkSync(_path.join(sourceDir, files[i]));

if (!opts.inflateSymlinks) {
fs.symlinkSync(symlinkFull, newDirLocation + "/" + files[i]);
fs.symlinkSync(symlinkFull, _path.join(newDirLocation, files[i]));
continue;
}

var tmpCurrFile = fs.lstatSync(sourceDir + "/" + symlinkFull);
var tmpCurrFile = fs.lstatSync(_path.join(sourceDir, symlinkFull));
if (tmpCurrFile.isDirectory()) {
exports.copyDirSyncRecursive(sourceDir + "/" + symlinkFull, newDirLocation + "/" + files[i], opts);
exports.copyDirSyncRecursive(_path.join(sourceDir, symlinkFull), _path.join(newDirLocation, files[i]), opts);
} else {
/* At this point, we've hit a file actually worth copying... so copy it on over. */
fCopyFile(sourceDir + "/" + symlinkFull, newDirLocation + "/" + files[i]);
fCopyFile(_path.join(sourceDir, symlinkFull), _path.join(newDirLocation, files[i]));
}
} else {
/* At this point, we've hit a file actually worth copying... so copy it on over. */
fCopyFile(sourceDir + "/" + files[i], newDirLocation + "/" + files[i]);
fCopyFile(_path.join(sourceDir, files[i]), _path.join(newDirLocation, files[i]));
}
}
};
Expand All @@ -235,14 +235,14 @@ exports.chmodSyncRecursive = function(sourceDir, filemode) {
var files = fs.readdirSync(sourceDir);

for(var i = 0; i < files.length; i++) {
var currFile = fs.lstatSync(sourceDir + "/" + files[i]);
var currFile = fs.lstatSync(_path.join(sourceDir, files[i]));

if(currFile.isDirectory()) {
/* ...and recursion this thing right on back. */
exports.chmodSyncRecursive(sourceDir + "/" + files[i], filemode);
exports.chmodSyncRecursive(_path.join(sourceDir, files[i]), filemode);
} else {
/* At this point, we've hit a file actually worth copying... so copy it on over. */
fs.chmod(sourceDir + "/" + files[i], filemode);
fs.chmod(_path.join(sourceDir, files[i]), filemode);
}
}

Expand All @@ -262,14 +262,14 @@ exports.chownSyncRecursive = function(sourceDir, uid, gid) {
var files = fs.readdirSync(sourceDir);

for(var i = 0; i < files.length; i++) {
var currFile = fs.lstatSync(sourceDir + "/" + files[i]);
var currFile = fs.lstatSync(_path.join(sourceDir, files[i]));

if(currFile.isDirectory()) {
/* ...and recursion this thing right on back. */
exports.chownSyncRecursive(sourceDir + "/" + files[i], uid, gid);
exports.chownSyncRecursive(_path.join(sourceDir, files[i]), uid, gid);
} else {
/* At this point, we've hit a file actually worth chowning... so own it. */
fs.chownSync(sourceDir + "/" + files[i], uid, gid);
fs.chownSync(_path.join(sourceDir, files[i]), uid, gid);
}
}

Expand Down Expand Up @@ -355,15 +355,13 @@ exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, clbk) {

var mkdirSyncRecursive = function(path, mode) {
var self = this;
path = _path.normalize(path)

try {
fs.mkdirSync(path, mode);
} catch(err) {
if(err.code == "ENOENT") {
var slashIdx = path.lastIndexOf("/");
if(slashIdx < 0) {
slashIdx = path.lastIndexOf("\\");
}
var slashIdx = path.lastIndexOf(_path.sep);

if(slashIdx > 0) {
var parentPath = path.substring(0, slashIdx);
Expand Down

0 comments on commit db84ed7

Please sign in to comment.