Skip to content

Commit

Permalink
DicRequireLoader - module prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
matuszeman committed Aug 11, 2016
1 parent 784cf25 commit 823b8b3
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 11 deletions.
10 changes: 10 additions & 0 deletions examples/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ const requireLoader = new DicRequireLoader({
}, dic);
requireLoader.enable();

//app config
dic.instance('./service-config', {
cache: {
ttl: 1000
},
consumer: {
my: 'APP options'
}
});

module.exports = {
dic
};
2 changes: 1 addition & 1 deletion examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { dic } = require('./bootstrap');
console.log('====================== APP =======================');//XXX

const fn = require('./services/function');
dic.alias('services/function', 'function');
//dic.alias('./services/function', 'function');

console.log('====================== RUN =======================');//XXX

Expand Down
3 changes: 2 additions & 1 deletion examples/services/cache.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const Cache = require('../src/cache');
module.exports = new Cache();
const config = require('./../service-config');
module.exports = new Cache(config.cache);
3 changes: 2 additions & 1 deletion examples/services/consumer.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
const Consumer = require('../src/consumer');
module.exports = new Consumer(require('./cache'));
const config = require('../service-config');
module.exports = new Consumer(config.consumer, require('./cache'));
6 changes: 5 additions & 1 deletion examples/src/cache.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module.exports = class Cache {
constructor(options) {
this.options = options;
}

*asyncInit() {
console.log('>>> Cache async initialization');//XXX
console.log('>>> Cache async init. Options: ', this.options);//XXX
this.dbConnection = true;
}

Expand Down
5 changes: 3 additions & 2 deletions examples/src/consumer.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module.exports = class Consumer {
constructor(cache) {
constructor(options, cache) {
this.options = options;
this.cache = cache;
this.init = false;
}

*asyncInit() {
console.log('>>> Consumer async initialization');//XXX
console.log('>>> Consumer async init. Options: ', this.options);//XXX
this.init = true;
}

Expand Down
5 changes: 4 additions & 1 deletion examples/test/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const { dic } = require('../bootstrap');

//test stubs replacing real application services
dic.instance('services/cache', {
dic.instance('./services/cache', {
get: function(key) {
return 'TEST CACHE VALUE';
}
});

const config = dic.get('./service-config');
config.consumer.my = 'TEST options';

before(function*() {
yield dic.asyncInit();
});
Expand Down
11 changes: 7 additions & 4 deletions src/dic-require-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ const path = require('path');

module.exports = class DicRequireLoader {
constructor(opts, dic) {
this.options = opts;
this.options = _.defaults(opts, {
modulePrefix: './'
});
this.dic = dic;
}

Expand All @@ -27,7 +29,7 @@ module.exports = class DicRequireLoader {
this.filename.indexOf('/node_modules/') !== -1 || //requires from system modules (mocha)
requestedModulePath.indexOf('/node_modules/') !== -1) { //app requires of system modules (mocha)
if (self.dic.has(requestedModulePath)) {
console.log(`DicRequireLoader: System module "${moduleId}" loaded from Dic [${this.filename}]`);//XXX
console.log(`DicRequireLoader: System module "${requestedModulePath}" loaded from Dic [${this.filename}]`);//XXX
return self.getInstance(requestedModulePath);
}

Expand All @@ -37,12 +39,13 @@ module.exports = class DicRequireLoader {

const currentDir = path.dirname(this.filename);
const modulePath = path.resolve(currentDir + '/' + requestedModulePath);
const moduleId = path.relative(self.options.rootDir, modulePath);
const relativeModulePath = path.relative(self.options.rootDir, modulePath);
const moduleId = self.options.modulePrefix + relativeModulePath;

if (self.options.exclude) {
for (const exc of self.options.exclude) {
//TODO this should be done much smarter - glob?
if (moduleId.indexOf(exc) === 0) {
if (relativeModulePath.indexOf(exc) === 0) {
console.log(`DicRequireLoader: Module "${moduleId}" excluded [${this.filename}]`);//XXX
return DicRequireLoader.originalRequire.apply(this, arguments);
}
Expand Down

0 comments on commit 823b8b3

Please sign in to comment.