Browse Source

version 0.1.1, added recursive directory lookups as well as better handling of index.js and dirctories

master
Brett Langdon 13 years ago
parent
commit
fe00940ccb
15 changed files with 81 additions and 25 deletions
  1. +9
    -0
      example/recursive/index.js
  2. +5
    -0
      example/recursive/submodule/api.js
  3. +3
    -0
      example/recursive/submodule/index.js
  4. +3
    -0
      example/recursive/submodule/tools/index.js
  5. +3
    -0
      example/recursive/submodule/tools/string/another.js
  6. +4
    -0
      example/recursive/submodule/tools/string/index.js
  7. +3
    -0
      example/recursive/submodule/tools/string/something.js
  8. +0
    -3
      example/simple.js
  9. +5
    -0
      example/simple/index.js
  10. +0
    -0
      example/simple/one/index.js
  11. +0
    -0
      example/simple/three/index.js
  12. +0
    -0
      example/simple/two/index.js
  13. +0
    -4
      example/test/index.js
  14. +45
    -17
      lib/index.js
  15. +1
    -1
      package.json

+ 9
- 0
example/recursive/index.js View File

@ -0,0 +1,9 @@
var automodule = require('../../');
var options = {
search: __dirname + '/submodule',
recursive: 3,
};
var submodule = automodule(options);
console.dir(submodule);

+ 5
- 0
example/recursive/submodule/api.js View File

@ -0,0 +1,5 @@
return module.exports = {
'api': 1.5,
'test': function(){},
'email': function(to, from, body){}
};

+ 3
- 0
example/recursive/submodule/index.js View File

@ -0,0 +1,3 @@
return module.exports = {
'global': 'value'
};

+ 3
- 0
example/recursive/submodule/tools/index.js View File

@ -0,0 +1,3 @@
return module.exports = {
'tools': 'index'
};

+ 3
- 0
example/recursive/submodule/tools/string/another.js View File

@ -0,0 +1,3 @@
return module.exports = {
'test': 'value'
};

+ 4
- 0
example/recursive/submodule/tools/string/index.js View File

@ -0,0 +1,4 @@
return module.exports = {
'split': function(){},
'rename': function(){}
};

+ 3
- 0
example/recursive/submodule/tools/string/something.js View File

@ -0,0 +1,3 @@
return module.exports = {
'some': 'data'
};

+ 0
- 3
example/simple.js View File

@ -1,3 +0,0 @@
var test = require('./test');
console.dir(test);

+ 5
- 0
example/simple/index.js View File

@ -0,0 +1,5 @@
var automodule = require('../../');
var options = {search: __dirname};
var test = automodule(options);
console.dir(test);

example/test/one/index.js → example/simple/one/index.js View File


example/test/three/index.js → example/simple/three/index.js View File


example/test/two/index.js → example/simple/two/index.js View File


+ 0
- 4
example/test/index.js View File

@ -1,4 +0,0 @@
var automodule = require('automodule');
var options = {search: __dirname};
return module.exports = automodule(options);

+ 45
- 17
lib/index.js View File

@ -1,26 +1,26 @@
var path = require('path');
var fs = require('fs');
var automodule = module.exports = function(options){
options = (options)?options:{};
var search_path = String(get(options, 'search', process.cwd()));
var print = Boolean(get(options, 'print', false));
var recursive = Number(get(options, 'recursive', 1));
var modules = {};
var all_files = get_files(search_path);
var all_files = get_files(search_path, recursive);
var top_level = ~all_files.indexOf('lib') || ~all_files.indexOf('src');
if(top_level){
modules = get_require('lib', search_path, print);
} else{
all_files.forEach(function(file){
if(~file.indexOf('.js')){
var len = file.length;
var name = file.substring(0, len - 3);
if(name.toLowerCase() != 'index'){
modules[name] = get_require(file, search_path, print);
}
} else{
modules[file] = get_require(file, search_path, print);
}
all_files.forEach(function(file){
var module = null;
var abs = path.resolve(search_path, file);
var ext = path.extname(file);
var name = path.basename(abs, ext);
var dirname = path.dirname(file);
module = get_require(file, search_path, print);
add_module(modules, file.replace(ext, '').split('/'), module);
});
}
if(print){
@ -52,19 +52,29 @@ var get_require = function(file, search_path, print){
}
};
var get_files = function(search_path){
var get_files = function(search_path, recurse_level){
var files = [];
if(recurse_level < 1){
return files;
}
fs.readdirSync(search_path).forEach(function(file){
if(file[0] == '.'){
return;
}
var stats = fs.lstatSync(search_path + '/' + file);
var abs_path = path.resolve(search_path, file);
var stats = fs.lstatSync(abs_path);
if(stats.isDirectory()){
files.push(file);
var new_files = get_files(abs_path, recurse_level-1);
new_files.forEach(function(new_f){
files.push(file + '/' + new_f);
});
if(!new_files.length && fs.existsSync(path.resolve(abs_path, 'index.js'))){
files.push(file);
}
} else{
var len = file.length;
var end = file.substring(len -3, len);
if(end == '.js'){
var ext = path.extname(file);
if(ext == '.js' || ext == '.json'){
files.push(file);
}
}
@ -78,4 +88,22 @@ var get = function(obj, key, def){
} else{
return obj[key];
}
};
var add_module = function(obj, name_parts, module){
if(name_parts.length <= 1){
if(name_parts[0].toLowerCase() == 'index'){
for(var name in module){
obj[name] = module[name]
}
}else{
obj[name_parts[0]] = module;
}
}else{
if(obj[name_parts[0]] == undefined){
obj[name_parts[0]] = {};
}
obj[name_parts[0]] = add_module(obj[name_parts[0]], name_parts.slice(1), module);
}
return obj;
};

+ 1
- 1
package.json View File

@ -2,7 +2,7 @@
"author": "Brett Langdon <brett@blangdon.com> (http://www.brett.is)",
"name": "automodule",
"description": "automagically include submodules or create static include scripts",
"version": "0.1.0",
"version": "0.1.1",
"homepage": "http://github.com/brettlangdon/automodule.git",
"repository": {
"type": "git",


Loading…
Cancel
Save