Recursive file operations in Node.js
This repository has been archived on 2026-03-31. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Find a file
Victor Jonsson 061c2e0387 Update lib/wrench.js
Code crashes on this line if opts is undefined. I'm guessing that 
the default behavior should be that symlinked files gets copied as
new files unless the option inflateSymlinks is set to false.
2012-11-19 15:49:07 +01:00
lib Update lib/wrench.js 2012-11-19 15:49:07 +01:00
tests added null check so copyDirSyncRecursive will work without passing opts 2012-11-15 12:25:11 +01:00
.gitignore Add npm-debug.log to .gitignore. 2012-05-14 19:14:16 -04:00
LICENSE Fix year... 2010-10-22 12:43:32 -04:00
package.json Version bump 2012-11-16 04:43:20 -05:00
readme.md Fixes issue #32 2012-11-09 05:20:15 -05:00

wrench.js - Recursive file operations in Node.js

While I love Node.js, I've found myself missing some functions. Things like recursively deleting/chmodding a directory (or even deep copying a directory), or even a basic line reader, shouldn't need to be re-invented time and time again.

That said, here's my attempt at a re-usable solution, at least until something more formalized gets integrated into Node.js (hint hint). wrench.js is fairly simple to use - check out the documentation/examples below:

Installation

npm install wrench

Usage

var wrench = require('wrench'),
	util = require('util');

Synchronous operations

// Recursively create directories, sub-trees and all.
wrench.mkdirSyncRecursive(dir, 0777);

// Recursively delete the entire sub-tree of a directory, then kill the directory
wrench.rmdirSyncRecursive('my_directory_name', failSilently);

// Recursively read directories contents.
wrench.readdirSyncRecursive('my_directory_name');

// Recursively chmod the entire sub-tree of a directory
wrench.chmodSyncRecursive('my_directory_name', 0755);

// Recursively chown the entire sub-tree of a directory
wrench.chownSyncRecursive("directory", uid, gid);

// Deep-copy an existing directory
wrench.copyDirSyncRecursive('directory_to_copy', 'location_where_copy_should_end_up');

// Read lines in from a file until you hit the end
var f = new wrench.LineReader('x.txt');
while(f.hasNextLine()) {
	util.puts(x.getNextLine());
}

// Note: You will need to close that above line reader at some point, otherwise
// you will run into a "too many open files" error. f.close() or fs.closeSync(f.fd) are
// your friends, as only you know when it is safe to close.

Asynchronous operations

// Recursively read directories contents
var files = [];
wrench.readdirRecursive('my_directory_name', function(error, curFiles) {
    // curFiles is what you want
});

Questions, comments? Hit me up. (ryan [at] venodesigns.net | http://twitter.com/ryanmcgrath)