batch-import
Advanced tools
Comparing version 3.1.1 to 3.2.0
### 3.1.1 | ||
不再强制覆盖已存在的节点,实现容器的增量混合。 | ||
不再强制覆盖已存在的节点,实现容器的增量混合。 | ||
### 3.2.0 | ||
* 代码优化 | ||
* 增加contain对子模块的深度匹配 | ||
* 增加contain对子目录的深度匹配 | ||
* 修复文件后缀名匹配错误的bug,如.js后缀会匹配到.json | ||
* 更新、完善测试用例,使用jtf驱动测试 |
122
index.js
'use strict'; | ||
let fs = require('fs') | ||
let path = require("path") | ||
const fs = require('fs') | ||
const path = require("path") | ||
const excludes = [".DS_Store", '.vscode', '.git', 'node_modules'] | ||
@@ -15,47 +16,55 @@ class Parser { | ||
* 模块目录递归遍历器(当找不到目录时停止递归) | ||
* @param {*} $path 模块路径 | ||
* @param {*} option 选项 | ||
* @param {*} container 模块导出容器 | ||
* @param {*} _path 模块路径 | ||
* @param {*} options 选项 | ||
*/ | ||
recursion($path, option, container) { | ||
recursion(_path, options) { | ||
const container = {} | ||
try { | ||
var subPath = fs.readdirSync($path) | ||
var readdir = fs.readdirSync(_path) | ||
} catch (error) { | ||
// 找不到目录时停止递归 | ||
return | ||
} | ||
for (let itemPath of subPath) { | ||
const { contain, exclude, ...other } = options | ||
// 目录、模块路径过滤 | ||
if (option.contain) { | ||
if (option.contain.indexOf(itemPath) === -1) { | ||
continue | ||
} | ||
} else if (option.exclude) { | ||
if (option.exclude.indexOf(itemPath) > -1) { | ||
continue | ||
} | ||
} | ||
for (const name of readdir) { | ||
let index = itemPath.indexOf('.js') | ||
// 内置的排除项 | ||
if (excludes.includes(name)) continue | ||
// js类型 | ||
if (index > 0) { | ||
const absolutePath = path.join(_path, name) | ||
let filename = itemPath.slice(0, index) | ||
let filePath = path.join($path, itemPath) | ||
const stat = fs.statSync(absolutePath) | ||
try { | ||
var result = require(filePath) | ||
} catch (error) { | ||
throw error | ||
// 文件类型 | ||
if (stat.isFile()) { | ||
if (contain) { | ||
if (!contain.includes(name)) continue | ||
} else if (exclude) { | ||
if (exclude.includes(name)) continue | ||
} | ||
// 模块导出数据处理函数,覆盖原始值 | ||
if (option.import) { | ||
container[filename] = option.import.call(container, filename, result) | ||
} else { | ||
container[filename] = result | ||
if (/\.js$/.test(name)) { | ||
const filePath = path.join(_path, name) | ||
try { | ||
var result = require(filePath) | ||
} catch (error) { | ||
throw error | ||
} | ||
const index = name.indexOf('.js') | ||
const filename = name.slice(0, index) | ||
// 模块导出数据处理函数,覆盖原始值 | ||
if (options.import) { | ||
container[filename] = options.import.call(container, filename, result) | ||
} else { | ||
container[filename] = result | ||
} | ||
} | ||
@@ -65,17 +74,20 @@ | ||
// 目录或其它类型 | ||
// 目录类型 | ||
else { | ||
// 跳过.DS_Store隐藏属性文件 | ||
if (itemPath === ".DS_Store") { | ||
continue | ||
let _options = options | ||
if (contain) { | ||
if (contain.includes(name)) { | ||
_options = other | ||
} | ||
} else if (exclude) { | ||
if (exclude.includes(name)) continue | ||
} | ||
if (!container[itemPath]) { | ||
container[itemPath] = {} | ||
const result = this.recursion(absolutePath, _options) | ||
if (result) { | ||
container[name] = result | ||
} | ||
let directoryPath = path.join($path, itemPath) | ||
this.recursion(directoryPath, option, container[itemPath]) | ||
} | ||
@@ -85,2 +97,6 @@ | ||
if (Object.keys(container).length) { | ||
return container | ||
} | ||
} | ||
@@ -100,19 +116,19 @@ | ||
let parser = new Parser(options, container); | ||
const parser = new Parser(options) | ||
let cwd = process.cwd() | ||
const cwd = process.cwd() | ||
for (let name in options) { | ||
for (const name in options) { | ||
if (!container[name]) { | ||
container[name] = {} | ||
const item = options[name] | ||
const directoryPath = path.join(cwd, item.path) | ||
const result = parser.recursion(directoryPath, item) | ||
if (result) { | ||
Object.assign(container, { [name]: result }) | ||
} | ||
let option = options[name] | ||
let directoryPath = path.join(cwd, option.path) | ||
parser.recursion(directoryPath, option, container[name]) | ||
// 导入完毕后的处理函数 | ||
if (option.complete) { | ||
container[name] = option.complete.call(container, container[name]) | ||
if (item.complete) { | ||
container[name] = item.complete.call(container, result || {}) | ||
} | ||
@@ -119,0 +135,0 @@ |
{ | ||
"name": "batch-import", | ||
"version": "3.1.1", | ||
"version": "3.2.0", | ||
"description": "批量加载node.js模块、目录,并转换为对应的JS对象", | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"typea": "^4.2.0" | ||
}, | ||
"scripts": { | ||
@@ -24,2 +26,2 @@ "dev": "jtf" | ||
"homepage": "https://github.com/xiangle/batch-import#readme" | ||
} | ||
} |
@@ -21,7 +21,7 @@ # batch-import | ||
* `path` *String* - 指定模块加载递归目录(必填) | ||
* `path` *String* - 模块所在相对路径(必填) | ||
* `contain` *Array* - 仅加载指定模块或目录,不能与exclude同时使用(可选) | ||
* `contain` *Array* - 加载指定模块或目录,支持深度递归匹配,与exclude互斥(可选) | ||
* `exclude` *Array* - 排除指定模块或目录,不能与contain同时使用(可选) | ||
* `exclude` *Array* - 排除指定模块或目录,支持深度递归匹配,与contain互斥(可选) | ||
@@ -28,0 +28,0 @@ * `import(filename, data)` *Function* - 模块导出数据处理函数,this指向当前层级容器。用于数据检验、预处理等操作(可选) |
6559
94
1