Switching to path.join instead of concatenation

Made path parsing friendlier across platforms
This commit is contained in:
TehShrike 2012-11-30 00:37:26 +00:00
parent 81ef6ee7df
commit db84ed7dc1

View file

@ -138,16 +138,16 @@ exports.rmdirSyncRecursive = function(path, failSilent) {
/* 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.lstatSync(path + "/" + files[i]); var currFile = fs.lstatSync(_path.join(path, files[i]));
if(currFile.isDirectory()) // Recursive function back to the beginning 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 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? 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 /* Now that we know everything in the sub-tree has been deleted, we can delete the main
@ -190,7 +190,7 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
if (opts.excludeHiddenUnix && /^\./.test(files[i])) continue; 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) { var fCopyFile = function(srcFile, destFile) {
if(typeof opts !== 'undefined' && opts.preserveFiles && fs.existsSync(destFile)) return; if(typeof opts !== 'undefined' && opts.preserveFiles && fs.existsSync(destFile)) return;
@ -201,25 +201,25 @@ exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) {
if(currFile.isDirectory()) { if(currFile.isDirectory()) {
/* recursion this thing right on back. */ /* 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()) { } else if(currFile.isSymbolicLink()) {
var symlinkFull = fs.readlinkSync(sourceDir + "/" + files[i]); var symlinkFull = fs.readlinkSync(_path.join(sourceDir, files[i]));
if (!opts.inflateSymlinks) { if (!opts.inflateSymlinks) {
fs.symlinkSync(symlinkFull, newDirLocation + "/" + files[i]); fs.symlinkSync(symlinkFull, _path.join(newDirLocation, files[i]));
continue; continue;
} }
var tmpCurrFile = fs.lstatSync(sourceDir + "/" + symlinkFull); var tmpCurrFile = fs.lstatSync(_path.join(sourceDir, symlinkFull));
if (tmpCurrFile.isDirectory()) { if (tmpCurrFile.isDirectory()) {
exports.copyDirSyncRecursive(sourceDir + "/" + symlinkFull, newDirLocation + "/" + files[i], opts); exports.copyDirSyncRecursive(_path.join(sourceDir, symlinkFull), _path.join(newDirLocation, files[i]), opts);
} 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. */
fCopyFile(sourceDir + "/" + symlinkFull, newDirLocation + "/" + files[i]); fCopyFile(_path.join(sourceDir, symlinkFull), _path.join(newDirLocation, files[i]));
} }
} 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. */
fCopyFile(sourceDir + "/" + files[i], newDirLocation + "/" + files[i]); fCopyFile(_path.join(sourceDir, files[i]), _path.join(newDirLocation, files[i]));
} }
} }
}; };
@ -235,14 +235,14 @@ 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.lstatSync(sourceDir + "/" + files[i]); var currFile = fs.lstatSync(_path.join(sourceDir, files[i]));
if(currFile.isDirectory()) { if(currFile.isDirectory()) {
/* ...and recursion this thing right on back. */ /* ...and recursion this thing right on back. */
exports.chmodSyncRecursive(sourceDir + "/" + files[i], filemode); exports.chmodSyncRecursive(_path.join(sourceDir, files[i]), 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(_path.join(sourceDir, files[i]), filemode);
} }
} }
@ -262,14 +262,14 @@ 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.lstatSync(sourceDir + "/" + files[i]); var currFile = fs.lstatSync(_path.join(sourceDir, files[i]));
if(currFile.isDirectory()) { if(currFile.isDirectory()) {
/* ...and recursion this thing right on back. */ /* ...and recursion this thing right on back. */
exports.chownSyncRecursive(sourceDir + "/" + files[i], uid, gid); exports.chownSyncRecursive(_path.join(sourceDir, files[i]), 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(_path.join(sourceDir, files[i]), uid, gid);
} }
} }
@ -355,15 +355,13 @@ exports.copyDirRecursive = function copyDirRecursive(srcDir, newDir, clbk) {
var mkdirSyncRecursive = function(path, mode) { var mkdirSyncRecursive = function(path, mode) {
var self = this; var self = this;
path = _path.normalize(path)
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(_path.sep);
if(slashIdx < 0) {
slashIdx = path.lastIndexOf("\\");
}
if(slashIdx > 0) { if(slashIdx > 0) {
var parentPath = path.substring(0, slashIdx); var parentPath = path.substring(0, slashIdx);