Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify for applying configs #95

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ The [`fined`](https://github.com/js-cli/fined) module accepts a string represent
Type: `Boolean`
Default: `false`

* `onFound`

The function which is executed after finding the file and requiring its loader, if the file was found. The parameters of the function is as follows:

| Parameter | Type | Description |
|:----------|:------:|:--------------------------------------|
| *name* | string | The unique name of the found path |
| *path* | string | The found path |

Type: `Functon`
Default: `null`

* `order`

The order of finding the file and post-find processing.

Type: `Number`
Default: `null`

**Examples:**

In this example Liftoff will look for the `.hacker.js` file relative to the `cwd` as declared in `configFiles`.
Expand Down Expand Up @@ -262,6 +281,44 @@ const MyApp = new Liftoff({
});
```

In this example, Liftoff will find files in specified `order`, and execute `onFound` functions for found paths.

```js
const MyApp = new Liftoff({
name: 'hacker',
configFiles: {
'.hacker': {
home: {
path: '~',
order: 2,
onFound: function(name, path) {
// This function be executed second.
// name => 'home'
// path => '/path/to/home/.hacker.js'
},
},
cwd: {
path: '.',
order: 1,
onFound: function(name, path) {
// The function Will be executed first.
// name => 'cwd'
// path => '/path/to/cwd/.hacker.js'
},
},
default: {
path: '/path/to/defaultdir',
onFound: function(name, path) {
// This function will be executed last when `order` is not specified.
// name => 'default'
// path => '/path/to/defaultdir/.hacker.js'
},
},
}
}
});
```

## 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.

Expand Down
49 changes: 10 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ const EE = require('events').EventEmitter;
const extend = require('extend');
const resolve = require('resolve');
const flaggedRespawn = require('flagged-respawn');
const isPlainObject = require('is-plain-object');
const mapValues = require('object.map');
const fined = require('fined');

const findCwd = require('./lib/find_cwd');
const findConfig = require('./lib/find_config');
const fileSearch = require('./lib/file_search');
const parseOptions = require('./lib/parse_options');
const silentRequire = require('./lib/silent_require');
const buildConfigName = require('./lib/build_config_name');
const registerLoader = require('./lib/register_loader');
const getNodeFlags = require('./lib/get_node_flags');
const prepareConfig = require('./lib/prepare_config');
const processConfigFiles = require('./lib/process_config_files');


function Liftoff (opts) {
Expand Down Expand Up @@ -115,34 +113,8 @@ Liftoff.prototype.buildEnvironment = function (opts) {
}
}

// load any modules which were requested to be required
if (preload.length) {
// unique results first
preload.filter(function (value, index, self) {
return self.indexOf(value) === index;
}).forEach(function (dep) {
this.requireLocal(dep, findCwd(opts));
}, this);
}

var exts = this.extensions;
var eventEmitter = this;
registerLoader(eventEmitter, exts, configPath, cwd);

var configFiles = {};
if (isPlainObject(this.configFiles)) {
var notfound = { path: null };
configFiles = mapValues(this.configFiles, function(prop, name) {
var defaultObj = { name: name, cwd: cwd, extensions: exts };
return mapValues(prop, function(pathObj) {
var found = fined(pathObj, defaultObj) || notfound;
if (isPlainObject(found.extension)) {
registerLoader(eventEmitter, found.extension, found.path, cwd);
}
return found.path;
});
});
}
var defaults = { cwd: cwd, extensions: this.extensions };
var configFiles = processConfigFiles(this.configFiles, defaults, this);

return {
cwd: cwd,
Expand Down Expand Up @@ -178,11 +150,6 @@ Liftoff.prototype.launch = function (opts, fn) {
}
process.title = this.processTitle;

var completion = opts.completion;
if (completion && this.completions) {
return this.completions(completion);
}

this.handleFlags(function (err, flags) {
if (err) {
throw err;
Expand All @@ -191,6 +158,11 @@ Liftoff.prototype.launch = function (opts, fn) {

var env = this.buildEnvironment(opts);

var completion = opts.completion;
if (completion && this.completions) {
return this.completions(completion);
}

var forcedFlags = getNodeFlags.arrayOrFunction(opts.forcedFlags, env);
flaggedRespawn(flags, process.argv, forcedFlags, execute.bind(this));

Expand All @@ -200,12 +172,11 @@ Liftoff.prototype.launch = function (opts, fn) {
this.emit('respawn', execArgv, child);
}
if (ready) {
prepareConfig.call(this, env, opts);
fn.call(this, env, argv);
}
}
}.bind(this));
};



module.exports = Liftoff;
19 changes: 19 additions & 0 deletions lib/prepare_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const registerLoader = require('./register_loader');
const findCwd = require('./find_cwd');

function prepareConfig(env, opts) {
const self = this;

const basedir = findCwd(opts);
env.require.filter(toUnique).forEach(function(module) {
self.requireLocal(module, basedir);
});

registerLoader(self, self.extensions, env.configPath, env.cwd);
}

function toUnique(elem, index, array) {
return array.indexOf(elem) === index;
}

module.exports = prepareConfig;
48 changes: 48 additions & 0 deletions lib/process_config_files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const sortBy = require('array-sort');
const isPlainObject = require('is-plain-object');
const mapValues = require('object.map');
const fined = require('fined');

const registerLoader = require('./register_loader');

const notFound = { path: null };

function processConfigFiles(configFiles, defaults, emitter) {
if (!isPlainObject(configFiles)) {
return {};
}

return mapValues(configFiles, function(propObj, propName) {
var defaultObj = {
name: propName,
cwd: defaults.cwd,
extensions: defaults.extensions,
};

return sortBy(objectToArray(propObj), 'value.order')
.reduce(function(rtnObj, elem) {
var pathName = elem.key;
var pathObj = elem.value;

var found = fined(pathObj, defaultObj) || notFound;
if (isPlainObject(found.extension)) {
registerLoader(emitter, found.extension, found.path, defaults.cwd);
}

if (found.path && typeof pathObj.onFound === 'function') {
pathObj.onFound.call(emitter, pathName, found.path);
}

rtnObj[pathName] = found.path;
return rtnObj;
}, {});
});
}

function objectToArray(obj) {
return Object.keys(obj).map(function(key) {
return { key: key, value: obj[key] };
});
}

module.exports = processConfigFiles;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"cover": "nyc --reporter=lcov --reporter=text-summary npm test"
},
"dependencies": {
"array-sort": "^1.0.0",
"extend": "^3.0.0",
"findup-sync": "^2.0.0",
"fined": "^1.0.1",
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/respawn_and_require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Liftoff = require('../..');

const Test = new Liftoff({
name: 'test',
v8flags: ['--harmony'],
});

Test.on('respawn', function(flags, proc) {
console.log('saw respawn', flags);
});

Test.on('require', function(name) {
console.log('require', name);
});

Test.launch({
require: 'coffeescript/register',
forcedFlags: ['--lazy'],
}, function(env, argv) {
console.log('execute');
});
Loading