Comparing version 1.3.5 to 1.3.6
@@ -58,3 +58,67 @@ /* wrench.js | ||
/* wrench.readdirRecursive("directory_path", function(error, files) {}); | ||
* | ||
* Recursively dives through directories and read the contents of all the | ||
* children directories. | ||
* | ||
* Asynchronous, so returns results/error in callback. | ||
* Callback receives the of files in currently recursed directory. | ||
* When no more directories are left, callback is called with null for all arguments. | ||
* | ||
*/ | ||
exports.readdirRecursive = function(baseDir, fn) { | ||
baseDir = baseDir.replace(/\/$/, ''); | ||
var waitCount = 0; | ||
function readdirRecursive(curDir) { | ||
var files = [], | ||
curFiles, | ||
nextDirs, | ||
prependcurDir = function(fname){ | ||
return _path.join(curDir, fname); | ||
}; | ||
waitCount++; | ||
fs.readdir(curDir, function(e, curFiles) { | ||
waitCount--; | ||
curFiles = curFiles.map(prependcurDir); | ||
curFiles.forEach(function(it) { | ||
waitCount++; | ||
fs.stat(it, function(e, stat) { | ||
waitCount--; | ||
if (e) { | ||
fn(e); | ||
} else { | ||
if (stat.isDirectory()) { | ||
readdirRecursive(it); | ||
} | ||
} | ||
if (waitCount == 0) { | ||
fn(null, null); | ||
} | ||
}); | ||
}); | ||
fn(null, curFiles.map(function(val) { | ||
// convert absolute paths to relative | ||
return val.replace(baseDir + '/', ''); | ||
})); | ||
if (waitCount == 0) { | ||
fn(null, null); | ||
} | ||
}); | ||
}; | ||
readdirRecursive(baseDir); | ||
}; | ||
/* wrench.rmdirSyncRecursive("directory_path", forceDelete, failSilent); | ||
@@ -103,13 +167,18 @@ * | ||
*/ | ||
exports.copyDirSyncRecursive = function(sourceDir, newDirLocation) { | ||
/* Copying over something is... tricky. The user should know what they're doing at this point, so... | ||
* blow any existing directory away! | ||
*/ | ||
try { | ||
if(fs.statSync(newDirLocation).isDirectory()) exports.rmdirSyncRecursive(newDirLocation); | ||
} catch(e) { } | ||
exports.copyDirSyncRecursive = function(sourceDir, newDirLocation, opts) { | ||
if (!opts || !opts.preserve) { | ||
try { | ||
if(fs.statSync(newDirLocation).isDirectory()) exports.rmdirSyncRecursive(newDirLocation); | ||
} catch(e) { } | ||
} | ||
/* 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); | ||
fs.mkdirSync(newDirLocation, checkDir.mode); | ||
try { | ||
fs.mkdirSync(newDirLocation, checkDir.mode); | ||
} catch (e) { | ||
//if the directory already exists, that's okay | ||
if (e.code !== 'EEXIST') throw e; | ||
} | ||
@@ -123,3 +192,3 @@ var files = fs.readdirSync(sourceDir); | ||
/* recursion this thing right on back. */ | ||
exports.copyDirSyncRecursive(sourceDir + "/" + files[i], newDirLocation + "/" + files[i]); | ||
exports.copyDirSyncRecursive(sourceDir + "/" + files[i], newDirLocation + "/" + files[i], opts); | ||
} else if(currFile.isSymbolicLink()) { | ||
@@ -273,2 +342,6 @@ var symlinkFull = fs.readlinkSync(sourceDir + "/" + files[i]); | ||
var slashIdx = path.lastIndexOf("/"); | ||
if(slashIdx < 0) { | ||
slashIdx = path.lastIndexOf("\\"); | ||
} | ||
if(slashIdx > 0) { | ||
@@ -275,0 +348,0 @@ var parentPath = path.substring(0, slashIdx); |
{ | ||
"name": "wrench", | ||
"description": "Recursive filesystem (and other) operations that Node *should* have.", | ||
"version": "1.3.5", | ||
"version": "1.3.6", | ||
"author": "Ryan McGrath <ryan@venodesigns.net>", | ||
@@ -22,2 +22,10 @@ | ||
"dependencies": { | ||
}, | ||
"devDependencies": { | ||
"nodeunit": ">= 0.6.4", | ||
"underscore": ">= 1.3.1" | ||
}, | ||
"main": "./lib/wrench", | ||
@@ -28,2 +36,4 @@ | ||
}, | ||
"scripts": { "test": "./node_modules/nodeunit/bin/nodeunit tests/runner.js" }, | ||
@@ -30,0 +40,0 @@ "licenses": [{ |
@@ -21,3 +21,6 @@ wrench.js - Recursive file operations in Node.js | ||
util = require('util'); | ||
``` | ||
### Synchronous operations | ||
``` javascript | ||
// Recursively create directories, sub-trees and all. | ||
@@ -47,4 +50,17 @@ wrench.mkdirSyncRecursive(dir, 0777); | ||
``` | ||
It should be noted that these are all currently synchronous operations. | ||
### Asynchronous operations | ||
``` javascript | ||
// Recursively read directories contents | ||
var files = []; | ||
wrench.readdirRecursive('my_directory_name', function(error, curFiles) { | ||
if (files) { | ||
files = files.concat(curFiles); | ||
} else { | ||
// files list contains all the directory contents now | ||
} | ||
}); | ||
``` | ||
Questions, comments? Hit me up. (ryan [at] venodesigns.net | http://twitter.com/ryanmcgrath) |
@@ -5,4 +5,4 @@ // `nodeunit tests/runner` | ||
module.exports = { | ||
group_mkdirSyncRecursive : require('./mkdirSyncRecursive'), | ||
group_readdirSyncRecursive : require('./readdirSyncRecursive') | ||
group_mkdir: require('./mkdir'), | ||
group_readdir: require('./readdir') | ||
}; |
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
19513
13
396
65
2