Browse Source

version 0.1.0, added basic features and example, update readme

master
Brett Langdon 13 years ago
parent
commit
cd6877084d
8 changed files with 165 additions and 1 deletions
  1. +52
    -1
      README.md
  2. +16
    -0
      bin/automodule
  3. +3
    -0
      example/simple.js
  4. +4
    -0
      example/test/index.js
  5. +3
    -0
      example/test/one/index.js
  6. +3
    -0
      example/test/three/index.js
  7. +3
    -0
      example/test/two/index.js
  8. +81
    -0
      lib/index.js

+ 52
- 1
README.md View File

@ -1,4 +1,55 @@
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`)

+ 16
- 0
bin/automodule View File

@ -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);
}

+ 3
- 0
example/simple.js View File

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

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

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

+ 3
- 0
example/test/one/index.js View File

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

+ 3
- 0
example/test/three/index.js View File

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

+ 3
- 0
example/test/two/index.js View File

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

+ 81
- 0
lib/index.js View File

@ -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];
}
};

Loading…
Cancel
Save