batch-import
Advanced tools
Comparing version 1.2.4 to 1.2.5
26
index.js
@@ -15,12 +15,13 @@ 'use strict'; | ||
* 模块递归遍历器 | ||
* @param {*} path 模块路径 | ||
* @param {*} $path 模块路径 | ||
* @param {*} option 选项 | ||
* @param {*} container 模块导出容器 | ||
*/ | ||
recursion(path, option, container) { | ||
recursion($path, option, container) { | ||
try { | ||
var subPath = fs.readdirSync(path) | ||
console.log($path) | ||
var subPath = fs.readdirSync($path) | ||
} catch (err) { | ||
throw err | ||
// throw err | ||
} | ||
@@ -50,6 +51,6 @@ | ||
let filename = itemPath.slice(0, key) | ||
let completePath = path + itemPath | ||
let filePath = path.join($path, itemPath) | ||
try { | ||
container[filename] = require(completePath) | ||
container[filename] = require(filePath) | ||
} catch (err) { | ||
@@ -59,4 +60,3 @@ throw err | ||
// 模块导出数据过滤 | ||
// 模块导出数据处理函数 | ||
if (option.process) { | ||
@@ -77,3 +77,4 @@ let { error, data } = option.process(container[filename], this.container) | ||
container[itemPath] = {} | ||
this.recursion(path + itemPath + '/', option, container[itemPath]) | ||
let directoryPath = path.join($path, itemPath) | ||
this.recursion(directoryPath, option, container[itemPath]) | ||
} | ||
@@ -98,10 +99,11 @@ | ||
let parser = new Parser(options, container); | ||
let cwd = process.cwd() | ||
let parser = new Parser(options, container); | ||
for (let name in options) { | ||
let option = options[name] | ||
container[name] = {} | ||
parser.recursion(`${cwd}/${option.path}/`, option, container[name]) | ||
let directoryPath = path.join(cwd, option.path) | ||
parser.recursion(directoryPath, option, container[name]) | ||
} | ||
@@ -108,0 +110,0 @@ |
{ | ||
"name": "batch-import", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"description": "批量加载node.js模块、目录,并转换为对应的JS对象", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,6 +9,8 @@ 批量加载指定目录下node.js模块,并将模块导出结果保存到与模块路径结构一致的对象中 | ||
let batchImport = require('batch-import') | ||
let modules = batchImport(options, container) | ||
```js | ||
let batchImport = require('batch-import') | ||
let modules = batchImport(options, container) | ||
``` | ||
### 参数 | ||
@@ -24,34 +26,37 @@ | ||
* `process` *Function* - 模块导出数据处理函数,用于数据检验、预处理等操作 | ||
* `process` *Function* - 模块导出数据处理函数,用于数据检验、预处理等操作。 | ||
输入参数1为模块输出用于加工的原始数据,输入参数2为输出容器对象,用于动态向container添加数据或访问已处理完成的依赖。 | ||
返回包含error对象用来抛出并定位异常,data属性导出正常结果。 | ||
#### container(可选) | ||
> 将模块导出结果添加到指定对象容器。 | ||
> 指定一个容器用于存放模块导出结果,按照指定顺序加载模块,来解决依赖时序问题 | ||
### 示例 | ||
let batchImport = require('batch-import') | ||
```js | ||
let batchImport = require('batch-import') | ||
let modules = batchImport({ | ||
"middleware": { | ||
"path": "middleware/", | ||
"exclude": ["test.js"], | ||
}, | ||
"models": { | ||
"path": "models/", | ||
item(data, completePath) { | ||
if (typeof data === 'function') { | ||
return data | ||
} else { | ||
throw `${completePath}模块输出数据类型错误,只接受函数类型` | ||
} | ||
let modules = batchImport({ | ||
"middleware": { | ||
"path": "middleware/", | ||
"exclude": ["test.js"], | ||
}, | ||
"models": { | ||
"path": "models/", | ||
process(func, container) { | ||
if (typeof func === 'function') { | ||
return { data: func() } | ||
} else { | ||
return { error: `模块输出数据类型必须为函数` } | ||
} | ||
}, | ||
"controllers": { | ||
"path": "controllers/", | ||
"contain": ["_route.js"], | ||
}, | ||
}) | ||
}, | ||
}, | ||
"controllers": { | ||
"path": "controllers/", | ||
"contain": ["_route.js"], | ||
}, | ||
}) | ||
``` | ||
### 注意事项 | ||
@@ -58,0 +63,0 @@ |
28
test.js
@@ -5,8 +5,12 @@ "use strict" | ||
let app = batchImport({ | ||
let app = { | ||
a: 666 | ||
} | ||
let container = batchImport({ | ||
"controllers": { | ||
"path": "controllers", | ||
"path": "app/controllers", | ||
// "contain": ["_route.js"], | ||
// "exclude": ['c1/'], | ||
process(func, app) { | ||
process(func, container) { | ||
if (typeof func === 'function') { | ||
@@ -20,5 +24,5 @@ return { data: func(app) } | ||
"models": { | ||
"path": "models", | ||
"exclude": ['c1/'], | ||
process(func, app) { | ||
"path": "app/models", | ||
"exclude": ['c1'], | ||
process(func) { | ||
if (typeof func === 'function') { | ||
@@ -31,4 +35,14 @@ return { data: func(app) } | ||
}, | ||
"extend": { | ||
"path": "app/extend", | ||
process(func) { | ||
if (typeof func === 'function') { | ||
return { data: func(app) } | ||
} else { | ||
return { error: `模块输出数据类型必须为函数` } | ||
} | ||
}, | ||
}, | ||
}) | ||
console.log(app) | ||
console.log(container) |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
6339
132
68