Comparing version 3.0.0 to 3.1.0
53
index.js
@@ -1,2 +0,1 @@ | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
@@ -21,3 +20,2 @@ var path = require('path'); | ||
var getNodeFlags = require('./lib/get_node_flags'); | ||
var prepareConfig = require('./lib/prepare_config'); | ||
@@ -86,6 +84,2 @@ function Liftoff(opts) { | ||
} | ||
// resolve symlink if needed | ||
if (fs.lstatSync(configPath).isSymbolicLink()) { | ||
configPath = fs.realpathSync(configPath); | ||
} | ||
} | ||
@@ -167,6 +161,7 @@ | ||
Liftoff.prototype.launch = function(opts, fn) { | ||
Liftoff.prototype.prepare = function(opts, fn) { | ||
if (typeof fn !== 'function') { | ||
throw new Error('You must provide a callback function.'); | ||
} | ||
process.title = this.processTitle; | ||
@@ -179,2 +174,16 @@ | ||
var env = this.buildEnvironment(opts); | ||
fn.call(this, env); | ||
}; | ||
Liftoff.prototype.execute = function(env, forcedFlags, fn) { | ||
if (typeof forcedFlags === 'function') { | ||
fn = forcedFlags; | ||
forcedFlags = undefined; | ||
} | ||
if (typeof fn !== 'function') { | ||
throw new Error('You must provide a callback function.'); | ||
} | ||
this.handleFlags(function(err, flags) { | ||
@@ -186,5 +195,2 @@ if (err) { | ||
var env = this.buildEnvironment(opts); | ||
var forcedFlags = getNodeFlags.arrayOrFunction(opts.forcedFlags, env); | ||
flaggedRespawn(flags, process.argv, forcedFlags, execute.bind(this)); | ||
@@ -198,3 +204,4 @@ | ||
if (ready) { | ||
prepareConfig(this, env, opts); | ||
preloadModules(this, env); | ||
registerLoader(this, this.extensions, env.configPath, env.cwd); | ||
fn.call(this, env, argv); | ||
@@ -206,2 +213,26 @@ } | ||
Liftoff.prototype.launch = function(opts, fn) { | ||
if (typeof fn !== 'function') { | ||
throw new Error('You must provide a callback function.'); | ||
} | ||
var self = this; | ||
self.prepare(opts, function(env) { | ||
var forcedFlags = getNodeFlags.arrayOrFunction(opts.forcedFlags, env); | ||
self.execute(env, forcedFlags, fn); | ||
}); | ||
}; | ||
function preloadModules(inst, env) { | ||
var basedir = env.cwd; | ||
env.require.filter(toUnique).forEach(function(module) { | ||
inst.requireLocal(module, basedir); | ||
}); | ||
} | ||
function toUnique(elem, index, array) { | ||
return array.indexOf(elem) === index; | ||
} | ||
module.exports = Liftoff; |
{ | ||
"name": "liftoff", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Launch your command line tool with ease.", | ||
@@ -20,3 +20,4 @@ "author": "Tyler Kellen (http://goingslowly.com/)", | ||
"pretest": "eslint .", | ||
"test": "mocha -t 5000 -b -R spec test/index", | ||
"test": "mocha -t 5000 -b -R spec test/index && npm run legacy-test", | ||
"legacy-test": "mocha -t 5000 -b -R spec test/launch", | ||
"cover": "nyc --reporter=lcov --reporter=text-summary npm test" | ||
@@ -23,0 +24,0 @@ }, |
124
README.md
@@ -265,6 +265,8 @@ <p align="center"> | ||
## launch(opts, callback(env)) | ||
Launches your application with provided options, builds an environment, and invokes your callback, passing the calculated environment as the first argument. | ||
### prepare(opts, callback(env)) | ||
##### Example Configuration w/ Options Parsing: | ||
Prepares the environment for your application with provided options, and invokes your callback with the calculated environment as the first argument. The environment can be modified before using it as the first argument to `execute`. | ||
**Example Configuration w/ Options Parsing:** | ||
```js | ||
@@ -274,8 +276,11 @@ const Liftoff = require('liftoff'); | ||
const argv = require('minimist')(process.argv.slice(2)); | ||
const invoke = function (env) { | ||
const onExecute = function (env, argv) { | ||
// Do post-execute things | ||
}; | ||
const onPrepare = function (env) { | ||
console.log('my environment is:', env); | ||
console.log('my cli options are:', argv); | ||
console.log('my liftoff config is:', this); | ||
MyApp.execute(env, onExecute); | ||
}; | ||
MyApp.launch({ | ||
MyApp.prepare({ | ||
cwd: argv.cwd, | ||
@@ -285,5 +290,39 @@ configPath: argv.myappfile, | ||
completion: argv.completion | ||
}, invoke); | ||
}, onPrepare); | ||
``` | ||
**Example w/ modified environment** | ||
```js | ||
const Liftoff = require('liftoff'); | ||
const Hacker = new Liftoff({ | ||
name: 'hacker', | ||
configFiles: { | ||
'.hacker': { | ||
home: { path: '.', cwd: '~' } | ||
} | ||
} | ||
}); | ||
const onExecute = function (env, argv) { | ||
// Do post-execute things | ||
}; | ||
const onPrepare = function (env) { | ||
env.configProps = ['home', 'cwd'].map(function(dirname) { | ||
return env.configFiles['.hacker'][dirname] | ||
}).filter(function(filePath) { | ||
return Boolean(filePath); | ||
}).reduce(function(config, filePath) { | ||
return mergeDeep(config, require(filePath)); | ||
}, {}); | ||
if (env.configProps.hackerfile) { | ||
env.configPath = path.resolve(env.configProps.hackerfile); | ||
env.configBase = path.dirname(env.configPath); | ||
} | ||
Hacker.execute(env, onExecute); | ||
}; | ||
Hacker.prepare({}, onPrepare); | ||
``` | ||
#### opts.cwd | ||
@@ -363,4 +402,62 @@ | ||
#### callback(env) | ||
A function called after your environment is prepared. A good place to modify the environment before calling `execute`. When invoked, `this` will be your instance of Liftoff. The `env` param will contain the following keys: | ||
- `cwd`: the current working directory | ||
- `require`: an array of modules that liftoff tried to pre-load | ||
- `configNameSearch`: the config files searched for | ||
- `configPath`: the full path to your configuration file (if found) | ||
- `configBase`: the base directory of your configuration file (if found) | ||
- `modulePath`: the full path to the local module your project relies on (if found) | ||
- `modulePackage`: the contents of the local module's package.json (if found) | ||
- `configFiles`: an object of filepaths for each found config file (filepath values will be null if not found) | ||
### execute(env, [forcedFlags], callback(env, argv)) | ||
A function to start your application, based on the `env` given. Optionally takes an array of `forcedFlags`, which will force a respawn with those node or V8 flags during startup. Invokes your callback with the environment and command-line arguments (minus node & v8 flags) after the application has been executed. | ||
**Example:** | ||
```js | ||
const Liftoff = require('liftoff'); | ||
const MyApp = new Liftoff({name:'myapp'}); | ||
const onExecute = function (env, argv) { | ||
// Do post-execute things | ||
console.log('my environment is:', env); | ||
console.log('my cli options are:', argv); | ||
console.log('my liftoff config is:', this); | ||
}; | ||
const onPrepare = function (env) { | ||
var forcedFlags = ['--trace-deprecation']; | ||
MyApp.execute(env, forcedFlags, onExecute); | ||
}; | ||
MyApp.prepare({}, onPrepare); | ||
``` | ||
#### callback(env, argv) | ||
A function called after your application is executed. When invoked, `this` will be your instance of Liftoff, `argv` will be all command-line arguments (minus node & v8 flags), and `env` will contain the following keys: | ||
- `cwd`: the current working directory | ||
- `require`: an array of modules that liftoff tried to pre-load | ||
- `configNameSearch`: the config files searched for | ||
- `configPath`: the full path to your configuration file (if found) | ||
- `configBase`: the base directory of your configuration file (if found) | ||
- `modulePath`: the full path to the local module your project relies on (if found) | ||
- `modulePackage`: the contents of the local module's package.json (if found) | ||
- `configFiles`: an object of filepaths for each found config file (filepath values will be null if not found) | ||
### launch(opts, callback(env, argv)) | ||
**Deprecated:** Please use `prepare` followed by `execute`. That's all this module does internally but those give you more control. | ||
Launches your application with provided options, builds an environment, and invokes your callback, passing the calculated environment and command-line arguments (minus node & v8 flags) as the arguments. | ||
Accepts any options that `prepare` allows, plus `opt.forcedFlags`. | ||
#### opts.forcedFlags | ||
**Deprecated:** If using `prepare`/`execute`, pass forcedFlags as the 2nd argument instead of using this option. | ||
Allows you to force node or V8 flags during the launch. This is useful if you need to make sure certain flags will always be enabled or if you need to enable flags that don't show up in `opts.v8flags` (as these flags aren't validated against `opts.v8flags`). | ||
@@ -385,15 +482,2 @@ | ||
#### callback(env) | ||
A function to start your application. When invoked, `this` will be your instance of Liftoff. The `env` param will contain the following keys: | ||
- `cwd`: the current working directory | ||
- `require`: an array of modules that liftoff tried to pre-load | ||
- `configNameSearch`: the config files searched for | ||
- `configPath`: the full path to your configuration file (if found) | ||
- `configBase`: the base directory of your configuration file (if found) | ||
- `modulePath`: the full path to the local module your project relies on (if found) | ||
- `modulePackage`: the contents of the local module's package.json (if found) | ||
- `configFiles`: an object of filepaths for each found config file (filepath values will be null if not found) | ||
### events | ||
@@ -400,0 +484,0 @@ |
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
32519
353
536
6
13