batch-import
Advanced tools
Comparing version 1.2.2 to 1.2.3
141
index.js
@@ -6,92 +6,103 @@ 'use strict'; | ||
/** | ||
* | ||
* @param {Object} options 选项 | ||
* @param {Object} container 将模块挂载到指定容器 | ||
*/ | ||
module.exports = function (options, container = {}) { | ||
class Parser { | ||
if (!options instanceof Object) return | ||
constructor(options, container) { | ||
this.options = options | ||
this.container = container | ||
} | ||
let cwd = process.cwd() | ||
/** | ||
* 模块递归遍历器 | ||
* @param {*} path 模块路径 | ||
* @param {*} option 选项 | ||
* @param {*} container 模块导出容器 | ||
*/ | ||
recursion(path, option, container) { | ||
for (let name in options) { | ||
let option = options[name] | ||
container[name] = {} | ||
recursion(`${cwd}/${option.path}/`, option, container[name]) | ||
} | ||
try { | ||
var subPath = fs.readdirSync(path) | ||
} catch (err) { | ||
throw err | ||
} | ||
return container | ||
if (subPath) { | ||
} | ||
subPath.forEach(itemPath => { | ||
/** | ||
* 模块递归遍历器 | ||
* @param {*} path 模块路径 | ||
* @param {*} option 选项 | ||
* @param {*} container 模块导出容器 | ||
*/ | ||
function recursion(path, option, container) { | ||
let key = itemPath.indexOf('.js') | ||
try { | ||
var subPath = fs.readdirSync(path) | ||
} catch (err) { | ||
throw err | ||
} | ||
// 文件类型 | ||
if (key > 0) { | ||
if (subPath) { | ||
// 模块路径过滤 | ||
subPath.forEach(itemPath => { | ||
if (option.contain) { | ||
if (option.contain.indexOf(itemPath) === -1) { | ||
return | ||
} | ||
} else if (option.exclude) { | ||
if (option.exclude.indexOf(itemPath) === 1) { | ||
return | ||
} | ||
} | ||
let key = itemPath.indexOf('.js') | ||
let filename = itemPath.slice(0, key) | ||
let completePath = path + itemPath | ||
// 文件类型 | ||
if (key > 0) { | ||
// 模块路径过滤 | ||
if (option.contain) { | ||
if (option.contain.indexOf(itemPath) === -1) { | ||
return | ||
try { | ||
container[filename] = require(completePath) | ||
} catch (err) { | ||
throw err | ||
} | ||
} else if (option.exclude) { | ||
if (option.exclude.indexOf(itemPath) === 1) { | ||
return | ||
} | ||
} | ||
let filename = itemPath.slice(0, key) | ||
let completePath = path + itemPath | ||
// 模块导出数据过滤 | ||
try { | ||
container[filename] = require(completePath) | ||
} catch (err) { | ||
throw err | ||
} | ||
if (option.filter) { | ||
let { error, data } = option.filter(container[filename], this.container) | ||
if (error) { | ||
throw `${completePath}${error}` | ||
} else { | ||
container[filename] = data | ||
} | ||
// 模块导出数据过滤 | ||
} | ||
if (option.type) { | ||
if (typeof container[filename] !== option.type) { | ||
throw `${completePath}模块输出数据类型错误,只接受${option.type}类型` | ||
} | ||
} | ||
if (option.filter) { | ||
container[filename] = option.filter(container[filename], completePath) | ||
// 目录类型 | ||
else { | ||
if (itemPath === ".DS_Store") return | ||
container[itemPath] = {} | ||
this.recursion(path + itemPath + '/', option, container[itemPath]) | ||
} | ||
} | ||
}) | ||
// 目录类型 | ||
else { | ||
if (itemPath === ".DS_Store") return | ||
container[itemPath] = {} | ||
recursion(path + itemPath + '/', option, container[itemPath]) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
/** | ||
* | ||
* @param {Object} options 选项 | ||
* @param {Object} container 将模块挂载到指定容器 | ||
*/ | ||
module.exports = function (options, container = {}) { | ||
if (!options instanceof Object) return | ||
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]) | ||
} | ||
return container | ||
} |
module.exports = function () { | ||
console.log(11) | ||
return 666 | ||
} |
{ | ||
"name": "batch-import", | ||
"version": "1.2.2", | ||
"version": "1.2.3", | ||
"description": "批量加载node.js模块、目录,并转换为对应的JS对象", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
36
test.js
@@ -5,8 +5,3 @@ "use strict" | ||
let container = { | ||
// a: 1, | ||
// b: 2 | ||
} | ||
let modules = batchImport({ | ||
let app = batchImport({ | ||
"controllers": { | ||
@@ -16,10 +11,9 @@ "path": "controllers", | ||
// "exclude": ['c1/'], | ||
// filter(data, completePath) { | ||
// if (typeof data === 'function') { | ||
// return data | ||
// } else { | ||
// throw `${completePath}模块输出数据类型错误,只接受函数类型` | ||
// } | ||
// }, | ||
type: 'function' | ||
filter(func, app) { | ||
if (typeof func === 'function') { | ||
return { data: func(app) } | ||
} else { | ||
return { error: `模块输出数据类型必须为函数` } | ||
} | ||
}, | ||
}, | ||
@@ -29,12 +23,12 @@ "models": { | ||
"exclude": ['c1/'], | ||
filter(data, completePath) { | ||
if (typeof data === 'function') { | ||
return data | ||
filter(func, app) { | ||
if (typeof func === 'function') { | ||
return { data: func(app) } | ||
} else { | ||
throw `${completePath}模块输出数据类型错误,只接受函数类型` | ||
return { error: `模块输出数据类型必须为函数` } | ||
} | ||
} | ||
}, | ||
}, | ||
}, container) | ||
}) | ||
console.log(modules) | ||
console.log(app) |
5637
112