Comparing version 1.3.3 to 1.3.4
@@ -14,4 +14,47 @@ /* wrench.js | ||
var fs = require("fs"); | ||
var fs = require("fs"), | ||
_path = require("path"); | ||
/* wrench.readdirSyncRecursive("directory_path"); | ||
* | ||
* Recursively dives through directories and read the contents of all the | ||
* children directories. | ||
*/ | ||
exports.readdirSyncRecursive = function(baseDir) { | ||
baseDir = baseDir.replace(/\/$/, ''); | ||
var readdirSyncRecursive = function(baseDir) { | ||
var files = [], | ||
curFiles, | ||
nextDirs, | ||
isDir = function(fname){ | ||
return fs.statSync( _path.join(baseDir, fname) ).isDirectory(); | ||
}, | ||
prependBaseDir = function(fname){ | ||
return _path.join(baseDir, fname); | ||
}; | ||
curFiles = fs.readdirSync(baseDir); | ||
nextDirs = curFiles.filter(isDir); | ||
curFiles = curFiles.map(prependBaseDir); | ||
files = files.concat( curFiles ); | ||
while (nextDirs.length) { | ||
files = files.concat( readdirSyncRecursive( _path.join(baseDir, nextDirs.shift()) ) ); | ||
} | ||
return files; | ||
}; | ||
// convert absolute paths to relative | ||
var fileList = readdirSyncRecursive(baseDir).map(function(val){ | ||
return val.replace(baseDir + '/', ''); | ||
}); | ||
return fileList; | ||
}; | ||
/* wrench.rmdirSyncRecursive("directory_path", forceDelete, failSilent); | ||
@@ -24,14 +67,14 @@ * | ||
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++) { | ||
var currFile = fs.statSync(path + "/" + files[i]); | ||
var currFile = fs.lstatSync(path + "/" + files[i]); | ||
@@ -62,3 +105,3 @@ if(currFile.isDirectory()) // Recursive function back to the beginning | ||
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! | ||
@@ -71,3 +114,3 @@ */ | ||
/* 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); | ||
@@ -77,4 +120,4 @@ | ||
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]); | ||
@@ -93,4 +136,4 @@ if(currFile.isDirectory()) { | ||
var contents = fs.readFileSync(sourceDir + "/" + files[i]); | ||
fs.writeFileSync(newDirLocation + "/" + files[i], contents); | ||
} | ||
fs.writeFileSync(newDirLocation + "/" + files[i], contents); | ||
} | ||
} | ||
@@ -109,4 +152,4 @@ }; | ||
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]); | ||
@@ -119,3 +162,3 @@ if(currFile.isDirectory()) { | ||
fs.chmod(sourceDir + "/" + files[i], filemode); | ||
} | ||
} | ||
} | ||
@@ -138,4 +181,4 @@ | ||
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]); | ||
@@ -148,3 +191,3 @@ if(currFile.isDirectory()) { | ||
fs.chownSync(sourceDir + "/" + files[i], uid, gid); | ||
} | ||
} | ||
} | ||
@@ -233,22 +276,22 @@ | ||
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; | ||
} | ||
} | ||
}; | ||
@@ -258,38 +301,38 @@ 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 |
{ | ||
"name": "wrench", | ||
"description": "Recursive filesystem (and other) operations that Node *should* have.", | ||
"version": "1.3.3", | ||
"version": "1.3.4", | ||
"author": "Ryan McGrath <ryan@venodesigns.net>", | ||
@@ -6,0 +6,0 @@ |
@@ -28,2 +28,5 @@ wrench.js - Recursive file operations in Node.js | ||
// Recursively read directories contents. | ||
wrench.readdirSyncRecursive('my_directory_name'); | ||
// Recursively chmod the entire sub-tree of a directory | ||
@@ -30,0 +33,0 @@ wrench.chmodSyncRecursive('my_directory_name', 0755); |
@@ -7,3 +7,3 @@ var testCase = require('nodeunit').testCase; | ||
module.exports = testCase({ | ||
testMkdirSyncRecursive: function(test) { | ||
test_mkdirSyncRecursive: function(test) { | ||
var dir = __dirname + '/_tmp/foo/bar'; | ||
@@ -10,0 +10,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16769
12
321
49
3