batch-import
Advanced tools
Comparing version 1.2.7 to 2.0.0
@@ -1,2 +0,2 @@ | ||
module.exports = function () { | ||
module.exports = function (app) { | ||
return { | ||
@@ -3,0 +3,0 @@ "xx": 666 |
module.exports = app => { | ||
return app.a | ||
return 888 | ||
} |
22
index.js
@@ -14,3 +14,3 @@ 'use strict'; | ||
/** | ||
* 模块递归遍历器 | ||
* 模块目录递归遍历器(当找不到目录时停止递归) | ||
* @param {*} $path 模块路径 | ||
@@ -25,11 +25,12 @@ * @param {*} option 选项 | ||
} catch (err) { | ||
// 找不到目录时停止递归 | ||
return | ||
} | ||
subPath.forEach(itemPath => { | ||
for (let itemPath of subPath) { | ||
let key = itemPath.indexOf('.js') | ||
let index = itemPath.indexOf('.js') | ||
// 文件类型 | ||
if (key > 0) { | ||
if (index > 0) { | ||
@@ -48,3 +49,3 @@ // 模块路径过滤 | ||
let filename = itemPath.slice(0, key) | ||
let filename = itemPath.slice(0, index) | ||
let filePath = path.join($path, itemPath) | ||
@@ -58,10 +59,5 @@ | ||
// 模块导出数据处理函数 | ||
// 模块导出数据处理函数,覆盖原始值 | ||
if (option.process) { | ||
let { error, data } = option.process(container[filename], this.container) | ||
if (error) { | ||
throw `${filePath}${error}` | ||
} else { | ||
container[filename] = data | ||
} | ||
container[filename] = option.process.call(this.container, filename, container[filename]) | ||
} | ||
@@ -79,3 +75,3 @@ | ||
}) | ||
} | ||
@@ -82,0 +78,0 @@ } |
{ | ||
"name": "batch-import", | ||
"version": "1.2.7", | ||
"version": "2.0.0", | ||
"description": "批量加载node.js模块、目录,并转换为对应的JS对象", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,1 +1,3 @@ | ||
# batch-import | ||
批量加载指定目录下node.js模块,并将模块导出结果保存到与模块路径结构一致的对象中 | ||
@@ -5,3 +7,3 @@ | ||
npm install batch-import --save | ||
npm install batch-import | ||
@@ -18,18 +20,21 @@ ### 使用方法 | ||
#### options *Object* | ||
* `options.path` *String* - 指定模块加载目录 | ||
* `path` *String* - 指定模块加载目录 | ||
* `options.contain` *Array* - 仅加载指定模块,不能与exclude同时使用 | ||
* `contain` *Array* - 仅加载指定模块,不能与exclude同时使用 | ||
* `options.exclude` *Array* - 排除指定模块,不能与contain同时使用 | ||
* `exclude` *Array* - 排除指定模块,不能与contain同时使用 | ||
* `options.process` *Function* - 模块导出数据处理函数,用于数据检验、预处理等操作。 | ||
* `process` *Function* - 模块导出数据处理函数,用于数据检验、预处理等操作。 | ||
输入参数1为模块输出用于加工的原始数据,输入参数2为输出容器对象,用于动态向container添加数据或访问已处理完成的依赖。 | ||
按照约定,process函数必须返回包含error和data属性的对象,error用于抛出错误信息,data输出正常结果。 | ||
* `container` *Object* - 指定一个容器用于存放模块导出结果,按照指定顺序加载模块,来解决依赖时序问题(可选) | ||
#### container(可选) | ||
#### process函数参数 | ||
> 指定一个容器用于存放模块导出结果,按照指定顺序加载模块,来解决依赖时序问题 | ||
> 函数中this指向根容器,按照约定,process函数必须返回包含error和data属性的对象,error用于抛出错误信息,data输出正常结果。 | ||
* `filename` *String* - 当前文件名称,不含后缀 | ||
* `data` * - 模块返回值 | ||
### 示例 | ||
@@ -47,5 +52,5 @@ | ||
"path": "models/", | ||
process(func, container) { | ||
process(filename, func) { | ||
if (typeof func === 'function') { | ||
return { data: func() } | ||
return func(this) | ||
} else { | ||
@@ -71,1 +76,7 @@ return { error: `模块输出数据类型必须为函数` } | ||
### 与1.x版本差异 | ||
* 2.x调整了process函数的入参,参考入参部分 | ||
* 取消process函数必须以{error、data}作为返回值的约定,不再使用模块内代理throw抛出异常,导致不能直接定位到错误 |
24
test.js
@@ -10,7 +10,7 @@ "use strict" | ||
// "exclude": ['c1/'], | ||
process(func, app) { | ||
if (typeof func === 'function') { | ||
return { data: func(app) } | ||
process(name, func) { | ||
if (func instanceof Function) { | ||
return func(this) | ||
} else { | ||
return { error: `模块输出数据类型必须为函数` } | ||
throw new Error(`控制器输出数据类型必须为函数`) | ||
} | ||
@@ -22,7 +22,7 @@ }, | ||
"exclude": ['c1'], | ||
process(func, app) { | ||
if (typeof func === 'function') { | ||
return { data: func(app) } | ||
process(name, func) { | ||
if (func instanceof Function) { | ||
return func(this) | ||
} else { | ||
return { error: `模块输出数据类型必须为函数` } | ||
throw new Error(`模型输出数据类型必须为函数`) | ||
} | ||
@@ -33,7 +33,7 @@ }, | ||
"path": "app/extend", | ||
process(func, app) { | ||
if (typeof func === 'function') { | ||
return { data: func(app) } | ||
process(name, func) { | ||
if (func instanceof Function) { | ||
return func(this) | ||
} else { | ||
return { error: `模块输出数据类型必须为函数` } | ||
throw new Error(`扩展输出数据类型必须为函数`) | ||
} | ||
@@ -40,0 +40,0 @@ }, |
6426
79
126