diff --git a/README.md b/README.md index 05a67d6..cf3a3a4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,55 @@ automodule ========== -Node.JS module to auto include your sub-modules or to generate static include scripts. \ No newline at end of file +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`) \ No newline at end of file diff --git a/bin/automodule b/bin/automodule index e69de29..010a8f8 100755 --- a/bin/automodule +++ b/bin/automodule @@ -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); +} \ No newline at end of file diff --git a/example/simple.js b/example/simple.js new file mode 100644 index 0000000..b1f2c8e --- /dev/null +++ b/example/simple.js @@ -0,0 +1,3 @@ +var test = require('./test'); + +console.dir(test); \ No newline at end of file diff --git a/example/test/index.js b/example/test/index.js new file mode 100644 index 0000000..af3ee34 --- /dev/null +++ b/example/test/index.js @@ -0,0 +1,4 @@ +var automodule = require('automodule'); +var options = {search: __dirname}; + +return module.exports = automodule(options); \ No newline at end of file diff --git a/example/test/one/index.js b/example/test/one/index.js new file mode 100644 index 0000000..d4d0de3 --- /dev/null +++ b/example/test/one/index.js @@ -0,0 +1,3 @@ +module.exports = { + one: 'one' +}; \ No newline at end of file diff --git a/example/test/three/index.js b/example/test/three/index.js new file mode 100644 index 0000000..c500d0d --- /dev/null +++ b/example/test/three/index.js @@ -0,0 +1,3 @@ +module.exports = { + three: 'three' +}; \ No newline at end of file diff --git a/example/test/two/index.js b/example/test/two/index.js new file mode 100644 index 0000000..5a288ea --- /dev/null +++ b/example/test/two/index.js @@ -0,0 +1,3 @@ +module.exports = { + two: 'two' +}; \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index e69de29..4bb1b2a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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]; + } +}; \ No newline at end of file