mm_scanner 2.0.0 → 5.0.0
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 +30 -42
- package/index.js +23 -18
- package/lib/diff_util.js +3 -3
- package/lib/path_util.js +13 -32
- package/lib/scanner.js +228 -339
- package/lib/type_store.js +33 -0
- package/package.json +6 -6
- package/lib/cache_store.js +0 -151
- package/lib/fs_util.js +0 -50
- package/lib/scanner_config.js +0 -14
package/example/demo.js
CHANGED
|
@@ -1,62 +1,50 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const path = require('path');
|
|
4
|
-
const { Scanner, scannerAdmin } = require('../index');
|
|
4
|
+
const { Scanner, scannerAdmin, typeStore } = require('../index');
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* 使用示例:扫描 example/data
|
|
7
|
+
* 使用示例:扫描 example/data 目录,按资源类型分类并写入 mm_store
|
|
8
8
|
*/
|
|
9
9
|
function demo() {
|
|
10
10
|
const dir = path.join(__dirname, 'data');
|
|
11
11
|
|
|
12
|
-
// 1.
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
// 1. 资源类型表:键为类型名,值为 { ext }
|
|
13
|
+
const resources = {
|
|
14
|
+
app: { ext: 'app.json' },
|
|
15
|
+
api: { ext: 'api.json' },
|
|
16
|
+
config: { ext: '.json' },
|
|
17
|
+
script: { ext: '.js' },
|
|
18
|
+
};
|
|
17
19
|
|
|
18
|
-
// 2.
|
|
20
|
+
// 2. 创建扫描器,扫描时即完成分类
|
|
21
|
+
const scanner = new Scanner({ resources, ignore: ['dist'] });
|
|
19
22
|
scanner.scan(dir);
|
|
20
23
|
console.log('=== 扫描结果 ===');
|
|
21
24
|
console.log(' 基目录: ' + scanner.baseDir);
|
|
22
25
|
console.log(' 文件数: ' + scanner.size);
|
|
23
|
-
for (const
|
|
24
|
-
console.log(
|
|
26
|
+
for (const item of scanner.getAll()) {
|
|
27
|
+
console.log(` [${item.type}] ${item.rel}`);
|
|
25
28
|
}
|
|
26
29
|
|
|
27
|
-
// 3.
|
|
28
|
-
console.log('===
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
console.log(' config 目录: ' + scanner.getByDir('config').map((f) => f.key).join(', '));
|
|
37
|
-
console.log(' 一级子目录: ' + scanner.dirTypes().join(', '));
|
|
38
|
-
console.log(' 扩展名类型: ' + scanner.extTypes().join(', '));
|
|
39
|
-
|
|
40
|
-
// 5. 自定义扩展名与忽略目录
|
|
41
|
-
const custom = new Scanner({ extensions: ['json', 'js'], ignore: ['dist'] });
|
|
42
|
-
custom.scan(dir);
|
|
43
|
-
console.log('=== 自定义配置 ===');
|
|
44
|
-
console.log(' 文件数: ' + custom.size);
|
|
45
|
-
console.log(' 文件列表: ' + custom.keys().join(', '));
|
|
46
|
-
console.log(' dist 目录已被忽略: ' + (custom.getByDir('dist').length === 0));
|
|
47
|
-
|
|
48
|
-
// 6. 文件变化后重新扫描
|
|
49
|
-
custom.rescan();
|
|
30
|
+
// 3. 一类一个 Store:作用域就是资源类型名
|
|
31
|
+
console.log('=== 按类型取切片 ===');
|
|
32
|
+
for (const type of Object.keys(resources)) {
|
|
33
|
+
const list = typeStore(type).list({ root: scanner.baseDir });
|
|
34
|
+
console.log(` ${type}: ` + list.map((one) => one.name).join(', '));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 4. 文件变化后重新扫描,Store 同步增删
|
|
38
|
+
scanner.rescan();
|
|
50
39
|
console.log('=== 重新扫描 ===');
|
|
51
|
-
console.log(' 文件数: ' +
|
|
52
|
-
|
|
53
|
-
//
|
|
54
|
-
const
|
|
55
|
-
const
|
|
56
|
-
console.log('===
|
|
57
|
-
console.log('
|
|
58
|
-
console.log('
|
|
59
|
-
console.log(' demo 实例与 os 实例相互独立: ' + (demo_scanner !== os_scanner));
|
|
40
|
+
console.log(' 文件数: ' + scanner.size);
|
|
41
|
+
|
|
42
|
+
// 5. 通过 admin 按目录复用扫描器实例
|
|
43
|
+
const first = scannerAdmin(dir, { resources });
|
|
44
|
+
const second = scannerAdmin(dir);
|
|
45
|
+
console.log('=== 目录实例 ===');
|
|
46
|
+
console.log(' 实例类型正确: ' + (first instanceof Scanner));
|
|
47
|
+
console.log(' 同目录复用同一实例: ' + (second === first));
|
|
60
48
|
}
|
|
61
49
|
|
|
62
50
|
demo();
|
package/index.js
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
const
|
|
2
|
-
const
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
admin
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
admin,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const Scanner = require('./lib/scanner');
|
|
3
|
+
const { typeStore } = require('./lib/type_store');
|
|
4
|
+
const { reg } = require('mm_admin');
|
|
5
|
+
|
|
6
|
+
const admin = reg('mm_scanner');
|
|
7
|
+
admin.setClass(Scanner);
|
|
8
|
+
|
|
9
|
+
module.exports = {
|
|
10
|
+
Scanner,
|
|
11
|
+
admin,
|
|
12
|
+
typeStore,
|
|
13
|
+
/**
|
|
14
|
+
* 按目录取共享扫描器实例
|
|
15
|
+
* @description 一个扫描器只负责一个目录:同目录返回同一实例,不同目录自动分实例
|
|
16
|
+
* @param {string} dir - 扫描目录
|
|
17
|
+
* @param {object} [config] - 扫描器配置
|
|
18
|
+
* @returns {Scanner} 扫描器实例
|
|
19
|
+
*/
|
|
20
|
+
scannerAdmin(dir, config) {
|
|
21
|
+
return admin.get(path.resolve(dir), config);
|
|
22
|
+
}
|
|
23
|
+
};
|
package/lib/diff_util.js
CHANGED
|
@@ -8,10 +8,10 @@
|
|
|
8
8
|
*/
|
|
9
9
|
function diffItems(before, after) {
|
|
10
10
|
const before_map = new Map();
|
|
11
|
-
for (let i = 0; i < before.length; i++) before_map.set(before[i].
|
|
11
|
+
for (let i = 0; i < before.length; i++) before_map.set(before[i].file, before[i]);
|
|
12
12
|
|
|
13
13
|
const after_map = new Map();
|
|
14
|
-
for (let i = 0; i < after.length; i++) after_map.set(after[i].
|
|
14
|
+
for (let i = 0; i < after.length; i++) after_map.set(after[i].file, after[i]);
|
|
15
15
|
|
|
16
16
|
const changes = [];
|
|
17
17
|
for (const [key, item] of after_map) {
|
|
@@ -20,7 +20,7 @@ function diffItems(before, after) {
|
|
|
20
20
|
changes.push({ type: 'add', key, item: { ...item } });
|
|
21
21
|
continue;
|
|
22
22
|
}
|
|
23
|
-
if (old.
|
|
23
|
+
if (old.rel !== item.rel) {
|
|
24
24
|
changes.push({ type: 'change', key, item: { ...item } });
|
|
25
25
|
}
|
|
26
26
|
}
|
package/lib/path_util.js
CHANGED
|
@@ -14,45 +14,26 @@ function normalizeKey(p) {
|
|
|
14
14
|
.replace(/^\/+|\/+$/g, '');
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* 扩展名规范化:补点、转小写
|
|
19
|
-
* @param {string} ext 原始扩展名
|
|
20
|
-
* @returns {string} 规范化后的扩展名
|
|
21
|
-
*/
|
|
22
|
-
function normalizeExt(ext) {
|
|
23
|
-
const e = String(ext).trim().toLowerCase();
|
|
24
|
-
return e.startsWith('.') ? e : '.' + e;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* 生成扫描缓存键
|
|
29
|
-
* @param {string} dir 目录绝对路径
|
|
30
|
-
* @param {Array<string>} extensions 扩展名数组
|
|
31
|
-
* @param {Array<string>} ignore 忽略目录数组
|
|
32
|
-
* @returns {string} 缓存键
|
|
33
|
-
*/
|
|
34
|
-
function buildCacheKey(dir, extensions, ignore) {
|
|
35
|
-
return [dir, extensions.join(','), ignore.join(',')].join('\u0001');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
17
|
/**
|
|
39
18
|
* 构造单个文件的条目元信息
|
|
40
|
-
* @param {string}
|
|
19
|
+
* @param {string} type 资源类型
|
|
20
|
+
* @param {string} root 扫描根目录绝对路径
|
|
41
21
|
* @param {string} abs_path 文件绝对路径
|
|
42
|
-
* @returns {{
|
|
22
|
+
* @returns {{ file: string, name: string, root: string, dir: string, rel: string, ext: string, type: string }} 条目
|
|
43
23
|
*/
|
|
44
|
-
function createItem(
|
|
45
|
-
const
|
|
46
|
-
const ext = path.extname(
|
|
47
|
-
const dir_part = path.posix.dirname(
|
|
24
|
+
function createItem(type, root, abs_path) {
|
|
25
|
+
const rel = path.relative(root, abs_path).replace(/\\/g, '/');
|
|
26
|
+
const ext = path.extname(rel).toLowerCase();
|
|
27
|
+
const dir_part = path.posix.dirname(rel);
|
|
48
28
|
return {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
29
|
+
file: abs_path,
|
|
30
|
+
name: path.basename(rel),
|
|
31
|
+
root,
|
|
52
32
|
dir: dir_part === '.' ? '' : dir_part,
|
|
53
|
-
|
|
33
|
+
rel,
|
|
54
34
|
ext,
|
|
35
|
+
type,
|
|
55
36
|
};
|
|
56
37
|
}
|
|
57
38
|
|
|
58
|
-
module.exports = { normalizeKey,
|
|
39
|
+
module.exports = { normalizeKey, createItem };
|
package/lib/scanner.js
CHANGED
|
@@ -2,50 +2,40 @@
|
|
|
2
2
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
|
-
const {
|
|
6
|
-
const { normalizeKey, normalizeExt, buildCacheKey, createItem } = require('./path_util');
|
|
7
|
-
const { DEFAULT_CONFIG } = require('./scanner_config');
|
|
8
|
-
const { walkDir } = require('./fs_util');
|
|
5
|
+
const { normalizeKey, createItem } = require('./path_util');
|
|
9
6
|
const { diffItems } = require('./diff_util');
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
/* 监听事件防抖窗口,与 mm_watcher 默认值一致 */
|
|
13
|
-
const WATCH_DEBOUNCE = 100;
|
|
7
|
+
const { typeStore, clearTypeStores } = require('./type_store');
|
|
14
8
|
|
|
15
9
|
/**
|
|
16
10
|
* 目录扫描器
|
|
17
|
-
* @description
|
|
18
|
-
*
|
|
19
|
-
*
|
|
11
|
+
* @description 按 Loader 的资源类型表递归扫描目录,扫描时即完成分类,
|
|
12
|
+
* 并按「一类一个 Store」写入 mm_store(作用域为资源类型名);
|
|
13
|
+
* 支持 apply 接收监听变更做增量更新,目录监听由调用方(mm_loader)负责
|
|
20
14
|
*/
|
|
21
15
|
class Scanner {
|
|
22
16
|
/**
|
|
23
17
|
* 扫描器默认配置
|
|
24
18
|
*/
|
|
25
|
-
static config =
|
|
19
|
+
static config = {
|
|
20
|
+
/** 资源类型表,键为类型名,值为 { ext };扫描结果按类型分别写入 mm_store */
|
|
21
|
+
resources: {},
|
|
22
|
+
/** 忽略的目录 */
|
|
23
|
+
ignore: ['.git', '.svn', '.hg', 'node_modules', '.idea', '.vscode']
|
|
24
|
+
};
|
|
26
25
|
|
|
27
|
-
/**
|
|
28
|
-
#files = new Map();
|
|
26
|
+
/** 扫描根目录绝对路径 */
|
|
29
27
|
#base_dir = null;
|
|
30
28
|
#scanned = false;
|
|
31
|
-
/**
|
|
32
|
-
#
|
|
33
|
-
/** 本次扫描对应的缓存键 */
|
|
34
|
-
#cache_key = null;
|
|
35
|
-
/** 本实例使用的共享监听器(watcherAdmin 按缓存键缓存) */
|
|
36
|
-
#watcher = null;
|
|
37
|
-
/** 本实例注册到监听器上的事件处理器,未监听时为 null */
|
|
38
|
-
#handlers = null;
|
|
39
|
-
/** 外部变更回调 */
|
|
40
|
-
#callback = null;
|
|
29
|
+
/** 配置变化后需重新分类 */
|
|
30
|
+
#stale = false;
|
|
41
31
|
|
|
42
32
|
/**
|
|
43
33
|
* 创建扫描器实例
|
|
44
|
-
* @param {{
|
|
34
|
+
* @param {{ resources?: object, ignore?: string[] }} [config] - 扫描器配置
|
|
45
35
|
*/
|
|
46
36
|
constructor(config = {}) {
|
|
47
37
|
this.config = {
|
|
48
|
-
|
|
38
|
+
resources: { ...Scanner.config.resources },
|
|
49
39
|
ignore: [...Scanner.config.ignore],
|
|
50
40
|
};
|
|
51
41
|
this.setConfig(config);
|
|
@@ -53,174 +43,187 @@ class Scanner {
|
|
|
53
43
|
|
|
54
44
|
/**
|
|
55
45
|
* 设置扫描配置
|
|
56
|
-
* @description
|
|
57
|
-
* ignore
|
|
58
|
-
* @param {{
|
|
46
|
+
* @description resources 浅合并(新增类型或后缀变化时标记需重扫),
|
|
47
|
+
* ignore 按并集追加(默认忽略目录始终生效)
|
|
48
|
+
* @param {{ resources?: object, ignore?: string[] }} [config] - 扫描器配置
|
|
59
49
|
* @returns {void}
|
|
60
50
|
*/
|
|
61
51
|
setConfig(config = {}) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
if (Array.isArray(config.ignore)) {
|
|
66
|
-
this.config.ignore = [...new Set([...this.config.ignore, ...config.ignore])];
|
|
67
|
-
}
|
|
52
|
+
this.#mergeResources(config.resources);
|
|
53
|
+
this.#mergeIgnore(config.ignore);
|
|
68
54
|
}
|
|
69
55
|
|
|
70
56
|
/* -------- 私有 -------- */
|
|
71
57
|
|
|
72
58
|
/**
|
|
73
|
-
*
|
|
74
|
-
* @param {
|
|
75
|
-
* @param {Array<string>} dirs 缓存中的目录列表
|
|
59
|
+
* 合并资源类型表
|
|
60
|
+
* @param {object} [resources] 资源类型表
|
|
76
61
|
* @returns {void}
|
|
62
|
+
* @private
|
|
77
63
|
*/
|
|
78
|
-
#
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
this.#
|
|
84
|
-
this.#scanned = true;
|
|
64
|
+
#mergeResources(resources) {
|
|
65
|
+
if (!resources || typeof resources !== 'object' || Array.isArray(resources)) return;
|
|
66
|
+
const merged = { ...this.config.resources, ...resources };
|
|
67
|
+
if (this.#sameResources(merged)) return;
|
|
68
|
+
this.config.resources = merged;
|
|
69
|
+
if (this.#scanned) this.#stale = true;
|
|
85
70
|
}
|
|
86
71
|
|
|
87
72
|
/**
|
|
88
|
-
*
|
|
89
|
-
* @
|
|
73
|
+
* 判断资源类型表是否与当前配置等价
|
|
74
|
+
* @param {object} resources 待比较的资源类型表
|
|
75
|
+
* @returns {boolean} 等价返回 true
|
|
76
|
+
* @private
|
|
90
77
|
*/
|
|
91
|
-
#
|
|
92
|
-
this
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
78
|
+
#sameResources(resources) {
|
|
79
|
+
const current = this.config.resources;
|
|
80
|
+
const keys = Object.keys(resources);
|
|
81
|
+
if (keys.length !== Object.keys(current).length) return false;
|
|
82
|
+
for (const type of keys) {
|
|
83
|
+
if (!current[type] || current[type].ext !== resources[type].ext) return false;
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
96
86
|
}
|
|
97
87
|
|
|
98
88
|
/**
|
|
99
|
-
*
|
|
100
|
-
* @
|
|
89
|
+
* 合并忽略目录
|
|
90
|
+
* @param {string[]} [ignore] 忽略目录列表
|
|
91
|
+
* @returns {void}
|
|
92
|
+
* @private
|
|
101
93
|
*/
|
|
102
|
-
#
|
|
103
|
-
return
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
dirs: this.#dirs,
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
#ensureReady() {
|
|
113
|
-
if (!this.#scanned) {
|
|
114
|
-
throw new Error('[scanner] 尚未初始化,请先在入口调用 scanner.scan(dir)');
|
|
115
|
-
}
|
|
94
|
+
#mergeIgnore(ignore) {
|
|
95
|
+
if (!Array.isArray(ignore)) return;
|
|
96
|
+
const merged = [...new Set([...this.config.ignore, ...ignore])];
|
|
97
|
+
if (merged.length === this.config.ignore.length) return;
|
|
98
|
+
this.config.ignore = merged;
|
|
99
|
+
if (this.#scanned) this.#stale = true;
|
|
116
100
|
}
|
|
117
101
|
|
|
118
|
-
/**
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
102
|
+
/**
|
|
103
|
+
* 按资源类型表对文件名分类
|
|
104
|
+
* @param {string} name 文件名
|
|
105
|
+
* @returns {string} 资源类型,未命中返回空字符串
|
|
106
|
+
* @private
|
|
107
|
+
*/
|
|
108
|
+
#typeOf(name) {
|
|
109
|
+
const file = String(name).toLowerCase();
|
|
110
|
+
const list = this.config.resources;
|
|
111
|
+
for (const type in list) {
|
|
112
|
+
const ext = String(list[type].ext || '').toLowerCase();
|
|
113
|
+
if (ext && file.endsWith(ext)) return type;
|
|
126
114
|
}
|
|
127
|
-
return
|
|
115
|
+
return '';
|
|
128
116
|
}
|
|
129
117
|
|
|
130
118
|
/**
|
|
131
|
-
*
|
|
132
|
-
* @
|
|
133
|
-
* @
|
|
134
|
-
* @returns {boolean} 命中返回 true,目标目录为空时视为全部命中
|
|
119
|
+
* 遍历根目录并完成分类
|
|
120
|
+
* @returns {Map<string, object>} 文件绝对路径 -> 条目
|
|
121
|
+
* @private
|
|
135
122
|
*/
|
|
136
|
-
#
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
return
|
|
123
|
+
#walk() {
|
|
124
|
+
const entries = new Map();
|
|
125
|
+
this.#walkDir(this.#base_dir, entries);
|
|
126
|
+
this.#scanned = true;
|
|
127
|
+
this.#stale = false;
|
|
128
|
+
return entries;
|
|
142
129
|
}
|
|
143
130
|
|
|
144
131
|
/**
|
|
145
|
-
*
|
|
146
|
-
* @
|
|
132
|
+
* 递归遍历目录并收集命中类型的文件
|
|
133
|
+
* @param {string} dir 当前目录绝对路径
|
|
134
|
+
* @param {Map<string, object>} entries 条目收集容器
|
|
147
135
|
* @returns {void}
|
|
136
|
+
* @private
|
|
148
137
|
*/
|
|
149
|
-
#
|
|
150
|
-
|
|
151
|
-
|
|
138
|
+
#walkDir(dir, entries) {
|
|
139
|
+
const list = fs.readdirSync(dir, { withFileTypes: true });
|
|
140
|
+
for (const entry of list) {
|
|
141
|
+
const full_path = path.join(dir, entry.name);
|
|
142
|
+
if (entry.isDirectory()) {
|
|
143
|
+
if (this.config.ignore.includes(entry.name)) continue;
|
|
144
|
+
this.#walkDir(full_path, entries);
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (!entry.isFile()) continue;
|
|
148
|
+
const type = this.#typeOf(entry.name);
|
|
149
|
+
if (!type) continue;
|
|
150
|
+
const item = createItem(type, this.#base_dir, full_path);
|
|
151
|
+
entries.set(item.file, item);
|
|
152
152
|
}
|
|
153
|
-
cache_store.setCache(this.#cache_key, this.#snapshot());
|
|
154
153
|
}
|
|
155
154
|
|
|
156
155
|
/**
|
|
157
|
-
*
|
|
156
|
+
* 把遍历结果按类型同步到 Store
|
|
157
|
+
* @description 同一 Store 可能被多个扫描根目录共用,只同步本 root 的条目;
|
|
158
|
+
* 已存在时跳过,保留 loader 写入的 config
|
|
159
|
+
* @param {Map<string, object>} entries 本次遍历结果
|
|
158
160
|
* @returns {void}
|
|
161
|
+
* @private
|
|
159
162
|
*/
|
|
160
|
-
#
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
163
|
+
#sync(entries) {
|
|
164
|
+
for (const type of Object.keys(this.config.resources)) {
|
|
165
|
+
const store = typeStore(type);
|
|
166
|
+
const list = [];
|
|
167
|
+
for (const item of entries.values()) {
|
|
168
|
+
if (item.type === type) list.push(item);
|
|
169
|
+
}
|
|
170
|
+
const keep = new Set(list.map((one) => one.file));
|
|
171
|
+
for (const old of store.list({ root: this.#base_dir })) {
|
|
172
|
+
if (!keep.has(old.file)) store.delById(old.file);
|
|
173
|
+
}
|
|
174
|
+
for (const item of list) {
|
|
175
|
+
if (!store.getById(item.file)) store.add(item);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
166
178
|
}
|
|
167
179
|
|
|
168
180
|
/**
|
|
169
|
-
*
|
|
170
|
-
* @
|
|
181
|
+
* 读取本实例扫描根下的全部条目
|
|
182
|
+
* @description Store 是唯一数据源,按类型取本 root 切片
|
|
183
|
+
* @returns {Array<object>} 条目数组
|
|
184
|
+
* @private
|
|
171
185
|
*/
|
|
172
|
-
#
|
|
173
|
-
const
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
186
|
+
#entries() {
|
|
187
|
+
const list = [];
|
|
188
|
+
for (const type of Object.keys(this.config.resources)) {
|
|
189
|
+
for (const item of typeStore(type).list({ root: this.#base_dir })) list.push(item);
|
|
190
|
+
}
|
|
191
|
+
return list;
|
|
177
192
|
}
|
|
178
193
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
*/
|
|
183
|
-
#detachHandlers() {
|
|
184
|
-
if (this.#watcher && this.#handlers) {
|
|
185
|
-
this.#watcher.off('add', this.#handlers.add);
|
|
186
|
-
this.#watcher.off('set', this.#handlers.set);
|
|
187
|
-
this.#watcher.off('del', this.#handlers.del);
|
|
188
|
-
this.#watcher.off('reload', this.#handlers.reload);
|
|
194
|
+
#ensureReady() {
|
|
195
|
+
if (!this.#scanned) {
|
|
196
|
+
throw new Error('[scanner] 尚未初始化,请先在入口调用 scanner.scan(dir)');
|
|
189
197
|
}
|
|
190
|
-
this.#handlers = null;
|
|
191
|
-
this.#watcher = null;
|
|
192
198
|
}
|
|
193
199
|
|
|
194
200
|
/**
|
|
195
201
|
* 判断相对路径是否为本实例关心的文件
|
|
196
|
-
* @description 目录段命中本实例忽略配置或扩展名不在白名单内均视为不关心;
|
|
197
|
-
* 共享监听器只按目录监听,配置过滤统一由实例自行完成
|
|
198
202
|
* @param {string} rel_path 规范化后的相对路径
|
|
199
|
-
* @returns {boolean}
|
|
203
|
+
* @returns {boolean} 关心返回 true
|
|
204
|
+
* @private
|
|
200
205
|
*/
|
|
201
206
|
#isConcerned(rel_path) {
|
|
202
207
|
const parts = rel_path.split('/');
|
|
203
208
|
for (let i = 0; i < parts.length - 1; i++) {
|
|
204
209
|
if (this.config.ignore.includes(parts[i])) return false;
|
|
205
210
|
}
|
|
206
|
-
|
|
207
|
-
return this.config.extensions.includes(ext);
|
|
211
|
+
return this.#typeOf(parts[parts.length - 1]) !== '';
|
|
208
212
|
}
|
|
209
213
|
|
|
210
214
|
/**
|
|
211
|
-
*
|
|
212
|
-
* @description
|
|
213
|
-
*
|
|
215
|
+
* 规划文件新增或修改
|
|
216
|
+
* @description 不关心的路径丢弃;stat 失败或不是文件按删除处理;
|
|
217
|
+
* Store 中已有条目视为 change,否则视为 add
|
|
214
218
|
* @param {string} rel_path 相对路径
|
|
215
|
-
* @returns {
|
|
219
|
+
* @returns {object|false|null} 变更条目;false 表示需全量重建;null 表示不关心
|
|
220
|
+
* @private
|
|
216
221
|
*/
|
|
217
|
-
#
|
|
222
|
+
#planAdd(rel_path) {
|
|
218
223
|
const key = normalizeKey(rel_path);
|
|
219
|
-
if (!this.#isConcerned(key)) return;
|
|
224
|
+
if (!this.#isConcerned(key)) return null;
|
|
220
225
|
|
|
221
226
|
const abs_path = path.join(this.#base_dir, key);
|
|
222
|
-
const item = createItem(this.#base_dir, abs_path);
|
|
223
|
-
|
|
224
227
|
let stat = null;
|
|
225
228
|
try {
|
|
226
229
|
stat = fs.statSync(abs_path);
|
|
@@ -228,89 +231,68 @@ class Scanner {
|
|
|
228
231
|
stat = null;
|
|
229
232
|
}
|
|
230
233
|
if (!stat || !stat.isFile()) {
|
|
231
|
-
this.#
|
|
232
|
-
return;
|
|
234
|
+
return this.#planDel(rel_path);
|
|
233
235
|
}
|
|
234
|
-
|
|
235
|
-
this.#
|
|
236
|
-
|
|
237
|
-
|
|
236
|
+
|
|
237
|
+
const item = createItem(this.#typeOf(path.basename(abs_path)), this.#base_dir, abs_path);
|
|
238
|
+
const old = typeStore(item.type).getById(item.file);
|
|
239
|
+
return { type: old ? 'change' : 'add', key: item.file, item };
|
|
238
240
|
}
|
|
239
241
|
|
|
240
242
|
/**
|
|
241
|
-
*
|
|
242
|
-
* @description
|
|
243
|
-
* 关心的路径未命中条目时无法精确增量,退化为全量重建兜底
|
|
243
|
+
* 规划文件删除
|
|
244
|
+
* @description 不关心的路径丢弃;Store 未命中时无法精确增量,需全量重建兜底
|
|
244
245
|
* @param {string} rel_path 相对路径
|
|
245
|
-
* @returns {
|
|
246
|
+
* @returns {object|false|null} 变更条目;false 表示需全量重建;null 表示不关心
|
|
247
|
+
* @private
|
|
246
248
|
*/
|
|
247
|
-
#
|
|
249
|
+
#planDel(rel_path) {
|
|
248
250
|
const key = normalizeKey(rel_path);
|
|
249
|
-
if (!this.#isConcerned(key)) return;
|
|
250
|
-
|
|
251
|
-
const old = this.#files.get(key);
|
|
252
|
-
if (!old) {
|
|
253
|
-
this.#onReloadEvent();
|
|
254
|
-
return;
|
|
255
|
-
}
|
|
256
|
-
this.#files.delete(key);
|
|
257
|
-
this.#writeCache();
|
|
258
|
-
this.#notify([{ type: 'unlink', key, item: old }]);
|
|
259
|
-
}
|
|
251
|
+
if (!this.#isConcerned(key)) return null;
|
|
260
252
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
const before = [...this.#files.values()];
|
|
267
|
-
this.#walkAndFill();
|
|
268
|
-
this.#writeCache();
|
|
269
|
-
this.#notify(diffItems(before, this.getAll()));
|
|
253
|
+
const file = path.join(this.#base_dir, key);
|
|
254
|
+
const store = typeStore(this.#typeOf(path.basename(file)));
|
|
255
|
+
const old = store.getById(file);
|
|
256
|
+
if (!old) return false;
|
|
257
|
+
return { type: 'unlink', key: file, item: old };
|
|
270
258
|
}
|
|
271
259
|
|
|
272
260
|
/**
|
|
273
|
-
*
|
|
274
|
-
* @param {
|
|
261
|
+
* 提交单条变更到 Store
|
|
262
|
+
* @param {{ type: string, key: string, item: object }} change 变更条目
|
|
275
263
|
* @returns {void}
|
|
264
|
+
* @private
|
|
276
265
|
*/
|
|
277
|
-
#
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
}
|
|
282
|
-
|
|
266
|
+
#commit(change) {
|
|
267
|
+
const store = typeStore(change.item.type);
|
|
268
|
+
if (change.type === 'unlink') {
|
|
269
|
+
store.delById(change.key);
|
|
270
|
+
} else if (change.type === 'change') {
|
|
271
|
+
store.setById(change.key, change.item);
|
|
272
|
+
} else {
|
|
273
|
+
store.add(change.item);
|
|
283
274
|
}
|
|
284
275
|
}
|
|
285
276
|
|
|
286
|
-
/**
|
|
287
|
-
* 判断文件是否命中指定类型
|
|
288
|
-
* 只写扩展名('.json' / 'json')按扩展名匹配,写文件名('event.json')按文件名结尾匹配
|
|
289
|
-
* @param {{ key: string, ext: string }} file - 文件条目
|
|
290
|
-
* @param {string} ext - 扩展名或文件名
|
|
291
|
-
* @returns {boolean} 命中返回 true
|
|
292
|
-
*/
|
|
293
|
-
#matchExt(file, ext) {
|
|
294
|
-
const norm = String(ext).trim().toLowerCase();
|
|
295
|
-
const bare = norm.replace(/^\./, '');
|
|
296
|
-
if (!bare.includes('.')) return file.ext === '.' + bare;
|
|
297
|
-
return file.key === bare || file.key.endsWith('/' + bare);
|
|
298
|
-
}
|
|
299
|
-
|
|
300
277
|
/* -------- 公开 API -------- */
|
|
301
278
|
|
|
302
279
|
/**
|
|
303
280
|
* 扫描目录(重复调用只生效一次)
|
|
304
|
-
* @description
|
|
305
|
-
* @param {string} dir
|
|
306
|
-
* @param {{
|
|
281
|
+
* @description 已扫描时目录不同仅告警;配置变化(resources/ignore)会重新分类
|
|
282
|
+
* @param {string} dir 目录路径
|
|
283
|
+
* @param {{ resources?: object, ignore?: string[] }} [options] 扫描配置
|
|
284
|
+
* @returns {Scanner} 当前实例
|
|
307
285
|
*/
|
|
308
|
-
scan
|
|
286
|
+
scan(dir, options = {}) {
|
|
309
287
|
const resolved = path.resolve(dir);
|
|
310
288
|
|
|
311
289
|
if (this.#scanned) {
|
|
312
290
|
if (this.#base_dir !== resolved) {
|
|
313
291
|
console.warn(`[scanner] 已使用 ${this.#base_dir} 扫描过,忽略本次 scan(${resolved})`);
|
|
292
|
+
return this;
|
|
293
|
+
}
|
|
294
|
+
if (this.#stale) {
|
|
295
|
+
this.#sync(this.#walk());
|
|
314
296
|
}
|
|
315
297
|
return this;
|
|
316
298
|
}
|
|
@@ -319,180 +301,87 @@ class Scanner {
|
|
|
319
301
|
if (!fs.statSync(resolved).isDirectory()) throw new Error(`[scanner] 不是目录: ${resolved}`);
|
|
320
302
|
|
|
321
303
|
this.setConfig(options);
|
|
322
|
-
|
|
323
304
|
this.#base_dir = resolved;
|
|
324
|
-
this.#
|
|
325
|
-
|
|
326
|
-
const cache = cache_store.getCache(this.#cache_key);
|
|
327
|
-
if (cache) {
|
|
328
|
-
this.#fill(cache.items, cache.dirs);
|
|
329
|
-
return this;
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
this.#walkAndFill();
|
|
333
|
-
this.#writeCache();
|
|
305
|
+
this.#sync(this.#walk());
|
|
334
306
|
return this;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/** 拿条目元信息(含 absPath),key 可带可不带扩展名 */
|
|
338
|
-
get = (key) => {
|
|
339
|
-
this.#ensureReady();
|
|
340
|
-
const k = this.#resolveKey(key);
|
|
341
|
-
return k ? this.#files.get(k) : undefined;
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
/** 拿绝对路径(给 Loader 用的最常见接口) */
|
|
345
|
-
getPath = (key) => {
|
|
346
|
-
const info = this.get(key);
|
|
347
|
-
return info ? info.absPath : undefined;
|
|
348
|
-
};
|
|
349
|
-
|
|
350
|
-
has = (key) => {
|
|
351
|
-
this.#ensureReady();
|
|
352
|
-
return this.#resolveKey(key) !== null;
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
keys = () => {
|
|
356
|
-
this.#ensureReady();
|
|
357
|
-
return [...this.#files.keys()];
|
|
358
|
-
};
|
|
359
|
-
|
|
360
|
-
getAll = () => {
|
|
361
|
-
this.#ensureReady();
|
|
362
|
-
return [...this.#files.values()];
|
|
363
|
-
};
|
|
364
|
-
|
|
365
|
-
/** 按"扩展名类型"筛:getByExt('.json') / getByExt('json') 都行 */
|
|
366
|
-
getByExt = (ext) => {
|
|
367
|
-
this.#ensureReady();
|
|
368
|
-
const e = normalizeExt(ext);
|
|
369
|
-
return this.getAll().filter((f) => f.ext === e);
|
|
370
|
-
};
|
|
371
|
-
|
|
372
|
-
/** 所有出现过的扩展名类型 */
|
|
373
|
-
extTypes = () => {
|
|
374
|
-
this.#ensureReady();
|
|
375
|
-
const set = new Set();
|
|
376
|
-
for (const f of this.#files.values()) set.add(f.ext);
|
|
377
|
-
return [...set];
|
|
378
|
-
};
|
|
379
|
-
|
|
380
|
-
/** 按目录筛:getByDir('config') -> data/config/**.{json,yaml,...} */
|
|
381
|
-
getByDir = (dir) => {
|
|
382
|
-
this.#ensureReady();
|
|
383
|
-
const norm = normalizeKey(dir);
|
|
384
|
-
const prefix = norm ? norm + '/' : '';
|
|
385
|
-
return this.getAll().filter((f) => f.key.startsWith(prefix));
|
|
386
|
-
};
|
|
307
|
+
}
|
|
387
308
|
|
|
388
309
|
/**
|
|
389
|
-
*
|
|
390
|
-
*
|
|
391
|
-
*
|
|
392
|
-
*
|
|
393
|
-
*
|
|
310
|
+
* 接收目录监听变更并增量更新扫描结果
|
|
311
|
+
* @description change.type 为 add/set/del/reload,path 为相对扫描根目录的路径;
|
|
312
|
+
* reload、空路径或非法条目触发全量重建;不关心的路径直接丢弃;
|
|
313
|
+
* 未命中的删除无法精确增量,同样退化为全量重建;
|
|
314
|
+
* 先规划再提交,重建时无需提前读取全量条目
|
|
315
|
+
* @param {Array<{ type: string, path: string }>} changes 监听变更
|
|
316
|
+
* @returns {Array<{ type: string, key: string, item: object }>} 标准化变更,type 为 add/change/unlink
|
|
394
317
|
*/
|
|
395
|
-
|
|
318
|
+
apply(changes) {
|
|
396
319
|
this.#ensureReady();
|
|
397
|
-
const dir_key = normalizeKey(dir);
|
|
398
|
-
return this.getAll().filter((f) => this.#matchDir(f.dir, dir_key) && this.#matchExt(f, ext));
|
|
399
|
-
};
|
|
400
320
|
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
321
|
+
let rebuild = changes.some((one) => {
|
|
322
|
+
const type = one.type;
|
|
323
|
+
return type === 'reload' || !one.path || (type !== 'add' && type !== 'set' && type !== 'del');
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
const planned = [];
|
|
327
|
+
if (!rebuild) {
|
|
328
|
+
for (const one of changes) {
|
|
329
|
+
const change = one.type === 'del' ? this.#planDel(one.path) : this.#planAdd(one.path);
|
|
330
|
+
if (change === null) continue;
|
|
331
|
+
if (change === false) {
|
|
332
|
+
rebuild = true;
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
planned.push(change);
|
|
336
|
+
}
|
|
408
337
|
}
|
|
409
|
-
return [...set];
|
|
410
|
-
};
|
|
411
338
|
|
|
412
|
-
|
|
413
|
-
|
|
339
|
+
if (rebuild) {
|
|
340
|
+
const before = this.#entries();
|
|
341
|
+
const entries = this.#walk();
|
|
342
|
+
this.#sync(entries);
|
|
343
|
+
return diffItems(before, [...entries.values()]);
|
|
344
|
+
}
|
|
414
345
|
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
* 其他实例不受通知,其监听会随真实文件事件自行收敛
|
|
419
|
-
* @returns {Scanner} 当前实例
|
|
420
|
-
*/
|
|
421
|
-
rescan = () => {
|
|
422
|
-
if (!this.#base_dir) throw new Error('[scanner] 尚未初始化,无法 rescan');
|
|
423
|
-
const before = [...this.#files.values()];
|
|
424
|
-
this.#walkAndFill();
|
|
425
|
-
this.#writeCache();
|
|
426
|
-
this.#notify(diffItems(before, this.getAll()));
|
|
427
|
-
return this;
|
|
428
|
-
};
|
|
346
|
+
for (const change of planned) this.#commit(change);
|
|
347
|
+
return planned;
|
|
348
|
+
}
|
|
429
349
|
|
|
430
350
|
/**
|
|
431
|
-
*
|
|
432
|
-
* @
|
|
433
|
-
* 监听器只声明监听哪个目录,忽略目录与扩展名白名单由本实例在事件处理中自行过滤;
|
|
434
|
-
* 重复调用只更新回调;不支持递归监听的环境会静默降级
|
|
435
|
-
* @param {(changes: Array<{ type: string, key: string, item: object }>) => void} [callback] 变更回调,type 为 add/change/unlink
|
|
436
|
-
* @returns {Scanner} 当前实例
|
|
351
|
+
* 全部扫描条目
|
|
352
|
+
* @returns {Array<object>} 条目数组
|
|
437
353
|
*/
|
|
438
|
-
|
|
354
|
+
getAll() {
|
|
439
355
|
this.#ensureReady();
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
if (this.#handlers) this.#detachHandlers();
|
|
443
|
-
|
|
444
|
-
const watcher = watcherAdmin(this.#base_dir, {
|
|
445
|
-
target: this.#base_dir,
|
|
446
|
-
debounce: WATCH_DEBOUNCE,
|
|
447
|
-
});
|
|
448
|
-
this.#watcher = watcher;
|
|
449
|
-
this.#handlers = {
|
|
450
|
-
add: (rel_path) => this.#onFileEvent(rel_path),
|
|
451
|
-
set: (rel_path) => this.#onFileEvent(rel_path),
|
|
452
|
-
del: (rel_path) => this.#onDelEvent(rel_path),
|
|
453
|
-
reload: () => this.#onReloadEvent(),
|
|
454
|
-
};
|
|
455
|
-
watcher.on('add', this.#handlers.add);
|
|
456
|
-
watcher.on('set', this.#handlers.set);
|
|
457
|
-
watcher.on('del', this.#handlers.del);
|
|
458
|
-
watcher.on('reload', this.#handlers.reload);
|
|
459
|
-
|
|
460
|
-
if (!watcher.listen()) {
|
|
461
|
-
this.#detachHandlers();
|
|
462
|
-
return this;
|
|
463
|
-
}
|
|
464
|
-
this.#markWatched();
|
|
465
|
-
return this;
|
|
466
|
-
};
|
|
356
|
+
return this.#entries();
|
|
357
|
+
}
|
|
467
358
|
|
|
468
359
|
/**
|
|
469
|
-
*
|
|
470
|
-
* @description
|
|
471
|
-
* 统一由 Scanner.clearCache() 释放全部监听器
|
|
360
|
+
* 重新扫描(开发环境热更新用)
|
|
361
|
+
* @description 强制遍历本实例目录并同步各类型 Store
|
|
472
362
|
* @returns {Scanner} 当前实例
|
|
473
363
|
*/
|
|
474
|
-
|
|
475
|
-
this.#
|
|
476
|
-
this.#
|
|
477
|
-
this.#markUnwatched();
|
|
364
|
+
rescan() {
|
|
365
|
+
if (!this.#base_dir) throw new Error('[scanner] 尚未初始化,无法 rescan');
|
|
366
|
+
this.#sync(this.#walk());
|
|
478
367
|
return this;
|
|
479
|
-
}
|
|
368
|
+
}
|
|
480
369
|
|
|
481
370
|
/**
|
|
482
|
-
*
|
|
371
|
+
* 清空本模块写入的类型 Store
|
|
483
372
|
* @returns {void}
|
|
484
373
|
*/
|
|
485
|
-
static clearCache
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
}
|
|
489
|
-
cache_store.clear();
|
|
490
|
-
};
|
|
374
|
+
static clearCache() {
|
|
375
|
+
clearTypeStores();
|
|
376
|
+
}
|
|
491
377
|
|
|
492
|
-
get size() {
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
378
|
+
get size() {
|
|
379
|
+
return this.#scanned ? this.#entries().length : 0;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
get baseDir() {
|
|
383
|
+
return this.#base_dir;
|
|
384
|
+
}
|
|
496
385
|
}
|
|
497
386
|
|
|
498
387
|
/* 导出类 */
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { storeAdmin } = require('mm_store');
|
|
4
|
+
|
|
5
|
+
/* 类型存储配置:主键为文件绝对路径,名称字段为文件名 */
|
|
6
|
+
const STORE_CONFIG = { key: 'file', name: 'name' };
|
|
7
|
+
|
|
8
|
+
/* 本模块使用过的类型,清理时只清这些 Store */
|
|
9
|
+
const types = new Set();
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 获取资源类型对应的存储实例
|
|
13
|
+
* @description 一类一个 Store,作用域即资源类型名;配置由本模块统一给出,
|
|
14
|
+
* 避免调用方各自传参导致 mm_store 按作用域缓存实例时配置不一致
|
|
15
|
+
* @param {string} type 资源类型
|
|
16
|
+
* @returns {object} mm_store 实例
|
|
17
|
+
*/
|
|
18
|
+
function typeStore(type) {
|
|
19
|
+
types.add(type);
|
|
20
|
+
return storeAdmin(type, STORE_CONFIG);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 清空本模块登记过的全部类型 Store
|
|
25
|
+
* @returns {void}
|
|
26
|
+
*/
|
|
27
|
+
function clearTypeStores() {
|
|
28
|
+
for (const type of types) {
|
|
29
|
+
typeStore(type).clear();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
module.exports = { STORE_CONFIG, 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.0",
|
|
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,8 +15,8 @@
|
|
|
15
15
|
"文件检索",
|
|
16
16
|
"scanner"
|
|
17
17
|
],
|
|
18
|
-
"layer": "
|
|
19
|
-
"group": "
|
|
18
|
+
"layer": "infrastructure",
|
|
19
|
+
"group": "resources",
|
|
20
20
|
"author": "邱文武",
|
|
21
21
|
"license": "ISC",
|
|
22
22
|
"main": "index.js",
|
|
@@ -35,8 +35,8 @@
|
|
|
35
35
|
"prepublishOnly": "npm test"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
38
|
+
"mm_admin": "^1.1.0",
|
|
39
|
+
"mm_store": "^1.0.1"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"jest": "^30.5.2"
|
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,14 +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
|
-
};
|
|
13
|
-
|
|
14
|
-
module.exports = { DEFAULT_CONFIG };
|