| @ -1,4 +1,55 @@ | |||||
| automodule | automodule | ||||
| ========== | ========== | ||||
| Node.JS module to auto include your sub-modules or to generate static include scripts. | |||||
| Node.JS module to auto include your sub-modules or to generate static include scripts. | |||||
| Automodule will automatically determine if you are within your projects top level directory | |||||
| by search for `lib` or `src` directories, if so, the `lib` directory is all that is required. | |||||
| Otherwise each directory and file (other than `index.js`) will be required and exposed. | |||||
| ## Installation | |||||
| ```bash | |||||
| npm install -g automodule | |||||
| ``` | |||||
| ## Usage | |||||
| See `examples` directory. | |||||
| ### Generate Static File | |||||
| To generate the static text for the module simple run `automodule` from the project directory. | |||||
| ```bash | |||||
| $ cd ./project/directory | |||||
| $ ls | |||||
| one three two | |||||
| $ automodule | |||||
| return module.exports = {one:require("./one"),three:require("./three"),two:require("./two"),}; | |||||
| ``` | |||||
| Or you can specify an output file. | |||||
| ```bash | |||||
| $ cd ./project/directory | |||||
| $ ls | |||||
| one three two | |||||
| $ automodule index.js | |||||
| $ cat index.js | |||||
| return module.exports = {one:require("./one"),three:require("./three"),two:require("./two"),}; | |||||
| ``` | |||||
| ### Use programmatically | |||||
| You can also use `automodule` as a function within your scripts to include all submodules. | |||||
| ```node | |||||
| var automodule = require('automodule'); | |||||
| var options = {search:__dirname}; | |||||
| return module.exports = automodule(options); | |||||
| ``` | |||||
| ### Options | |||||
| * `search` - directory to look in for modules (String|default: `process.cwd()) | |||||
| * `print` - whether or not to include the modules or generate static string (Boolean|default: `false`) | |||||
| @ -0,0 +1,16 @@ | |||||
| #!/usr/bin/env node | |||||
| var fs = require('fs'); | |||||
| var automodule = require('automodule'); | |||||
| var modules = automodule({print:true}); | |||||
| if(process.argv.length > 2){ | |||||
| var out = process.argv[2]; | |||||
| if(fs.existsSync(out)){ | |||||
| console.error(out + ' already exists, exiting'); | |||||
| process.exit(1); | |||||
| } | |||||
| fs.writeFileSync(out, modules); | |||||
| } else{ | |||||
| console.log(modules); | |||||
| } | |||||
| @ -0,0 +1,3 @@ | |||||
| var test = require('./test'); | |||||
| console.dir(test); | |||||
| @ -0,0 +1,4 @@ | |||||
| var automodule = require('automodule'); | |||||
| var options = {search: __dirname}; | |||||
| return module.exports = automodule(options); | |||||
| @ -0,0 +1,3 @@ | |||||
| module.exports = { | |||||
| one: 'one' | |||||
| }; | |||||
| @ -0,0 +1,3 @@ | |||||
| module.exports = { | |||||
| three: 'three' | |||||
| }; | |||||
| @ -0,0 +1,3 @@ | |||||
| module.exports = { | |||||
| two: 'two' | |||||
| }; | |||||
| @ -0,0 +1,81 @@ | |||||
| 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 modules = {}; | |||||
| var all_files = get_files(search_path); | |||||
| 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); | |||||
| } | |||||
| }); | |||||
| } | |||||
| if(print){ | |||||
| return compile(modules); | |||||
| } else{ | |||||
| return modules; | |||||
| } | |||||
| }; | |||||
| var compile = function(modules){ | |||||
| var out = 'return module.exports = '; | |||||
| if(typeof(modules) == 'string'){ | |||||
| return out += modules + ';'; | |||||
| } | |||||
| out += '{'; | |||||
| for(var name in modules){ | |||||
| out += name + ':' + modules[name] + ','; | |||||
| } | |||||
| out += '};'; | |||||
| return out; | |||||
| }; | |||||
| var get_require = function(file, search_path, print){ | |||||
| if(print){ | |||||
| return 'require("./' + file + '")'; | |||||
| } else{ | |||||
| return require(search_path + '/' + file); | |||||
| } | |||||
| }; | |||||
| var get_files = function(search_path){ | |||||
| var files = []; | |||||
| fs.readdirSync(search_path).forEach(function(file){ | |||||
| if(file[0] == '.'){ | |||||
| return; | |||||
| } | |||||
| var stats = fs.lstatSync(search_path + '/' + file); | |||||
| if(stats.isDirectory()){ | |||||
| files.push(file); | |||||
| } else{ | |||||
| var len = file.length; | |||||
| var end = file.substring(len -3, len); | |||||
| if(end == '.js'){ | |||||
| files.push(file); | |||||
| } | |||||
| } | |||||
| }); | |||||
| return files; | |||||
| }; | |||||
| var get = function(obj, key, def){ | |||||
| if(obj[key] == null || obj[key] == undefined){ | |||||
| return def; | |||||
| } else{ | |||||
| return obj[key]; | |||||
| } | |||||
| }; | |||||