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
Ryan McGrath a31e8029cd Merge pull request #14 from cbou/master
- Package.json now contains depedencies.
- Tests do not use test.deepEqual() anymore because with this method, the order has to be the same, which is not always the case.
2012-02-03 16:24:14 -08:00
lib Replaced a few more statSync references with lstatSync to ensure symbolic links to directories aren't detected as directories which causes errors. Also in copyDirSyncRecursive removed an explict call to mkDirSync before recusively calling to copy the sub directory as the recursive method is already creating that directory. The explicit call to create the sub directory beforehand is causing the directory to be created twice causing an error. 2012-01-16 19:34:42 -05:00
tests some small improvements 2012-01-31 11:35:58 +01:00
.gitignore some small improvements 2012-01-31 11:35:58 +01:00
LICENSE Fix year... 2010-10-22 12:43:32 -04:00
package.json add test script to package.json 2012-01-31 18:49:22 +01:00
readme.md add readdirSyncRecursive and the test runner file. 2012-01-14 14:10:47 -02: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');

// 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());
}

It should be noted that these are all currently synchronous operations.

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