Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
107 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1 @@ | |||
test/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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" | |||
} | |||
} | } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,6 @@ | |||
var dive = require("../"); | |||
|
|||
dive(process.cwd(), { all: false, recursive: false }, function(err, file) { | |||
if (err) throw err; | |||
console.log(file); | |||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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); |