Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 7fdb23eacd6a29b5f4ea8b2eb1b8c2ad4550453d @pvorb committed Sep 20, 2011
Showing with 128 additions and 0 deletions.
  1. +18 −0 LICENSE.mkd
  2. +34 −0 README.mkd
  3. +46 −0 lib/diveSync.js
  4. +24 −0 package.json
  5. +6 −0 test/test.js
18 LICENSE.mkd
@@ -0,0 +1,18 @@
+Copyright © 2011 Paul Vorbach <paul@vorb.de>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the “Software”), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 README.mkd
@@ -0,0 +1,34 @@
+**diveSync** is a tiny module for node that is able to recursively walk (_„dive“_) a directory tree. **diveSync** is the synchronous version of [dive](//github.com/pvorb/node-dive).
+
+## Example
+
+```javascript
+var diveSync = require("diveSync");
+
+diveSync(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.
+
+You may also apply options to the function call.
+
+```javascript
+dive(dir, opt, action);
+```
+
+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
+ // files in "dot directories", e.g. ".gitinore" or
+ // ".git/HEAD".
+}
+```
+
+This argument is optional.
46 lib/diveSync.js
@@ -0,0 +1,46 @@
+var fs = require('fs'),
+ append = require('append');
+
+// General function
+var diveSync = function(dir, opt, action) {
+
+ // action is the last argument
+ action = arguments[arguments.length - 1];
+
+ // default options
+ var defaultOpt = {
+ recursive: true,
+ all: false
+ };
+
+ // ensure opt is an object
+ if (typeof opt != 'object')
+ opt = {};
+
+ opt = append(defaultOpt, opt);
+
+ // Read the directory
+ var list = fs.readdirSync(dir);
+
+ // For every file in the list
+ list.forEach(function(file) {
+ if (opt.all || file[0] != '.') {
+ // Full path of that file
+ var path = dir + '/' + file;
+ // Get the file's stats
+ var stat = fs.statSync(path);
+
+ // If the file is a directory
+ if (stat && stat.isDirectory()) {
+ // Dive into the directory
+ if (opt.recursive)
+ dive(path, opt, action);
+ } else {
+ // Call the action
+ action(null, path);
+ }
+ }
+ });
+};
+
+module.exports = diveSync;
24 package.json
@@ -0,0 +1,24 @@
+{
+ "name": "diveSync",
+ "description":
+ "walk through directory trees and apply an action on every file (synchronous dive)",
+ "author": "Paul Vorbach <paul@vorb.de>",
+ "version": "0.0.0",
+ "main": "./lib/diveSync.js",
+ "repository": "git://github.com/pvorb/node-diveSync.git",
+ "tags": [
+ "recursive",
+ "file walking",
+ "directories",
+ "sync"
+ ],
+ "bugs": {
+ "web": "http://github.com/pvorb/node-diveSync/issues"
+ },
+ "engines": {
+ "node": ">=0.4.0"
+ },
+ "dependencies": {
+ "append": ">=0.1.0"
+ }
+}
6 test/test.js
@@ -0,0 +1,6 @@
+var diveSync = require("../");
+
+diveSync(process.cwd(), { all: false, recursive: false }, function(err, file) {
+ if (err) throw err;
+ console.log(file);
+});

0 comments on commit 7fdb23e

Please sign in to comment.