mm_scanner 3.0.0 → 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/example/demo.js +33 -45
- package/index.js +23 -28
- package/lib/diff_util.js +11 -17
- package/lib/path_util.js +13 -32
- package/lib/scanner.js +260 -367
- package/lib/type_store.js +56 -0
- package/package.json +12 -6
- package/tool/scan/index.js +105 -0
- package/tool/scan/tool.json +57 -0
- package/tool/scanner_admin/index.js +88 -0
- package/tool/scanner_admin/tool.json +39 -0
- package/lib/cache_store.js +0 -151
- package/lib/fs_util.js +0 -50
- package/lib/scanner_config.js +0 -16
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
let { storeAdmin } = require('mm_store');
|
|
4
|
+
let { constAdmin } = require('mm_consts');
|
|
5
|
+
let { varAdmin } = require('mm_vars');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 初始化类型存储配置常量
|
|
9
|
+
* @description 主键为文件绝对路径,名称字段为文件名
|
|
10
|
+
* @returns {void}
|
|
11
|
+
*/
|
|
12
|
+
function initStoreConfig() {
|
|
13
|
+
let consts = constAdmin('scanner');
|
|
14
|
+
if (!consts.has('STORE_CONFIG')) {
|
|
15
|
+
consts.set('STORE_CONFIG', { key: 'file', name: 'name' });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
initStoreConfig();
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 获取本模块使用过的类型集合
|
|
23
|
+
* @description 集合保存在 mm_vars 实例池中,随作用域实例统一管理,避免模块级散落状态
|
|
24
|
+
* @returns {Set<string>} 类型集合
|
|
25
|
+
*/
|
|
26
|
+
function typeSet() {
|
|
27
|
+
let vars = varAdmin('mm_scanner');
|
|
28
|
+
if (!vars.has('TYPES')) {
|
|
29
|
+
vars.set('TYPES', new Set());
|
|
30
|
+
}
|
|
31
|
+
return vars.get('TYPES');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 获取资源类型对应的存储实例
|
|
36
|
+
* @description 一类一个 Store,作用域即资源类型名;配置由本模块统一给出,
|
|
37
|
+
* 避免调用方各自传参导致 mm_store 按作用域缓存实例时配置不一致
|
|
38
|
+
* @param {string} type 资源类型
|
|
39
|
+
* @returns {object} mm_store 实例
|
|
40
|
+
*/
|
|
41
|
+
function typeStore(type) {
|
|
42
|
+
typeSet().add(type);
|
|
43
|
+
return storeAdmin(type, constAdmin('scanner').get('STORE_CONFIG'));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 清空本模块登记过的全部类型 Store
|
|
48
|
+
* @returns {void}
|
|
49
|
+
*/
|
|
50
|
+
function clearTypeStores() {
|
|
51
|
+
for (let type of typeSet()) {
|
|
52
|
+
typeStore(type).clear();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = { typeStore, clearTypeStores };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_scanner",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "5.0.1",
|
|
4
|
+
"description": "目录扫描器,按资源类型表递归扫描目录并在扫描时完成分类,一类一个 Store 写入 mm_store,支持按类型取文件切片,默认忽略 .git/node_modules 等常见目录,并支持接收监听变更做增量更新。",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://gitee.com/qiuwenwu91/mm_scanner.git"
|
|
@@ -15,14 +15,15 @@
|
|
|
15
15
|
"文件检索",
|
|
16
16
|
"scanner"
|
|
17
17
|
],
|
|
18
|
-
"layer": "
|
|
19
|
-
"group": "
|
|
18
|
+
"layer": "util",
|
|
19
|
+
"group": "io",
|
|
20
20
|
"author": "邱文武",
|
|
21
21
|
"license": "ISC",
|
|
22
22
|
"main": "index.js",
|
|
23
23
|
"files": [
|
|
24
24
|
"index.js",
|
|
25
25
|
"lib",
|
|
26
|
+
"tool",
|
|
26
27
|
"example"
|
|
27
28
|
],
|
|
28
29
|
"directories": {
|
|
@@ -35,10 +36,15 @@
|
|
|
35
36
|
"prepublishOnly": "npm test"
|
|
36
37
|
},
|
|
37
38
|
"dependencies": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
39
|
+
"mm_admin": "^1.1.0",
|
|
40
|
+
"mm_consts": "^1.0.2",
|
|
41
|
+
"mm_store": "^1.0.1",
|
|
42
|
+
"mm_vars": "^1.1.0"
|
|
40
43
|
},
|
|
41
44
|
"devDependencies": {
|
|
42
45
|
"jest": "^30.5.2"
|
|
46
|
+
},
|
|
47
|
+
"overrides": {
|
|
48
|
+
"mm_persist": "0.0.0-stage"
|
|
43
49
|
}
|
|
44
50
|
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* scan —— 目录扫描工具
|
|
4
|
+
* 调用 mm_scanner 按资源类型表扫描目录,或查看 / 重新扫描已扫描目录。
|
|
5
|
+
*/
|
|
6
|
+
let { scannerAdmin } = require('../../index.js');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 扫描、查看或重新扫描目录
|
|
10
|
+
* @param {object} params 参数:action/dir/type/resources/ignore
|
|
11
|
+
* @param {object} ctx 执行上下文
|
|
12
|
+
* @returns {Promise<object>} 执行结果
|
|
13
|
+
*/
|
|
14
|
+
async function main(params, ctx, options) {
|
|
15
|
+
void options;
|
|
16
|
+
let p = params || {};
|
|
17
|
+
let action = p.action || 'scan';
|
|
18
|
+
/**
|
|
19
|
+
* 写入执行结果到上下文并返回。
|
|
20
|
+
* @param {object} data - 结果数据
|
|
21
|
+
* @returns {object} 结果数据本身
|
|
22
|
+
*/
|
|
23
|
+
const done = (data) => {
|
|
24
|
+
if (ctx) ctx.output = data;
|
|
25
|
+
return data;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
if (action === 'scan') {
|
|
30
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
31
|
+
let config = { resources: p.resources, ignore: p.ignore };
|
|
32
|
+
let scanner = scannerAdmin(p.dir, config);
|
|
33
|
+
scanner.scan(p.dir, config);
|
|
34
|
+
return done({
|
|
35
|
+
code: 0,
|
|
36
|
+
msg: '扫描完成',
|
|
37
|
+
action,
|
|
38
|
+
dir: scanner.baseDir,
|
|
39
|
+
count: scanner.size,
|
|
40
|
+
files: scanner.getAll()
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (action === 'rescan') {
|
|
45
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
46
|
+
let scanner = scannerAdmin(p.dir);
|
|
47
|
+
scanner.rescan();
|
|
48
|
+
return done({
|
|
49
|
+
code: 0,
|
|
50
|
+
msg: '重新扫描完成',
|
|
51
|
+
action,
|
|
52
|
+
dir: scanner.baseDir,
|
|
53
|
+
count: scanner.size
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (action === 'list') {
|
|
58
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
59
|
+
let scanner = scannerAdmin(p.dir);
|
|
60
|
+
let files = scanner.getAll();
|
|
61
|
+
if (p.type) {
|
|
62
|
+
files = files.filter((item) => item.type === p.type);
|
|
63
|
+
}
|
|
64
|
+
return done({
|
|
65
|
+
code: 0,
|
|
66
|
+
msg: '获取成功',
|
|
67
|
+
action,
|
|
68
|
+
dir: scanner.baseDir,
|
|
69
|
+
count: files.length,
|
|
70
|
+
files
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (action === 'apply') {
|
|
75
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
76
|
+
if (!Array.isArray(p.changes)) throw new TypeError('changes 必须是数组');
|
|
77
|
+
let scanner = scannerAdmin(p.dir);
|
|
78
|
+
let changes = scanner.apply(p.changes);
|
|
79
|
+
return done({
|
|
80
|
+
code: 0,
|
|
81
|
+
msg: '增量更新完成',
|
|
82
|
+
action,
|
|
83
|
+
dir: scanner.baseDir,
|
|
84
|
+
count: changes.length,
|
|
85
|
+
changes
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (action === 'config') {
|
|
90
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
91
|
+
let scanner = scannerAdmin(p.dir);
|
|
92
|
+
return done({ code: 0, msg: '获取成功', action, dir: scanner.baseDir, config: scanner.config });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
96
|
+
} catch (error) {
|
|
97
|
+
return done({
|
|
98
|
+
code: 10000,
|
|
99
|
+
msg: '执行失败:' + error.message,
|
|
100
|
+
error: { name: error.name, message: error.message }
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
module.exports = { main };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "scan",
|
|
3
|
+
"title": "目录扫描",
|
|
4
|
+
"description": "调用 mm_scanner 按资源类型表递归扫描目录并分类,或查看 / 重新扫描 / 增量更新已扫描目录、读取扫描配置。适用场景:加载配置与脚本前获取文件清单、按类型筛选资源、热更新后强制重扫或按变更增量更新、确认资源类型与忽略目录。工作流提示:scan 需传 dir,list/rescan/apply/config 需传已扫描过的 dir;type 用于 list 时按资源类型过滤,apply 需传 changes。",
|
|
5
|
+
"category": "资源管理",
|
|
6
|
+
"group": "resources",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"properties": {
|
|
12
|
+
"action": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "操作类型: scan 扫描 / list 查看 / rescan 重新扫描 / apply 增量更新 / config 查看配置",
|
|
15
|
+
"enum": ["scan", "list", "rescan", "apply", "config"],
|
|
16
|
+
"default": "scan"
|
|
17
|
+
},
|
|
18
|
+
"dir": {
|
|
19
|
+
"type": "string",
|
|
20
|
+
"description": "扫描目录(scan/list/rescan/apply/config 必填)"
|
|
21
|
+
},
|
|
22
|
+
"changes": {
|
|
23
|
+
"type": "array",
|
|
24
|
+
"description": "监听变更列表(apply 必填),元素为 { type, path },type 为 add/set/del/reload",
|
|
25
|
+
"items": { "type": "object" }
|
|
26
|
+
},
|
|
27
|
+
"type": {
|
|
28
|
+
"type": "string",
|
|
29
|
+
"description": "资源类型,仅 list 时用于过滤"
|
|
30
|
+
},
|
|
31
|
+
"resources": {
|
|
32
|
+
"type": "object",
|
|
33
|
+
"description": "资源类型表,键为类型名,值为 { ext },仅 scan 首次创建实例时生效"
|
|
34
|
+
},
|
|
35
|
+
"ignore": {
|
|
36
|
+
"type": "array",
|
|
37
|
+
"items": { "type": "string" },
|
|
38
|
+
"description": "忽略的目录名列表,与默认忽略目录取并集"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"required": ["action", "dir"]
|
|
42
|
+
},
|
|
43
|
+
"returns": {
|
|
44
|
+
"type": "object",
|
|
45
|
+
"properties": {
|
|
46
|
+
"code": { "type": "number", "description": "0 表示成功" },
|
|
47
|
+
"msg": { "type": "string", "description": "结果描述" },
|
|
48
|
+
"dir": { "type": "string", "description": "扫描根目录绝对路径" },
|
|
49
|
+
"count": { "type": "number", "description": "文件或变更数量" },
|
|
50
|
+
"files": { "type": "array", "description": "扫描条目数组" },
|
|
51
|
+
"changes": { "type": "array", "description": "标准化后的增量变更数组(apply)" },
|
|
52
|
+
"config": { "type": "object", "description": "扫描器配置(config)" },
|
|
53
|
+
"error": { "type": "object", "description": "异常信息" }
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"handler": "manager"
|
|
57
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* scanner_admin —— 扫描器实例管理工具
|
|
4
|
+
* 管理 mm_scanner 的实例池与类型 Store 缓存,不执行具体扫描。
|
|
5
|
+
*/
|
|
6
|
+
const path = require('path');
|
|
7
|
+
let { admin, Scanner } = require('../../index.js');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 获取实例键
|
|
11
|
+
* @param {object} p 参数
|
|
12
|
+
* @returns {string} 绝对路径
|
|
13
|
+
*/
|
|
14
|
+
function keyOf(p) {
|
|
15
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
16
|
+
return path.resolve(p.dir);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 管理扫描器实例池
|
|
21
|
+
* @param {object} params 参数:action/dir
|
|
22
|
+
* @param {object} ctx 执行上下文
|
|
23
|
+
* @returns {Promise<object>} 执行结果
|
|
24
|
+
*/
|
|
25
|
+
async function main(params, ctx, options) {
|
|
26
|
+
void options;
|
|
27
|
+
let p = params || {};
|
|
28
|
+
let action = p.action || 'list';
|
|
29
|
+
/**
|
|
30
|
+
* 写入执行结果到上下文并返回。
|
|
31
|
+
* @param {object} data - 结果数据
|
|
32
|
+
* @returns {object} 结果数据本身
|
|
33
|
+
*/
|
|
34
|
+
const done = (data) => {
|
|
35
|
+
if (ctx) ctx.output = data;
|
|
36
|
+
return data;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
if (action === 'list') {
|
|
41
|
+
let dirs = [...admin.keys()];
|
|
42
|
+
return done({ code: 0, msg: '获取成功', action, count: dirs.length, dirs });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (action === 'inspect') {
|
|
46
|
+
let key = keyOf(p);
|
|
47
|
+
let exists = admin.has(key);
|
|
48
|
+
if (!exists) {
|
|
49
|
+
return done({ code: 0, msg: '实例不存在', action, dir: key, exists: false });
|
|
50
|
+
}
|
|
51
|
+
let scanner = admin.get(key);
|
|
52
|
+
return done({
|
|
53
|
+
code: 0,
|
|
54
|
+
msg: '获取成功',
|
|
55
|
+
action,
|
|
56
|
+
dir: key,
|
|
57
|
+
exists: true,
|
|
58
|
+
count: scanner.size,
|
|
59
|
+
config: scanner.config
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (action === 'del') {
|
|
64
|
+
let key = keyOf(p);
|
|
65
|
+
return done({ code: 0, msg: '删除成功', action, dir: key, ok: admin.del(key) });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (action === 'clear') {
|
|
69
|
+
admin.clear();
|
|
70
|
+
return done({ code: 0, msg: '实例池已清空', action });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (action === 'clear_cache') {
|
|
74
|
+
Scanner.clearCache();
|
|
75
|
+
return done({ code: 0, msg: '类型 Store 已清空', action });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
79
|
+
} catch (error) {
|
|
80
|
+
return done({
|
|
81
|
+
code: 10000,
|
|
82
|
+
msg: '执行失败:' + error.message,
|
|
83
|
+
error: { name: error.name, message: error.message }
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
module.exports = { main };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "scanner_admin",
|
|
3
|
+
"title": "扫描器实例管理",
|
|
4
|
+
"description": "管理 mm_scanner 的实例池与类型 Store 缓存:列出已扫描目录、查看实例状态、删除实例、清空实例池或清空扫描缓存。适用场景:排查扫描实例未创建、清理热更新残留、释放目录扫描状态。",
|
|
5
|
+
"category": "资源管理",
|
|
6
|
+
"group": "resources",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"properties": {
|
|
12
|
+
"action": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "操作类型: list 实例列表 / inspect 查看实例 / del 删除实例 / clear 清空实例池 / clear_cache 清空扫描缓存",
|
|
15
|
+
"enum": ["list", "inspect", "del", "clear", "clear_cache"],
|
|
16
|
+
"default": "list"
|
|
17
|
+
},
|
|
18
|
+
"dir": {
|
|
19
|
+
"type": "string",
|
|
20
|
+
"description": "扫描目录(inspect/del 必填)"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"required": ["action"]
|
|
24
|
+
},
|
|
25
|
+
"returns": {
|
|
26
|
+
"type": "object",
|
|
27
|
+
"properties": {
|
|
28
|
+
"code": { "type": "number", "description": "0 表示成功" },
|
|
29
|
+
"msg": { "type": "string", "description": "结果描述" },
|
|
30
|
+
"count": { "type": "number", "description": "实例或文件数量" },
|
|
31
|
+
"dirs": { "type": "array", "items": { "type": "string" }, "description": "实例目录列表" },
|
|
32
|
+
"exists": { "type": "boolean", "description": "实例是否存在" },
|
|
33
|
+
"config": { "type": "object", "description": "扫描器配置" },
|
|
34
|
+
"ok": { "type": "boolean", "description": "操作是否成功" },
|
|
35
|
+
"error": { "type": "object", "description": "异常信息" }
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"handler": "manager"
|
|
39
|
+
}
|
package/lib/cache_store.js
DELETED
|
@@ -1,151 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const { getMtime } = require('./fs_util');
|
|
4
|
-
|
|
5
|
-
/* 缓存条目上限,超出后整体清空 */
|
|
6
|
-
const CACHE_MAX = 200;
|
|
7
|
-
/* 短时间窗口内直接复用,避免每次重复校验目录与文件的时间戳 */
|
|
8
|
-
const CACHE_TTL = 2000;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* 扫描结果缓存
|
|
12
|
-
* @description 跨实例共享同一目录的遍历结果,未监听时按时间戳校验有效性
|
|
13
|
-
*/
|
|
14
|
-
class CacheStore {
|
|
15
|
-
/** 缓存键 -> 缓存项 */
|
|
16
|
-
#stores = new Map();
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* 读取缓存项,不做有效性校验
|
|
20
|
-
* @param {string} key 缓存键
|
|
21
|
-
* @returns {object|null} 缓存项,不存在返回 null
|
|
22
|
-
*/
|
|
23
|
-
readCache(key) {
|
|
24
|
-
return this.#stores.get(key) || null;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 读取仍然有效的缓存
|
|
29
|
-
* @param {string} key 缓存键
|
|
30
|
-
* @returns {object|null} 缓存项,不存在或已失效返回 null
|
|
31
|
-
*/
|
|
32
|
-
getCache(key) {
|
|
33
|
-
const cache = this.#stores.get(key);
|
|
34
|
-
if (!cache) {
|
|
35
|
-
return null;
|
|
36
|
-
}
|
|
37
|
-
if (cache.watched) {
|
|
38
|
-
return cache;
|
|
39
|
-
}
|
|
40
|
-
const now = Date.now();
|
|
41
|
-
if (now - cache.time <= CACHE_TTL) {
|
|
42
|
-
return cache;
|
|
43
|
-
}
|
|
44
|
-
if (this._isCacheValid(cache)) {
|
|
45
|
-
cache.time = now;
|
|
46
|
-
return cache;
|
|
47
|
-
}
|
|
48
|
-
this.#stores.delete(key);
|
|
49
|
-
return null;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* 判断写入新缓存键是否会触发上限淘汰
|
|
54
|
-
* @param {string} key 缓存键
|
|
55
|
-
* @returns {boolean} 已达上限且键不存在返回 true
|
|
56
|
-
*/
|
|
57
|
-
isOverflow(key) {
|
|
58
|
-
return this.#stores.size >= CACHE_MAX && !this.#stores.has(key);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 写入扫描缓存
|
|
63
|
-
* @description 已有监听器时保留监听标记,避免监听期间退化回时间戳校验
|
|
64
|
-
* @param {string} key 缓存键
|
|
65
|
-
* @param {{ dir: string, extensions: Array<string>, ignore: Array<string>, items: Array<object>, dirs: Array<string> }} options 缓存内容
|
|
66
|
-
* @returns {void}
|
|
67
|
-
*/
|
|
68
|
-
setCache(key, options) {
|
|
69
|
-
const old = this.#stores.get(key);
|
|
70
|
-
const watched = !!(old && old.watched);
|
|
71
|
-
this.#stores.set(key, this._createCache(options, watched));
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 清空全部缓存项
|
|
76
|
-
* @returns {void}
|
|
77
|
-
*/
|
|
78
|
-
clear() {
|
|
79
|
-
this.#stores.clear();
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* 记录缓存对应的目录与文件时间戳
|
|
84
|
-
* @param {object} cache 缓存项
|
|
85
|
-
* @returns {void}
|
|
86
|
-
*/
|
|
87
|
-
captureMtimes(cache) {
|
|
88
|
-
cache.dir_mtimes = [];
|
|
89
|
-
for (let i = 0; i < cache.dirs.length; i++) {
|
|
90
|
-
cache.dir_mtimes.push(getMtime(cache.dirs[i]));
|
|
91
|
-
}
|
|
92
|
-
cache.file_mtimes = [];
|
|
93
|
-
for (let i = 0; i < cache.items.length; i++) {
|
|
94
|
-
cache.file_mtimes.push(getMtime(cache.items[i].absPath));
|
|
95
|
-
}
|
|
96
|
-
cache.time = Date.now();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* 校验缓存是否有效
|
|
101
|
-
* @description 目录时间戳变化表示增删条目,文件时间戳变化表示内容修改
|
|
102
|
-
* @param {object} cache 缓存项
|
|
103
|
-
* @returns {boolean} 全部未变化返回 true
|
|
104
|
-
* @private
|
|
105
|
-
*/
|
|
106
|
-
_isCacheValid(cache) {
|
|
107
|
-
for (let i = 0; i < cache.dirs.length; i++) {
|
|
108
|
-
if (getMtime(cache.dirs[i]) !== cache.dir_mtimes[i]) {
|
|
109
|
-
return false;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
for (let i = 0; i < cache.items.length; i++) {
|
|
113
|
-
if (getMtime(cache.items[i].absPath) !== cache.file_mtimes[i]) {
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return true;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 创建缓存项
|
|
122
|
-
* @param {{ dir: string, extensions: Array<string>, ignore: Array<string>, items: Array<object>, dirs: Array<string> }} options 缓存内容
|
|
123
|
-
* @param {boolean} watched 是否已开启监听
|
|
124
|
-
* @returns {object} 缓存项
|
|
125
|
-
* @private
|
|
126
|
-
*/
|
|
127
|
-
_createCache(options, watched) {
|
|
128
|
-
const items = [];
|
|
129
|
-
for (let i = 0; i < options.items.length; i++) {
|
|
130
|
-
items.push({ ...options.items[i] });
|
|
131
|
-
}
|
|
132
|
-
const cache = {
|
|
133
|
-
dir: options.dir,
|
|
134
|
-
extensions: [...options.extensions],
|
|
135
|
-
ignore: [...options.ignore],
|
|
136
|
-
items,
|
|
137
|
-
dirs: [...options.dirs],
|
|
138
|
-
watched: !!watched,
|
|
139
|
-
dir_mtimes: [],
|
|
140
|
-
file_mtimes: [],
|
|
141
|
-
time: Date.now(),
|
|
142
|
-
};
|
|
143
|
-
if (!cache.watched) {
|
|
144
|
-
this.captureMtimes(cache);
|
|
145
|
-
}
|
|
146
|
-
return cache;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/* 导出单例 */
|
|
151
|
-
module.exports = new CacheStore();
|
package/lib/fs_util.js
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const fs = require('fs');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
const { createItem } = require('./path_util');
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* 获取路径的修改时间
|
|
9
|
-
* @param {string} target 文件或目录路径
|
|
10
|
-
* @returns {number} 毫秒时间戳,读取失败返回 -1
|
|
11
|
-
*/
|
|
12
|
-
function getMtime(target) {
|
|
13
|
-
try {
|
|
14
|
-
return fs.statSync(target).mtimeMs;
|
|
15
|
-
} catch {
|
|
16
|
-
return -1;
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 递归遍历目录并写入条目
|
|
22
|
-
* @param {string} dir 当前目录绝对路径
|
|
23
|
-
* @param {string} base_dir 扫描根目录绝对路径
|
|
24
|
-
* @param {{ extensions: string[], ignore: string[] }} config 扫描配置
|
|
25
|
-
* @param {Map<string, object>} files 条目容器
|
|
26
|
-
* @param {Array<string>} dirs 遍历过的目录,用于记录与缓存校验
|
|
27
|
-
* @returns {void}
|
|
28
|
-
*/
|
|
29
|
-
function walkDir(dir, base_dir, config, files, dirs) {
|
|
30
|
-
dirs.push(dir);
|
|
31
|
-
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
32
|
-
|
|
33
|
-
for (const entry of entries) {
|
|
34
|
-
const full_path = path.join(dir, entry.name);
|
|
35
|
-
|
|
36
|
-
if (entry.isDirectory()) {
|
|
37
|
-
if (config.ignore.includes(entry.name)) continue;
|
|
38
|
-
walkDir(full_path, base_dir, config, files, dirs);
|
|
39
|
-
continue;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (!entry.isFile()) continue;
|
|
43
|
-
|
|
44
|
-
const item = createItem(base_dir, full_path);
|
|
45
|
-
if (!config.extensions.includes(item.ext)) continue;
|
|
46
|
-
files.set(item.key, item);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
module.exports = { getMtime, walkDir };
|
package/lib/scanner_config.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/* 默认忽略的常见目录(版本控制、依赖、IDE 配置),始终生效,用户配置在其后追加 */
|
|
4
|
-
const DEFAULT_IGNORE_DIRS = ['.git', '.svn', '.hg', 'node_modules', '.idea', '.vscode'];
|
|
5
|
-
|
|
6
|
-
/* 扫描器默认配置 */
|
|
7
|
-
const DEFAULT_CONFIG = {
|
|
8
|
-
/** 支持的扩展名 */
|
|
9
|
-
extensions: ['.json'],
|
|
10
|
-
/** 忽略的目录 */
|
|
11
|
-
ignore: DEFAULT_IGNORE_DIRS,
|
|
12
|
-
/** 取用 mm_watcher 共享监听器的作用域:同作用域同目录共用一个监听器 */
|
|
13
|
-
use_scope: 'os',
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
module.exports = { DEFAULT_CONFIG };
|