Comparing version 0.0.1 to 0.0.2
39
index.js
var main = function() { | ||
// ran directly from the cli? | ||
var ranDirectly = !module.parent.parent; | ||
// no need to load optimist if we're not going to use it. | ||
var optimist = ranDirectly ? require('optimist') : undefined; | ||
var exitFn = function(exitCode, message) { | ||
if (message) { console.error(message); } | ||
process.exit(exitCode || 0); | ||
}; | ||
// Load in options for optimist | ||
this.flags = function(flags) { | ||
if (!ranDirectly || !flags) { return this; } | ||
for (var key in flags) { | ||
optimist.options(key, flags[key]); | ||
} | ||
// check for any errors & reset argv | ||
optimist.argv = optimist.parse(process.argv); | ||
return this; | ||
}; | ||
// Run the main function if we're running this script directly | ||
this.run = function(mainFn) { | ||
if (!ranDirectly) { return; } | ||
mainFn(optimist.argv, exitFn); | ||
}; | ||
}; | ||
/* | ||
@param {object} options - Optimist formatted options | ||
@param {function} mainFn - Function that takes optimist argv | ||
*/ | ||
module.exports = function(mainFn) { | ||
// If the script that required US does not have a parent, then | ||
// we assume it was run the the CLI (and not required!) | ||
if (!module.parent.parent) { | ||
var argv = require('optimist').argv; | ||
mainFn(argv); | ||
} | ||
}; | ||
module.exports = new main(); |
{ | ||
"name": "main", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
node-main | ||
========= | ||
A *very* basic module that only runs code if the script is ran directly (And not required). It utilizes [optimist](https://github.com/substack/node-optimist) for argument parsing. See [this SO question](http://stackoverflow.com/q/4981891/586621) for more information on how it works. | ||
A *very* basic module that only runs code if the script is ran directly (e.g. calling `node script`). It will not call the code if the script has been required (e.g. `require('script')`). | ||
## Usage | ||
node-main utilizes [optimist](https://github.com/substack/node-optimist) for argument parsing. | ||
## Installation & Usage | ||
npm install main | ||
### require('main') | ||
Once required, you can chain the functions below | ||
### .flags(options) | ||
`options` follows the [optimist format for options](https://github.com/substack/node-optimist#optionskey-opt), but groups them together, e.g.: | ||
```javascript | ||
require('main')(function(argv) { | ||
// The code in here will only be executed if running from the CLI | ||
// | ||
// argv is the argv object that optimist produces, e.g. | ||
// require('optimist').argv; | ||
}); | ||
require('main').flags({ | ||
f: { alias: 'flag' }, | ||
t: { alias: 'secondFlag' }, | ||
d: { demand: true }, | ||
// ... | ||
}) | ||
``` | ||
## Example | ||
### .run(fn) | ||
test.js | ||
`fn` is the callback that will be invoked when the script is ran directly from a terminal. It can take the following parameters: | ||
```javascript | ||
exports.foo = function() { return 'bar'; } | ||
fn(argv, exit) | ||
``` | ||
require('main')(function(argv) { | ||
console.log(exports.foo()); | ||
- `argv` is the parsed [optimist argv object](https://github.com/substack/node-optimist#argv) | ||
- `exit` is a helper function that can be used to exit the script. It follows the form `exit(exitCode, optionalMessage)`. If no exit code if provided, it will exit with 0 (success). | ||
## Example | ||
```javascript | ||
#!/usr/bin/env node | ||
exports.isTrue = function(value) { // just to get a point across... | ||
return value === true; | ||
}; | ||
require('main') | ||
.flags({ | ||
v: { alias: 'valid', 'boolean': true, 'default': false } | ||
}) | ||
.run(function(argv, exit) { | ||
if (exports.isTrue(argv.valid)) { | ||
exit(); | ||
} else { | ||
exit(1, 'optional error message'); | ||
} | ||
}); | ||
``` | ||
Running from the terminal: | ||
Running from the terminal ($? indicates exit status): | ||
$ node test.js | ||
bar | ||
```bash | ||
> node scriptAbove.js --valid | ||
> $? | ||
0 | ||
> node scriptAbove.js | ||
optional error message | ||
> $? | ||
1 | ||
``` | ||
@@ -37,4 +78,5 @@ Using the module from another script will not execute the code in main: | ||
```javascript | ||
var test = require('./test'); | ||
console.log(test.foo()); // 'bar' | ||
var scriptAbove = require('./scriptAbove'); | ||
console.log(scriptAbove.valid('hello')); // 'false' | ||
console.log(scriptAbove.valid(true)); // 'true' | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4627
30
82