Skip to content

Commit

Permalink
share project
Browse files Browse the repository at this point in the history
  • Loading branch information
bodenr committed Nov 8, 2012
1 parent 5499874 commit 8c61e64
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
.settings
.project
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.git*
.settings
.project
.travis.yml
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
(The MIT License)

Copyright (c) 2011-2012 Boden Russell <bodensemail@gmail.com>

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, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
121 changes: 121 additions & 0 deletions lib/expose.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@

var fs = require('fs'),
path = require('path'),
env = process.env.NODE_ENV || 'development';

function debug() {
if (env == 'debug' || env == 'trace') {
console.log.apply(this, arguments);
}
};

/**
* Perform a top level mixing between and source
* and destination object optionally skipping
* undefined/null properties.
*
* Examples:
*
* mixin({a: 'A'}, {b: 'B});
* // => {a: 'A', b: 'B'}
*
* mixin({'a': null}, {b: 'B}, true);
* // => {b: 'B'}
*
* @param {Object} src
* @param {Object} dest
* @param {Boolean} skipEmpty
* @returns {Object}
* @api private
*/

function mixin(src, dest, skipEmpty) {
// TODO: refactor into common module
dest = dest || {}, src = src || {};
Object.keys(src).forEach(function(key) {
if (!dest[key] && (skipEmpty && src[key] != null && src[key] != undefined)) {
dest[key] = src[key];
}
});
return dest;
};

function toPath(dir, file) {
// TODO: move into common module
return path.resolve(dir) + path.sep + file;
};

function test(target, grep, ungrep) {
function testRegexes(regexes) {
return regexes.some(function(regex) {
return regex.test(target);
});
}
return testRegexes(grep) && !testRegexes(ungrep);
};

function importModule(module, scope) {
debug("Importing module: " + module);

var imports = require(module);
for (k in imports) {
if (imports.hasOwnProperty(k)) {
scope[k] = imports[k];
}
}
};

function load(target, opts) {
target = path.resolve(target);

debug("Load enter: " + target);

if (fs.statSync(target).isDirectory()) {
fs.readdirSync(target).forEach(function(file) {
var fullPath = target + path.sep + file;

if (fs.statSync(fullPath).isDirectory() && opts.recurse) {
load(fullPath, opts);
} else if (test(fullPath, opts.grep, opts.ungrep)) {
importModule(fullPath, opts.scope);
}
});
} else if (test(target, opts.grep, opts.ungrep)) {
importModule(target, opts.scope);
}
};

function defaultTarget() {
[toPath(__dirname, 'lib'), toPath(__dirname, 'src')].forEach(function(target) {
try {
if (fs.lstatSync(target).isDirectory()) {
return target;
}
} catch (e) {
// no path
}
});

return __dirname;
};

function defaultOpts(opts) {
return mixin({targets: defaultTarget(),
grep: [/\.js$/],
ungrep: [/node_modules/],
scope: {},
recurse: true},
opts, true);
};

exports.expose = function(options) {
var opts = defaultOpts(options),
targets = Array.isArray(opts.targets) ? opts.targets : [opts.targets];

debug("Expose with options:\n", opts);

targets.forEach(function(target) {
load(target, opts);
});
return opts.scope;
};
25 changes: 25 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "expose",
"version": "0.0.0",
"description": "Consolodate exports from sub modules",
"keywords": [
"utilities"
],
"author": "Boden Russell <bodensemail@gmail.com>",
"repository": {
"type": "git",
"url": "git://github.com/bodenr/expose.git"
},
"main": "index",
"engines": {
"node": "*"
},
"scripts": {
"test": "make test"
},
"directories": {
"doc": "./doc"
},
"license": "MIT",
"private": true
}

0 comments on commit 8c61e64

Please sign in to comment.