Skip to content

Commit

Permalink
增加了可以加载目录中的文件的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
ykan committed Mar 21, 2017
1 parent a7e0b1a commit f491ade
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
25 changes: 17 additions & 8 deletions index.js
@@ -1,16 +1,20 @@
'use strict';
const fs = require('fs');
const co = require('co');
const path = require('path');
//# 加载目录下的模块
// * args.path(*): 目录路径
// * args.deps(*): 依赖的数组
// * args.pre: map中的前缀
// * args.defaultFile: 默认加载的文件名
// * args.attach: 附加上的配置
// * args.shouldLoadFile: 选择加载目录还是文件
// return Promise.resolve(Map);
module.exports = function(args) {
let files = fs.readdirSync(args.path);
args.defaultFile = args.defaultFile || 'index';
let defaultFile = args.defaultFile || 'index';
let shouldLoadFile = !!args.shouldLoadFile;
let ctx = args.ctx || {};
return co(function*(){
let map = {};
let keys = [];
Expand All @@ -21,17 +25,22 @@ module.exports = function(args) {
let file = files[i];
let fullPath = `${args.path}/${file}`;
let stat = fs.lstatSync(fullPath);
if(stat.isDirectory()) {
let filePath = `${fullPath}/${args.defaultFile}.js`;
let filePath, modName;
if(!shouldLoadFile && stat.isDirectory()) {
filePath = `${fullPath}/${defaultFile}.js`;
modName = file;
} else if(shouldLoadFile && stat.isFile() && path.extname(file) === '.js') {
filePath = fullPath;
modName = file.replace(/\.js$/, '');
}
if(filePath) {
let modFunc = require(filePath);
let modObj = yield modFunc.apply({}, args.deps);
let name = modObj.name || file;
modObj.path = fullPath;
modObj.name = name;
let modObj = yield modFunc.apply(ctx, args.deps);
modObj.name = modName;
keys.forEach(key => {
modObj[key] = modObj[key] || args.attach[key];
});
map[name] = modObj;
map[modName] = modObj;
}
}
return map;
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "clmloader",
"version": "0.1.0",
"version": "0.2.0",
"description": "conglai module loader",
"scripts": {
"mocha": "NODE_ENV=test mocha tests/**/*.test.js -r ./tests/a.helper.js -R dot -w",
Expand Down
10 changes: 10 additions & 0 deletions tests/examples-2/router-12.js
@@ -0,0 +1,10 @@
'use strict';

module.exports = function(deps1, deps2) {
deps1.test +=1;
deps2.test +=2;
return Promise.resolve({
test: 2,
common: 'xxx'
});
};
9 changes: 9 additions & 0 deletions tests/examples-2/router.js
@@ -0,0 +1,9 @@
'use strict';

module.exports = function(deps1, deps2) {
deps1.test +=1;
deps2.test +=2;
return Promise.resolve({
test: 1
});
};
21 changes: 19 additions & 2 deletions tests/main.test.js
Expand Up @@ -16,7 +16,8 @@ describe('加载模块测试', () => {
dep2.test.should.be.equal(4);
});
});
it('修改默认地址', () => {

it('修改默认模块名', () => {
return co(function*(){
let dep1 = { test: 0 };
let dep2 = { test: 0 };
Expand All @@ -32,9 +33,25 @@ describe('加载模块测试', () => {
map['dir1'].common.should.be.equal('xx');
map['dir2'].common.should.be.equal('xxx');
map['dir2'].name.should.be.equal('dir2');
map['dir2'].path.should.be.equal(__dirname + '/examples-1/dir2');
dep1.test.should.be.equal(2);
dep2.test.should.be.equal(4);
});
});

it('加载文件模块', () => {
return co(function*(){
let dep1 = { test: 0 };
let dep2 = { test: 0 };
let map = yield loadMoudles({
path: __dirname + '/examples-2',
deps: [dep1, dep2],
defaultFile: 'router',
shouldLoadFile: true,
attach: {
common: 'xx'
}
});
map.should.have.keys('router', 'router-12');
});
});
});

0 comments on commit f491ade

Please sign in to comment.