Skip to content

Commit

Permalink
Breaking: Utilize evented-require and emit-mapper modules to remove t…
Browse files Browse the repository at this point in the history
…his.requireLocal
  • Loading branch information
phated committed Apr 28, 2019
1 parent e7a969d commit 11ee36a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
44 changes: 20 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var flaggedRespawn = require('flagged-respawn');
var isPlainObject = require('is-plain-object');
var mapValues = require('object.map');
var fined = require('fined');
var reemit = require('emit-mapper');
var EventedRequire = require('evented-require');

var findCwd = require('./lib/find_cwd');
var findConfig = require('./lib/find_config');
Expand All @@ -18,23 +20,18 @@ var buildConfigName = require('./lib/build_config_name');
var registerLoader = require('./lib/register_loader');
var getNodeFlags = require('./lib/get_node_flags');

var preloadEvents = {
before: 'preload:before',
success: 'preload:success',
failure: 'preload:failure',
};

function Liftoff(opts) {
EE.call(this);
extend(this, parseOptions(opts));
}
util.inherits(Liftoff, EE);

Liftoff.prototype.requireLocal = function(module, basedir) {
try {
this.emit('preload:before', module);
var result = require(resolve.sync(module, { basedir: basedir }));
this.emit('preload:success', module, result);
return result;
} catch (e) {
this.emit('preload:failure', module, e);
}
};

Liftoff.prototype.buildEnvironment = function(opts) {
opts = opts || {};

Expand Down Expand Up @@ -175,6 +172,8 @@ Liftoff.prototype.prepare = function(opts, fn) {
};

Liftoff.prototype.execute = function(env, forcedFlags, fn) {
var self = this;

if (typeof forcedFlags === 'function') {
fn = forcedFlags;
forcedFlags = undefined;
Expand All @@ -183,34 +182,31 @@ Liftoff.prototype.execute = function(env, forcedFlags, fn) {
throw new Error('You must provide a callback function.');
}

var preloader = new EventedRequire(env.cwd);
reemit(preloader, self, preloadEvents);

this.handleFlags(function(err, flags) {
if (err) {
throw err;
}
flags = flags || [];

flaggedRespawn(flags, process.argv, forcedFlags, execute.bind(this));
flaggedRespawn(flags, process.argv, forcedFlags, execute);

function execute(ready, child, argv) {
if (child !== process) {
var execArgv = getNodeFlags.fromReorderedArgv(argv);
this.emit('respawn', execArgv, child);
self.emit('respawn', execArgv, child);
}
if (ready) {
preloadModules(this, env);
registerLoader(this, this.extensions, env.configPath, env.cwd);
fn.call(this, env, argv);
// TODO: Remove if unique filter stays in EventedRequire
preloader.requireAll(env.preload.filter(toUnique));
registerLoader(self, self.extensions, env.configPath, env.cwd);
fn.call(self, env, argv);
}
}
}.bind(this));
};

function preloadModules(inst, env) {
var basedir = env.cwd;
env.preload.filter(toUnique).forEach(function(module) {
inst.requireLocal(module, basedir);
});
}
};

function toUnique(elem, index, array) {
return array.indexOf(elem) === index;
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"cover": "nyc --reporter=lcov --reporter=text-summary npm test"
},
"dependencies": {
"emit-mapper": "^1.0.0",
"evented-require": "^1.0.0",
"extend": "^3.0.0",
"findup-sync": "^3.0.0",
"fined": "^1.0.1",
Expand Down
10 changes: 7 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ describe('Liftoff', function() {
});
});

describe('requireLocal', function() {
describe('events: preload:*', function() {

it('should attempt pre-loading local modules if they are requested', function(done) {
var app = new Liftoff({ name: 'test' });
Expand Down Expand Up @@ -357,7 +357,9 @@ describe('Liftoff', function() {
expect(isEmittedBeforeRequired).to.equal(true);
done();
});
requireTest.requireLocal('mocha', __dirname);
requireTest.prepare({ preload: ['mocha'] }, function(env) {
requireTest.execute(env, function() {});
});
});

it('should emit `preload:before` and `preload:failure` with an error if a module can\'t be found.', function(done) {
Expand All @@ -372,7 +374,9 @@ describe('Liftoff', function() {
expect(isEmittedBeforeRequired).to.equal(true);
done();
});
requireFailTest.requireLocal('badmodule', __dirname);
requireFailTest.prepare({ preload: ['badmodule'] }, function(env) {
requireFailTest.execute(env, function() {});
});
});

});
Expand Down

0 comments on commit 11ee36a

Please sign in to comment.