pomelo-loader
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -43,3 +43,3 @@ /** | ||
var loadFile = function(fp, context) { | ||
var m = require(fp); | ||
var m = requireUncached(fp); | ||
@@ -128,2 +128,7 @@ if(!m) { | ||
return fn; | ||
}; | ||
}; | ||
var requireUncached = function(module){ | ||
delete require.cache[require.resolve(module)] | ||
return require(module) | ||
} |
{ | ||
"name": "pomelo-loader", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ }, |
#pomelo-loader - loader module for pomelo | ||
pomelo中使用Convention over Configuration的形式管理工程目录,不同的功能按约定放在不同的目录下。pomelo-loader为pomelo提供了按目录加载模块的功能。 | ||
pomelo-rpc可以批量加载指定目录下的模块,挂到一个空对象下返回(但不会递归加载子目录),同时提供模块命名机制。 | ||
Load codes for pomelo based on the convention over configuration rules. | ||
pomelo-rpc could load modules in batch but not load the sub-directory recursively. | ||
+ Tags: node.js | ||
##规则说明 | ||
模块命名 | ||
##Regulation | ||
Module name | ||
模块默认以文件名为名。如:加载lib/a.js后,返回的结果为:{a: require('./lib/a')}。 | ||
Module would use the filename by default. For example: load ```lib/a.js``` and the return result would be: ```{a: require('./lib/a')}``` | ||
如果模块中定义了name属性,则会以name作为模块的名称。如: | ||
It would use the name if the module with a name property. For example | ||
```javascript | ||
@@ -18,14 +20,15 @@ a.js | ||
``` | ||
返回的结果为:{test: require('./lib/a')} | ||
the return result would be: ```{test: require('./lib/a')}``` | ||
模块定义 | ||
Module definiation | ||
如果模块以function的形式暴露出去,则这个function会被当作构造模块实例的工厂方法,Loader会调用这个function获取模块的实例,同时可以传递一个context参数作为工厂方法的参数。其他情况则直接把加载到的模块返回。 | ||
If the module exported as a function, pomelo-loader would take it as a factory method and generate a new instance of module by calling the function. And it would return the module directly for other situation. | ||
```javascript | ||
module.exports = function(context) { | ||
return {}; // return some module instance | ||
return {}; // return some module instance | ||
}; | ||
``` | ||
##安装 | ||
##Installation | ||
``` | ||
@@ -35,3 +38,3 @@ npm install pomelo-loader | ||
##用法 | ||
##Usage | ||
``` javascript | ||
@@ -42,10 +45,9 @@ var Loader = require('pomelo-loader'); | ||
console.log('res: %j', res); | ||
``` | ||
模块定义成函数,加载 | ||
``` | ||
##API | ||
###Loader.load(path, context) | ||
加载path目录下所有模块。如果模本身是一个function,则把这个function作为获取模块的工厂方法,通过调用这个方法获取到模块实例;否则直接返回加载到的模块。 | ||
####参数 | ||
+ path 加载的目录 | ||
+ context 如果通过工厂方法加载模块,会将该参数作为初始化参数传给工厂方法。 | ||
Load all modules in the path. | ||
####Parameters | ||
+ path loaded path | ||
+ context if the module provides a factory method, the context would be pass as a parameter as the factory method. |
@@ -13,2 +13,3 @@ var Loader = require('../'); | ||
var services = Loader.load(path); | ||
console.log(services); | ||
should.exist(services); | ||
@@ -36,7 +37,11 @@ | ||
}); | ||
it('should invoke functions of loaded object successfully', function(done) { | ||
var callbackCount = 0, sid = 'area-server-1'; | ||
var context = {id: sid}; | ||
var callbackCount = 0, | ||
sid = 'area-server-1'; | ||
var context = { | ||
id: sid | ||
}; | ||
var services = Loader.load(path, context); | ||
console.log(services); | ||
should.exist(services); | ||
@@ -58,3 +63,3 @@ | ||
}); | ||
// context should be pass to factory function for each module | ||
@@ -65,3 +70,3 @@ services.whoAmIRemote.doService(function(err, res) { | ||
}); | ||
setTimeout(function() { | ||
@@ -72,3 +77,3 @@ callbackCount.should.equal(4); | ||
}); | ||
it('should throw an error if the path is empty', function() { | ||
@@ -78,5 +83,6 @@ var path = './mock-remote/connector'; | ||
Loader.load(path); | ||
}).should.throw(); | ||
}).should. | ||
throw (); | ||
}); | ||
it('should throw exception if the path dose not exist', function() { | ||
@@ -86,5 +92,6 @@ var path = './some/error/path'; | ||
Loader.loadPath(path); | ||
}).should.throw(); | ||
}).should. | ||
throw (); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
11340
16
222
51
5
1