Permalink
Browse files

Initial commit

  • Loading branch information...
1 parent d59128b commit a40974e98a70bddbace33134415ca2abbb5c6706 @nathanoehlman committed Nov 12, 2012
Showing with 169 additions and 3 deletions.
  1. +38 −3 README.md
  2. +61 −0 index.js
  3. +22 −0 package.json
  4. +3 −0 test/fixtures/deep/test3.js
  5. +3 −0 test/fixtures/test1.js
  6. +3 −0 test/fixtures/test2.js
  7. +39 −0 test/inclusive.js
View
@@ -1,4 +1,39 @@
-inclusive
-=========
+#inclusive
-Simple module to bring in a pile of files
+
+Ultra simple utility to make it easier to do something with a whole pile of files in a directory. Ie. Including them in a require, etc... This doesn't do a lot of things, and is mainly to save me from repeating myself :)
+
+### Todo
+
+* Possible reimplement using fstream, and make it emit the events (allow filters, etc)
+
+### Installation
+
+```npm install inclusive```
+
+### Usage
+
+To require all the files in a directory
+```
+var inclusive = require('inclusive'),
+ path = require('path');
+
+// Include all the handlers in the handlers directory
+inclusive(path.join(__dirname, 'handlers', function(err, includes) {
+
+ // Results
+ // { relative_file_path: file exports }
+
+});
+```
+
+To do something else with all the files
+```
+var inclusive = require('inclusive'),
+ path = require('path');
+
+inclusive(path.join(__dirname, 'files', {handler: function(filePath) { return filePath; }, function(err, files) {
+ // Results
+ // { relative_file_path: absolute_file_path }
+});
+```
View
@@ -0,0 +1,61 @@
+var _ = require('lodash'),
+ async = require('async'),
+ fs = require('fs'),
+ path = require('path');
+
+function readDirectory(directory, prefix, handler, callback) {
+
+ fs.readdir(directory, function(err, files) {
+ if (err) return callback(err);
+
+ var results = {};
+
+ async.forEach(
+ files,
+ function(file, done) {
+
+ var filePath = path.join(directory, file);
+
+ // Stat the file to determine if directory or file
+ fs.stat(filePath, function(err, stats) {
+ if (err) return done(err);
+
+ // If a directory, then read the contents of that directory
+ if (stats.isDirectory()) {
+ readDirectory(filePath, prefix + file + '/', handler, function(err, includes) {
+ if (err) return done(err);
+ results = _.extend(results, includes);
+ done();
+ });
+ } else {
+ // Include the file and process it using the handler
+ try {
+ var include = handler(path.join(directory, file));
+ results[prefix + file] = include;
+ return done();
+ } catch (e) {
+ return done(e);
+ }
+ }
+ });
+
+ },
+ function(err) {
+ if (err) return callback(err);
+ return callback(null, results);
+ }
+ );
+ });
+}
+
+module.exports = function(includeDir, opts, callback) {
+
+ if (!callback && typeof opts == 'function') {
+ callback = opts;
+ opts = {};
+ }
+ opts = opts || {};
+
+ var handler = opts.handler || require;
+ readDirectory(includeDir, '', handler, callback);
+}
View
@@ -0,0 +1,22 @@
+{
+ "name": "inclusive",
+ "version": "0.0.0",
+ "description": "A simple module to pull in a pile of files so you don't have to",
+ "main": "index.js",
+ "scripts": {
+ "test": "mocha"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/nathanoehlman/inclusive.git"
+ },
+ "author": "Nathan Oehlman",
+ "license": "BSD",
+ "dependencies": {
+ "async": "~0.1.22",
+ "lodash": "~0.9.2"
+ },
+ "devDependencies": {
+ "chai": "~1.3.0"
+ }
+}
@@ -0,0 +1,3 @@
+module.exports = {
+ name: '3'
+}
View
@@ -0,0 +1,3 @@
+module.exports = {
+ name: '1'
+}
View
@@ -0,0 +1,3 @@
+module.exports = {
+ name: '2'
+}
View
@@ -0,0 +1,39 @@
+var path = require('path'),
+ should = require('chai').should(),
+ inclusive = require('..');
+
+describe('Inclusive', function() {
+
+ it('should be able to import a directory using default requires', function(done) {
+ inclusive(path.join(__dirname, '/fixtures'), function(err, includes) {
+ if (err) return done(err);
+ includes.should.have.property('test1.js');
+ includes.should.have.property('test2.js');
+ includes.should.have.property('deep/test3.js');
+ includes['test1.js'].name.should.equal('1');
+ done();
+ });
+ });
+
+ it('should return an error when attempting to import a dodgy directory', function(done) {
+ inclusive('jasdasd', function(err) {
+ if (err) return done();
+ return done('Did not throw error');
+ })
+ });
+
+ it('should be able to provide a custom handler', function(done) {
+
+ var opts = {
+ handler: function(filePath) {
+ return filePath;
+ }
+ };
+
+ inclusive(path.join(__dirname, '/fixtures'), opts, function(err, includes) {
+ if (err) return done(err);
+ includes.should.have.property('test1.js', path.join(__dirname, 'fixtures', 'test1.js'));
+ done();
+ });
+ });
+});

0 comments on commit a40974e

Please sign in to comment.