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