Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
pvorb committed Sep 20, 2011
1 parent 0dcebf3 commit 98c47de
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 78 deletions.
1 change: 1 addition & 0 deletions .npmignore
@@ -0,0 +1 @@
test/
37 changes: 22 additions & 15 deletions README.mkd
@@ -1,28 +1,35 @@
_dive_ is a tiny module for node that is able to recursively walk ("dive") a **dive** is a tiny module for node that is able to recursively walk (_dive_) a
directory tree. You may use it as follows: directory tree.


var dive = require("dive"); ## Example


dive(process.cwd(), function(err, file) { ```javascript
if (err) var dive = require("dive");
throw err;


console.log(file); dive(process.cwd(), function(err, file) {
}); if (err) throw err;

console.log(file);
});
```


This will list all files in your current working directory without an order. This will list all files in your current working directory without an order.


You may also apply options to the function call. You may also apply options to the function call.


dive(dir, opt, action); ```javascript
dive(dir, opt, action);
```


The default options are The default options are


{ ```javascript
recursive: true, // If set to false, this will ignore subdirectories. {
all: false // If set to true, this will show "dot files" and recursive: true, // If set to false, this will ignore subdirectories.
// files in "dot directories", e.g. ".gitinore" or all: false // If set to true, this will show "dot files" and
// ".git/HEAD". // files in "dot directories", e.g. ".gitinore" or
} // ".git/HEAD".
}
```


This argument is optional. This argument is optional.
92 changes: 44 additions & 48 deletions lib/dive.js
@@ -1,55 +1,51 @@
var fs = require("fs"); var fs = require('fs');


// General function // General function
var dive = function(dir, opt, action) { var dive = function(dir, opt, action) {


// action is the last argument // action is the last argument
action = arguments[arguments.length - 1]; action = arguments[arguments.length - 1];


// Assert that dir is a string // Assert that dir is a string
if (typeof dir !== "string") if (typeof dir != 'string')
dir = process.cwd(); dir = process.cwd();


// Assert that opt is an object // Assert that opt is an object
if (opt == undefined || typeof opt !== "object") if (typeof opt == 'undefined')
opt = { }; opt = { };


// Default settings // Default settings
if (opt.recursive == undefined) if (opt.recursive == undefined)
opt.recursive = true; opt.recursive = true;
if (opt.all == undefined) if (opt.all == undefined)
opt.all = false; opt.all = false;


// Assert that it's a function // Read the directory
if (typeof action !== "function") fs.readdir(dir, function(err, list) {
action = function(error, file) { }; // Return the error if something went wrong

if (err)
// Read the directory return action(err);
fs.readdir(dir, function(err, list) {
// Return the error if something went wrong // For every file in the list
if (err) list.forEach(function(file) {
return action(err); if (opt.all || file[0] != '.') {

// Full path of that file
// For every file in the list var path = dir + '/' + file;
list.forEach(function(file) { // Get the file's stats
if (opt.all || file[0] != ".") { fs.stat(path, function(err, stat) {
// Full path of that file // If the file is a directory
var path = dir + "/" + file; if (stat && stat.isDirectory()) {
// Get the file's stats // Dive into the directory
fs.stat(path, function(err, stat) { if (opt.recursive)
// If the file is a directory dive(path, opt, action);
if (stat && stat.isDirectory()) { } else {
// Dive into the directory // Call the action
if (opt.recursive) action(null, path);
dive(path, opt, action); }
} else { });
// Call the action }
action(null, path); });
} });
});
}
});
});
}; };


module.exports = dive; module.exports = dive;
34 changes: 19 additions & 15 deletions package.json
@@ -1,17 +1,21 @@
{ {
"name": "dive", "name": "dive",
"description": "description":
"walk through directory trees and apply an action to every file", "walk through directory trees and apply an action to every file",
"author": "Paul Vorbach <paul@vorb.de>", "author": "Paul Vorbach <paul@vorb.de>",
"version": "0.0.3", "version": "0.0.4",
"main": "./lib/dive.js", "main": "./lib/dive.js",
"repository": "git://github.com/pvorb/node-dive.git", "repository": "git://github.com/pvorb/node-dive.git",
"tags": [ "tags": [
"recursive", "recursive",
"file walking", "file walking",
"directories" "directories",
], "async"
"bugs": { ],
"web": "http://github.com/pvorb/dive/issues" "bugs": {
} "web": "http://github.com/pvorb/node-dive/issues"
},
"engines": {
"node": ">=0.4.0"
}
} }
6 changes: 6 additions & 0 deletions test/test.js
@@ -0,0 +1,6 @@
var dive = require("../");

dive(process.cwd(), { all: false, recursive: false }, function(err, file) {
if (err) throw err;
console.log(file);
});
15 changes: 15 additions & 0 deletions test/test2.js
@@ -0,0 +1,15 @@
var dive = require("../");

var res = [];

dive(process.cwd(), function(err, file) {
if (err)
throw err;
res.push(file);
});

setTimeout(function() {
console.log(res);
console.log();
console.log(res.sort());
}, 500);

0 comments on commit 98c47de

Please sign in to comment.