• This repository has been archived on 19/Jun/2019
  • Stars
    star
    438
  • Rank 95,970 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 14 years ago
  • Updated almost 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Recursive file operations in Node.js

Warning: This Project Is Deprecated!

wrench.js is deprecated, and hasn't been updated in quite some time. I heavily recommend using fs-extra to do any extra filesystem operations.

Wrench was built for the early days of Node, and it solved a problem that needed solving. I'm proud of what it's done; at the time of writing this, it was still downloaded over 25,000 times yesterday, and over 500,000 times in the last month. The fact that it wound up being embedded in so many projects is humbling and a great source of fun for me, but I just don't have the time to keep up with this at the moment. No alternate maintainers have appeared, and fs-extra is very well maintained anyway - one community solution is likely better.

So long, and thanks for all the fish. The original docs remain available here for anyone who may need them. If I could 301 a GitHub repository I'd do so.

Cheers,

  • Ryan McGrath

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:

Possibly Breaking Change in v1.5.0

In previous versions of Wrench, we went against the OS-default behavior of not deleting a directory unless the operation is forced. In 1.5.0, this has been changed to be the behavior people expect there to be - if you try to copy over a directory that already exists, you'll get an Error returned or thrown stating that you need to force it.

Something like this will do the trick:

wrench.copyDirSyncRecursive('directory_to_copy', 'location_where_copy_should_end_up', {
    forceDelete: true
});

If you desire the older behavior of Wrench... hit up your package.json. If you happen to find bugs in the 1.5.0 release please feel free to file them on the GitHub issues tracker for this project, or send me a pull request and I'll get to it as fast as I can. Thanks!

If this breaks enough projects I will consider rolling it back. Please hit me up if this seems to be the case.

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', {
    forceDelete: bool, // Whether to overwrite existing directory or not
    excludeHiddenUnix: bool, // Whether to copy hidden Unix files or not (preceding .)
    preserveFiles: bool, // If we're overwriting something and the file already exists, keep the existing
    preserveTimestamps: bool, // Preserve the mtime and atime when copying files
    inflateSymlinks: bool, // Whether to follow symlinks or not when copying files
    filter: regexpOrFunction, // A filter to match files against; if matches, do nothing (exclude).
    whitelist: bool, // if true every file or directory which doesn't match filter will be ignored
    include: regexpOrFunction, // An include filter (either a regexp or a function)
    exclude: regexpOrFunction // An exclude filter (either a regexp or a function)
});

// Note: If a RegExp is provided then then it will be matched against the filename. If a function is
//       provided then the signature should be the following:
//       function(filename, dir) { return result; }

// Read lines in from a file until you hit the end
var f = new wrench.LineReader('x.txt');
while(f.hasNextLine()) {
	util.puts(f.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
});

// If you're feeling somewhat masochistic
wrench.copyDirRecursive(srcDir, newDir, {forceDelete: bool /* See sync version */}, callbackfn);

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

More Repositories

1

twython

Actively maintained, pure Python wrapper for the Twitter API. Supports both normal and streaming Twitter APIs.
Python
1,849
star
2

cacao

Rust bindings for AppKit (macOS) and UIKit (iOS/tvOS). Experimental, but working!
Rust
1,560
star
3

alchemy

An experimental GUI framework for Rust, backed by per-platform native widgets. React, AppKit/UIKit inspired. EXPERIMENTAL, runs on Cocoa right now. ;P
Rust
385
star
4

wii-js

A sane, documented, (hopefully) performant event-based library for Wiimote webpage interaction.
JavaScript
154
star
5

jelly

User authentication/sessions/etc for Actix-Web. More of a sample project than a crate, but probably useful to some people.
Rust
89
star
6

twython-django

An example Django application to showcase how to use OAuth with Twitter in Django using Twython.
Python
73
star
7

1.1.1.1-macOS

Experimenting with cloning CloudFlare's 1.1.1.1 app as a macOS status bar app.
Swift
34
star
8

cloudkit-sane-sharing

A dump of code that illustrates a better way to share CloudKit resources.
Swift
29
star
9

react-iconpack

A React Component for handling SVG icons, coupled with Babel and Browserify plugins to only bundle the icons you use.
JavaScript
25
star
10

django-rednoise

An opinionated addon for WhiteNoise, with a focus on Django environments.
Python
18
star
11

pythentic_jobs

A pure Python wrapper around the Authentic Jobs (http://www.authenticjobs.com) API.
Python
18
star
12

svgalib-1

It's not svgalib "dash" 1, it's svgalib negative 1, because if you still use this library there's probably something wrong with you. That said, this is a 'fork' of the most recent (haha) version found on the internets, with a ton of patches from various people around the internet cobbled together. I have not and will not make any outlandish efforts to credit people, but if you see something here you wrote and you want credit, message me. Should fix a lot of compiling issues under recent issues of Linux.
C
16
star
13

webpack-babel-react-setup-lesson

Walking through setting up Webpack, Babel, and React.
JavaScript
15
star
14

memelee

An unofficial smash.gg app, read-only.
JavaScript
13
star
15

node-utf8

utf8 encoding and decoding in Node.js
JavaScript
12
star
16

drinkkitcom

A Foursquare clone written in Django to let Redditors broadcast DC bar crawls.
Python
11
star
17

gitstatus

A Github-repository widget to display most recent commits on a given repository.
JavaScript
8
star
18

jTransliterate

Transliterate [Hirag/Katak]ana to Latin/English and back with Python. Convert half/full-width Japanese text.
Python
7
star
19

holidaycalendar

A demo holiday calendar, which shows how to make NSCollectionViewItem's swipeable.
Swift
6
star
20

franz

Client side color swatches.
JavaScript
4
star
21

beyond-react

Session 4, going beyond React and integrating with other libraries.
JavaScript
3
star
22

activity-scraper

A social media scraper written in Rust. Used for the data on my personal site.
Rust
3
star
23

shinekit

iOS/macOS/Windows UI in Rust. Highly experimental.
Rust
3
star
24

rubeclosures

Ruby wrapper for the foreclosurelistings.com API
Ruby
2
star
25

splash

A programming language aimed at kids. Builds on their existing writing skills and doesn't try to reinvent arcane logic.
JavaScript
2
star
26

react-flux-redux-lesson

Walkthrough guide for React session #2 via Codementor.
JavaScript
2
star
27

react-router-lesson

React Router & co.
JavaScript
1
star
28

jsmag

Random JSMag Code Samplings
JavaScript
1
star
29

katakana

An Android application that aims to teach people Katakana through a basic brain-timing-calculation/algorithmic method.
Java
1
star
30

redditimages

Reddit: Images in Comments hack
JavaScript
1
star
31

takeoff

A Chrome extension to randomly load up one of your bookmarks on new windows/tabs, built for user "hokku". Enjoy.
1
star
32

smashgg-upcoming-tournaments

A scraper example for finding upcoming tournaments on smash.gg. From an unreleased side project, might be fun for some people.
Rust
1
star