mm_scanner 5.0.3 → 6.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 +9 -36
- package/index.js +3 -18
- package/lib/admin.js +31 -0
- package/lib/scanner.js +56 -352
- package/package.json +5 -7
- package/lib/diff_util.js +0 -27
- package/lib/path_util.js +0 -39
- package/lib/type_store.js +0 -168
- package/tool/scan/index.js +0 -105
- package/tool/scan/tool.json +0 -57
- package/tool/scanner_admin/index.js +0 -88
- package/tool/scanner_admin/tool.json +0 -39
package/example/demo.js
CHANGED
|
@@ -1,50 +1,23 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const path = require('path');
|
|
4
|
-
|
|
4
|
+
const { Scanner } = require('../index');
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* 使用示例:扫描 example/data 目录,按资源类型分类并写入 mm_store
|
|
8
|
-
*/
|
|
9
6
|
function demo() {
|
|
10
7
|
let dir = path.join(__dirname, 'data');
|
|
11
|
-
|
|
12
|
-
// 1. 资源类型表:键为类型名,值为 { ext }
|
|
13
8
|
let resources = {
|
|
14
|
-
app:
|
|
15
|
-
api:
|
|
16
|
-
config:
|
|
17
|
-
script:
|
|
9
|
+
app: 'app.json',
|
|
10
|
+
api: 'api.json',
|
|
11
|
+
config: '.json',
|
|
12
|
+
script: '.js'
|
|
18
13
|
};
|
|
19
|
-
|
|
20
|
-
// 2. 创建扫描器,扫描时即完成分类
|
|
21
|
-
let scanner = new Scanner({ resources, ignore: ['dist'] });
|
|
14
|
+
let scanner = new Scanner({ resources });
|
|
22
15
|
scanner.scan(dir);
|
|
23
|
-
console.log('=== 扫描结果 ===');
|
|
24
|
-
console.log(' 基目录: ' + scanner.baseDir);
|
|
25
|
-
console.log(' 文件数: ' + scanner.size);
|
|
26
|
-
for (const item of scanner.getAll()) {
|
|
27
|
-
console.log(` [${item.type}] ${item.rel}`);
|
|
28
|
-
}
|
|
29
16
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
let list = typeStore(type).list({ root: scanner.baseDir });
|
|
34
|
-
console.log(` ${type}: ` + list.map((one) => one.name).join(', '));
|
|
17
|
+
console.log('=== 扫描结果 this.src ===');
|
|
18
|
+
for (let type of Object.keys(scanner.src)) {
|
|
19
|
+
console.log(` [${type}] ` + scanner.src[type].map((one) => one.name).join(', '));
|
|
35
20
|
}
|
|
36
|
-
|
|
37
|
-
// 4. 文件变化后重新扫描,Store 同步增删
|
|
38
|
-
scanner.rescan();
|
|
39
|
-
console.log('=== 重新扫描 ===');
|
|
40
|
-
console.log(' 文件数: ' + scanner.size);
|
|
41
|
-
|
|
42
|
-
// 5. 通过 admin 按目录复用扫描器实例
|
|
43
|
-
let first = scannerAdmin(dir, { resources });
|
|
44
|
-
let second = scannerAdmin(dir);
|
|
45
|
-
console.log('=== 目录实例 ===');
|
|
46
|
-
console.log(' 实例类型正确: ' + (first instanceof Scanner));
|
|
47
|
-
console.log(' 同目录复用同一实例: ' + (second === first));
|
|
48
21
|
}
|
|
49
22
|
|
|
50
23
|
demo();
|
package/index.js
CHANGED
|
@@ -1,23 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
2
|
const Scanner = require('./lib/scanner');
|
|
3
|
-
|
|
4
|
-
let { reg } = require('mm_admin');
|
|
5
|
-
|
|
6
|
-
let admin = reg('scanner');
|
|
7
|
-
admin.setClass(Scanner);
|
|
3
|
+
const adminLib = require('./lib/admin');
|
|
8
4
|
|
|
9
5
|
module.exports = {
|
|
10
6
|
Scanner,
|
|
11
|
-
|
|
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
|
-
}
|
|
7
|
+
...adminLib
|
|
23
8
|
};
|
package/lib/admin.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const { reg } = require('mm_admin');
|
|
2
|
+
const Scanner = require('./scanner.js');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 扫描器存储实例管理(非类文件)
|
|
6
|
+
* scannerAdmin 获取 mm_admin 'scanner' 注册池中的扫描器实例。
|
|
7
|
+
* 首次创建实例时按实例自身的 config.file 自动恢复;每个实例配置独立,互不干扰
|
|
8
|
+
*/
|
|
9
|
+
let admin = reg('scanner');
|
|
10
|
+
admin.setClass(Scanner);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 获取扫描器实例(带注册池管理)
|
|
14
|
+
* @param {string} [scope] 实例名称,缺省为 os
|
|
15
|
+
* @param {object} [config] 实例构造配置(仅在首次创建时生效)
|
|
16
|
+
* @returns {Scanner} 扫描器实例
|
|
17
|
+
*/
|
|
18
|
+
function scannerAdmin(scope, config) {
|
|
19
|
+
let name = scope || 'os';
|
|
20
|
+
let existed = admin.has(name);
|
|
21
|
+
let instance = admin.get(name, config);
|
|
22
|
+
if (!existed) {
|
|
23
|
+
instance.restore();
|
|
24
|
+
}
|
|
25
|
+
return instance;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
module.exports = {
|
|
29
|
+
admin,
|
|
30
|
+
scannerAdmin
|
|
31
|
+
};
|
package/lib/scanner.js
CHANGED
|
@@ -1,407 +1,111 @@
|
|
|
1
1
|
'use strict';
|
|
2
|
-
|
|
3
2
|
const fs = require('fs');
|
|
3
|
+
const { fullname } = require('mm_helper');
|
|
4
4
|
const path = require('path');
|
|
5
|
-
let { normalizeKey, createItem } = require('./path_util');
|
|
6
|
-
let { diffItems } = require('./diff_util');
|
|
7
|
-
let { typeStore, clearTypeStores } = require('./type_store');
|
|
8
5
|
|
|
9
|
-
/**
|
|
10
|
-
* 目录扫描器
|
|
11
|
-
* @description 按 Loader 的资源类型表递归扫描目录,扫描时即完成分类,
|
|
12
|
-
* 并按「一类一个轻量类型表」保存条目(Map 索引);
|
|
13
|
-
* 支持 apply 接收监听变更做增量更新,目录监听由调用方(mm_loader)负责
|
|
14
|
-
*/
|
|
6
|
+
/** 目录扫描器:遍历目录,把文件按后缀分组存入 this.src */
|
|
15
7
|
class Scanner {
|
|
16
|
-
/**
|
|
17
|
-
* 扫描器默认配置
|
|
18
|
-
*/
|
|
19
8
|
static config = {
|
|
20
|
-
/** 资源类型表,键为类型名,值为 { ext };扫描结果按类型分别写入 mm_store */
|
|
21
9
|
resources: {},
|
|
22
|
-
/** 忽略的目录 */
|
|
23
10
|
ignore: ['.git', '.svn', '.hg', 'node_modules', '.idea', '.vscode']
|
|
24
11
|
};
|
|
25
12
|
|
|
26
|
-
/** 扫描根目录绝对路径 */
|
|
27
|
-
#base_dir = null;
|
|
28
|
-
#scanned = false;
|
|
29
|
-
/** 配置变化后需重新分类 */
|
|
30
|
-
#stale = false;
|
|
31
|
-
|
|
32
13
|
/**
|
|
33
|
-
*
|
|
34
|
-
* @param {
|
|
14
|
+
* 构造函数
|
|
15
|
+
* @param {Object} config - 配置对象
|
|
35
16
|
*/
|
|
36
17
|
constructor(config = {}) {
|
|
37
|
-
this.config = {
|
|
38
|
-
|
|
39
|
-
ignore: [...Scanner.config.ignore],
|
|
40
|
-
};
|
|
18
|
+
this.config = {...Scanner.config};
|
|
19
|
+
this.src = {};
|
|
41
20
|
this.setConfig(config);
|
|
42
21
|
}
|
|
43
22
|
|
|
44
23
|
/**
|
|
45
|
-
*
|
|
46
|
-
* @
|
|
47
|
-
* ignore 按并集追加(默认忽略目录始终生效)
|
|
48
|
-
* @param {{ resources?: object, ignore?: string[] }} [config] - 扫描器配置
|
|
49
|
-
* @returns {void}
|
|
24
|
+
* 设置配置
|
|
25
|
+
* @param {Object} config - 配置对象
|
|
50
26
|
*/
|
|
51
27
|
setConfig(config = {}) {
|
|
52
|
-
this
|
|
53
|
-
this.#mergeIgnore(config.ignore);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/* -------- 私有 -------- */
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 合并资源类型表
|
|
60
|
-
* @param {object} [resources] 资源类型表
|
|
61
|
-
* @returns {void}
|
|
62
|
-
* @private
|
|
63
|
-
*/
|
|
64
|
-
#mergeResources(resources) {
|
|
65
|
-
if (!resources || typeof resources !== 'object' || Array.isArray(resources)) return;
|
|
66
|
-
let merged = { ...this.config.resources };
|
|
67
|
-
for (let type of Object.keys(resources)) {
|
|
68
|
-
let ext = resources[type] && resources[type].ext;
|
|
69
|
-
if (typeof ext !== 'string' || !ext) continue;
|
|
70
|
-
merged[type] = { ext: ext.toLowerCase() };
|
|
71
|
-
}
|
|
72
|
-
if (this.#sameResources(merged)) return;
|
|
73
|
-
this.config.resources = merged;
|
|
74
|
-
if (this.#scanned) this.#stale = true;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* 判断资源类型表是否与当前配置等价
|
|
79
|
-
* @param {object} resources 待比较的资源类型表
|
|
80
|
-
* @returns {boolean} 等价返回 true
|
|
81
|
-
* @private
|
|
82
|
-
*/
|
|
83
|
-
#sameResources(resources) {
|
|
84
|
-
let current = this.config.resources;
|
|
85
|
-
let keys = Object.keys(resources);
|
|
86
|
-
if (keys.length !== Object.keys(current).length) return false;
|
|
87
|
-
for (let type of keys) {
|
|
88
|
-
if (current[type].ext !== resources[type].ext) return false;
|
|
89
|
-
}
|
|
90
|
-
return true;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* 合并忽略目录
|
|
95
|
-
* @param {string[]} [ignore] 忽略目录列表
|
|
96
|
-
* @returns {void}
|
|
97
|
-
* @private
|
|
98
|
-
*/
|
|
99
|
-
#mergeIgnore(ignore) {
|
|
100
|
-
if (!Array.isArray(ignore)) return;
|
|
101
|
-
let merged = [...new Set([...this.config.ignore, ...ignore])];
|
|
102
|
-
if (merged.length === this.config.ignore.length) return;
|
|
103
|
-
this.config.ignore = merged;
|
|
104
|
-
if (this.#scanned) this.#stale = true;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* 按资源类型表对文件名分类
|
|
109
|
-
* @param {string} name 文件名
|
|
110
|
-
* @returns {string} 资源类型,未命中返回空字符串
|
|
111
|
-
* @private
|
|
112
|
-
*/
|
|
113
|
-
#typeOf(name) {
|
|
114
|
-
let file = String(name).toLowerCase();
|
|
115
|
-
let list = this.config.resources;
|
|
116
|
-
for (let type in list) {
|
|
117
|
-
if (file.endsWith(list[type].ext)) return type;
|
|
118
|
-
}
|
|
119
|
-
return '';
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* 遍历根目录并完成分类
|
|
124
|
-
* @returns {Map<string, object>} 文件绝对路径 -> 条目
|
|
125
|
-
* @private
|
|
126
|
-
*/
|
|
127
|
-
#walk() {
|
|
128
|
-
let entries = new Map();
|
|
129
|
-
this.#walkDir(this.#base_dir, entries);
|
|
130
|
-
this.#scanned = true;
|
|
131
|
-
this.#stale = false;
|
|
132
|
-
return entries;
|
|
28
|
+
this.config = { ...this.config, ...config };
|
|
133
29
|
}
|
|
134
30
|
|
|
135
31
|
/**
|
|
136
|
-
*
|
|
137
|
-
* @param {string} dir
|
|
138
|
-
* @
|
|
139
|
-
* @returns {void}
|
|
140
|
-
* @private
|
|
32
|
+
* 扫描目录,返回自身
|
|
33
|
+
* @param {string} dir - 目录路径
|
|
34
|
+
* @returns {Scanner} 自身
|
|
141
35
|
*/
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (this.config.ignore.includes(entry.name)) continue;
|
|
148
|
-
this.#walkDir(full_path, entries);
|
|
149
|
-
continue;
|
|
150
|
-
}
|
|
151
|
-
if (!entry.isFile()) continue;
|
|
152
|
-
let type = this.#typeOf(entry.name);
|
|
153
|
-
if (!type) continue;
|
|
154
|
-
let item = createItem(type, this.#base_dir, full_path);
|
|
155
|
-
entries.set(item.file, item);
|
|
156
|
-
}
|
|
36
|
+
scan(dir) {
|
|
37
|
+
this.src = {};
|
|
38
|
+
this.root = fullname(dir);
|
|
39
|
+
this._walk(this.root);
|
|
40
|
+
return this;
|
|
157
41
|
}
|
|
158
42
|
|
|
159
43
|
/**
|
|
160
|
-
*
|
|
161
|
-
* @
|
|
162
|
-
* 已存在时跳过,保留 loader 写入的 config
|
|
163
|
-
* @param {Map<string, object>} entries 本次遍历结果
|
|
164
|
-
* @returns {void}
|
|
165
|
-
* @private
|
|
44
|
+
* 遍历目录,递归扫描子目录
|
|
45
|
+
* @param {string} dir - 目录路径
|
|
166
46
|
*/
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
if (
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
for (let old of store.list({ root: this.#base_dir })) {
|
|
179
|
-
if (!keep.has(old.file)) store.delById(old.file);
|
|
180
|
-
}
|
|
181
|
-
for (let item of list) {
|
|
182
|
-
if (!store.getById(item.file)) store.add(item);
|
|
47
|
+
_walk(dir) {
|
|
48
|
+
for (const e of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
49
|
+
if (e.isDirectory()) {
|
|
50
|
+
if (!this.config.ignore.includes(e.name)) this._walk(path.join(dir, e.name));
|
|
51
|
+
} else if (e.isFile()) {
|
|
52
|
+
let type = this._typeOf(e.name);
|
|
53
|
+
if (!type) continue;
|
|
54
|
+
let file = path.join(dir, e.name);
|
|
55
|
+
(this.src[type] || (this.src[type] = [])).push({
|
|
56
|
+
file, name: e.name, type
|
|
57
|
+
});
|
|
183
58
|
}
|
|
184
59
|
}
|
|
185
60
|
}
|
|
186
61
|
|
|
187
62
|
/**
|
|
188
|
-
*
|
|
189
|
-
* @
|
|
190
|
-
* @returns {
|
|
191
|
-
* @private
|
|
63
|
+
* 根据文件名获取文件类型
|
|
64
|
+
* @param {string} name - 文件名
|
|
65
|
+
* @returns {string} 文件类型
|
|
192
66
|
*/
|
|
193
|
-
|
|
194
|
-
let
|
|
67
|
+
_typeOf(name) {
|
|
68
|
+
let lower = String(name).toLowerCase();
|
|
195
69
|
for (let type of Object.keys(this.config.resources)) {
|
|
196
|
-
|
|
70
|
+
if (lower.endsWith(this.config.resources[type].toLowerCase())) return type;
|
|
197
71
|
}
|
|
198
|
-
return
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
#ensureReady() {
|
|
202
|
-
if (!this.#scanned) {
|
|
203
|
-
throw new Error('[scanner] 尚未初始化,请先在入口调用 scanner.scan(dir)');
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* 取相对路径对应的资源类型
|
|
209
|
-
* @param {string} rel_path 规范化后的相对路径
|
|
210
|
-
* @returns {string} 资源类型,忽略目录下或未命中类型返回空字符串
|
|
211
|
-
* @private
|
|
212
|
-
*/
|
|
213
|
-
#typeOfRel(rel_path) {
|
|
214
|
-
let parts = rel_path.split('/');
|
|
215
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
216
|
-
if (this.config.ignore.includes(parts[i])) return '';
|
|
217
|
-
}
|
|
218
|
-
return this.#typeOf(parts[parts.length - 1]);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* 规划文件新增或修改
|
|
223
|
-
* @description 不关心的路径丢弃;stat 失败或不是文件按删除处理;
|
|
224
|
-
* Store 中已有条目视为 change,否则视为 add
|
|
225
|
-
* @param {string} rel_path 相对路径
|
|
226
|
-
* @returns {object|false|null} 变更条目;false 表示需全量重建;null 表示不关心
|
|
227
|
-
* @private
|
|
228
|
-
*/
|
|
229
|
-
#planAdd(rel_path) {
|
|
230
|
-
let key = normalizeKey(rel_path);
|
|
231
|
-
let type = this.#typeOfRel(key);
|
|
232
|
-
if (!type) return null;
|
|
233
|
-
|
|
234
|
-
let abs_path = path.join(this.#base_dir, key);
|
|
235
|
-
let stat = fs.statSync(abs_path, { throwIfNoEntry: false });
|
|
236
|
-
if (!stat || !stat.isFile()) {
|
|
237
|
-
return this.#planDel(rel_path);
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
let item = createItem(type, this.#base_dir, abs_path);
|
|
241
|
-
let old = typeStore(type).getById(item.file);
|
|
242
|
-
return { type: old ? 'change' : 'add', key: item.file, item };
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* 规划文件删除
|
|
247
|
-
* @description 不关心的路径丢弃;Store 未命中时无法精确增量,需全量重建兜底
|
|
248
|
-
* @param {string} rel_path 相对路径
|
|
249
|
-
* @returns {object|false|null} 变更条目;false 表示需全量重建;null 表示不关心
|
|
250
|
-
* @private
|
|
251
|
-
*/
|
|
252
|
-
#planDel(rel_path) {
|
|
253
|
-
let key = normalizeKey(rel_path);
|
|
254
|
-
let type = this.#typeOfRel(key);
|
|
255
|
-
if (!type) return null;
|
|
256
|
-
|
|
257
|
-
let file = path.join(this.#base_dir, key);
|
|
258
|
-
let old = typeStore(type).getById(file);
|
|
259
|
-
if (!old) return false;
|
|
260
|
-
return { type: 'unlink', key: file, item: old };
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
/**
|
|
264
|
-
* 提交单条变更到 Store
|
|
265
|
-
* @param {{ type: string, key: string, item: object }} change 变更条目
|
|
266
|
-
* @returns {void}
|
|
267
|
-
* @private
|
|
268
|
-
*/
|
|
269
|
-
#commit(change) {
|
|
270
|
-
let store = typeStore(change.item.type);
|
|
271
|
-
if (change.type === 'unlink') {
|
|
272
|
-
store.delById(change.key);
|
|
273
|
-
} else if (change.type === 'change') {
|
|
274
|
-
store.setById(change.key, change.item);
|
|
275
|
-
} else {
|
|
276
|
-
store.add(change.item);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
/* -------- 公开 API -------- */
|
|
281
|
-
|
|
282
|
-
/**
|
|
283
|
-
* 扫描目录(重复调用只生效一次)
|
|
284
|
-
* @description 已扫描时目录不同仅告警;配置变化(resources/ignore)会重新分类
|
|
285
|
-
* @param {string} dir 目录路径
|
|
286
|
-
* @param {{ resources?: object, ignore?: string[] }} [options] 扫描配置
|
|
287
|
-
* @returns {Scanner} 当前实例
|
|
288
|
-
*/
|
|
289
|
-
scan(dir, options = {}) {
|
|
290
|
-
let resolved = path.resolve(dir);
|
|
291
|
-
|
|
292
|
-
if (this.#scanned) {
|
|
293
|
-
if (this.#base_dir !== resolved) {
|
|
294
|
-
console.warn(`[scanner] 已使用 ${this.#base_dir} 扫描过,忽略本次 scan(${resolved})`);
|
|
295
|
-
return this;
|
|
296
|
-
}
|
|
297
|
-
this.setConfig(options);
|
|
298
|
-
if (this.#stale) {
|
|
299
|
-
this.#sync(this.#walk());
|
|
300
|
-
}
|
|
301
|
-
return this;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
let stat = fs.statSync(resolved, { throwIfNoEntry: false });
|
|
305
|
-
if (!stat) throw new Error(`[scanner] 目录不存在: ${resolved}`);
|
|
306
|
-
if (!stat.isDirectory()) throw new Error(`[scanner] 不是目录: ${resolved}`);
|
|
307
|
-
|
|
308
|
-
this.setConfig(options);
|
|
309
|
-
this.#base_dir = resolved;
|
|
310
|
-
this.#sync(this.#walk());
|
|
311
|
-
return this;
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* 接收目录监听变更并增量更新扫描结果
|
|
316
|
-
* @description change.type 为 add/set/del/reload,path 为相对扫描根目录的路径;
|
|
317
|
-
* reload、空路径或非法条目触发全量重建;不关心的路径直接丢弃;
|
|
318
|
-
* 未命中的删除无法精确增量,同样退化为全量重建;
|
|
319
|
-
* 先规划再提交,重建时无需提前读取全量条目
|
|
320
|
-
* @param {Array<{ type: string, path: string }>} changes 监听变更
|
|
321
|
-
* @returns {Array<{ type: string, key: string, item: object }>} 标准化变更,type 为 add/change/unlink
|
|
322
|
-
*/
|
|
323
|
-
apply(changes) {
|
|
324
|
-
this.#ensureReady();
|
|
325
|
-
|
|
326
|
-
let rebuild = changes.some((one) => {
|
|
327
|
-
let type = one.type;
|
|
328
|
-
return type === 'reload' || !one.path || (type !== 'add' && type !== 'set' && type !== 'del');
|
|
329
|
-
});
|
|
330
|
-
|
|
331
|
-
let planned = [];
|
|
332
|
-
if (!rebuild) {
|
|
333
|
-
for (let one of changes) {
|
|
334
|
-
let change = one.type === 'del' ? this.#planDel(one.path) : this.#planAdd(one.path);
|
|
335
|
-
if (change === null) continue;
|
|
336
|
-
if (change === false) {
|
|
337
|
-
rebuild = true;
|
|
338
|
-
break;
|
|
339
|
-
}
|
|
340
|
-
planned.push(change);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
if (rebuild) {
|
|
345
|
-
let before = this.#entries();
|
|
346
|
-
let entries = this.#walk();
|
|
347
|
-
this.#sync(entries);
|
|
348
|
-
return diffItems(before, [...entries.values()]);
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
for (let change of planned) this.#commit(change);
|
|
352
|
-
return planned;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* 全部扫描条目
|
|
357
|
-
* @returns {Array<object>} 条目数组
|
|
358
|
-
*/
|
|
359
|
-
getAll() {
|
|
360
|
-
this.#ensureReady();
|
|
361
|
-
return this.#entries();
|
|
72
|
+
return '';
|
|
362
73
|
}
|
|
363
74
|
|
|
364
75
|
/**
|
|
365
|
-
*
|
|
366
|
-
* @
|
|
367
|
-
* @returns {
|
|
76
|
+
* 获取指定类型的文件列表
|
|
77
|
+
* @param {string} type - 文件类型
|
|
78
|
+
* @returns {Array} 文件列表
|
|
368
79
|
*/
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
this.#sync(this.#walk());
|
|
372
|
-
return this;
|
|
80
|
+
getFiles(type) {
|
|
81
|
+
return this.src[type] || [];
|
|
373
82
|
}
|
|
374
83
|
|
|
375
84
|
/**
|
|
376
|
-
*
|
|
377
|
-
* @
|
|
85
|
+
* 删除指定类型的文件
|
|
86
|
+
* @param {string} type - 文件类型
|
|
87
|
+
* @param {string} file - 文件路径
|
|
378
88
|
*/
|
|
379
|
-
|
|
380
|
-
|
|
89
|
+
delFiles(type, file) {
|
|
90
|
+
delete this.src[type];
|
|
381
91
|
}
|
|
382
92
|
|
|
383
93
|
/**
|
|
384
|
-
*
|
|
385
|
-
* @
|
|
386
|
-
* @
|
|
94
|
+
* 设置指定类型的文件后缀
|
|
95
|
+
* @param {string} type - 文件类型
|
|
96
|
+
* @param {string} ext - 文件后缀
|
|
387
97
|
*/
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
let total = 0;
|
|
391
|
-
for (let type of Object.keys(this.config.resources)) {
|
|
392
|
-
total += typeStore(type).count({ root: this.#base_dir });
|
|
393
|
-
}
|
|
394
|
-
return total;
|
|
98
|
+
setType(type, ext) {
|
|
99
|
+
this.config.resources[type] = ext;
|
|
395
100
|
}
|
|
396
101
|
|
|
397
102
|
/**
|
|
398
|
-
*
|
|
399
|
-
* @
|
|
103
|
+
* 删除指定类型的文件后缀
|
|
104
|
+
* @param {string} type - 文件类型
|
|
400
105
|
*/
|
|
401
|
-
|
|
402
|
-
|
|
106
|
+
delType(type) {
|
|
107
|
+
delete this.config.resources[type];
|
|
403
108
|
}
|
|
404
109
|
}
|
|
405
110
|
|
|
406
|
-
/* 导出类 */
|
|
407
111
|
module.exports = Scanner;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_scanner",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
|
+
"description": "轻量目录扫描器:递归遍历目录、按后缀分组写入 this.src,默认忽略 .git/node_modules 等常见目录。",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://gitee.com/qiuwenwu91/mm_scanner.git"
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"files": [
|
|
24
24
|
"index.js",
|
|
25
25
|
"lib",
|
|
26
|
-
"tool",
|
|
27
26
|
"example",
|
|
28
27
|
"data"
|
|
29
28
|
],
|
|
@@ -36,11 +35,10 @@
|
|
|
36
35
|
"demo": "node example/demo.js",
|
|
37
36
|
"prepublishOnly": "npm test"
|
|
38
37
|
},
|
|
39
|
-
"dependencies": {
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"mm_helper": "^2.0.5"
|
|
40
|
+
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"jest": "^30.5.2"
|
|
42
|
-
},
|
|
43
|
-
"overrides": {
|
|
44
|
-
"mm_persist": "0.0.0-stage"
|
|
45
43
|
}
|
|
46
44
|
}
|
package/lib/diff_util.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 对比两份条目集合,生成增量变更
|
|
5
|
-
* @description 条目为纯路径元信息,主键(绝对路径)相同即条目相同,只产生新增与删除
|
|
6
|
-
* @param {Array<object>} before 变更前的条目
|
|
7
|
-
* @param {Array<object>} after 变更后的条目
|
|
8
|
-
* @returns {Array<{ type: string, key: string, item: object }>} 变更列表,type 为 add/unlink
|
|
9
|
-
*/
|
|
10
|
-
function diffItems(before, after) {
|
|
11
|
-
let before_map = new Map();
|
|
12
|
-
for (let i = 0; i < before.length; i++) before_map.set(before[i].file, before[i]);
|
|
13
|
-
|
|
14
|
-
let after_map = new Map();
|
|
15
|
-
for (let i = 0; i < after.length; i++) after_map.set(after[i].file, after[i]);
|
|
16
|
-
|
|
17
|
-
let changes = [];
|
|
18
|
-
for (let [key, item] of after_map) {
|
|
19
|
-
if (!before_map.has(key)) changes.push({ type: 'add', key, item });
|
|
20
|
-
}
|
|
21
|
-
for (let [key, item] of before_map) {
|
|
22
|
-
if (!after_map.has(key)) changes.push({ type: 'unlink', key, item });
|
|
23
|
-
}
|
|
24
|
-
return changes;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
module.exports = { diffItems };
|
package/lib/path_util.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const path = require('path');
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* key 规范化:统一分隔符、去 "./" 前缀与首尾斜杠
|
|
7
|
-
* @param {string} p 原始路径
|
|
8
|
-
* @returns {string} 规范化后的路径键
|
|
9
|
-
*/
|
|
10
|
-
function normalizeKey(p) {
|
|
11
|
-
return String(p)
|
|
12
|
-
.replace(/\\/g, '/')
|
|
13
|
-
.replace(/^\.\//, '')
|
|
14
|
-
.replace(/^\/+|\/+$/g, '');
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 构造单个文件的条目元信息
|
|
19
|
-
* @param {string} type 资源类型
|
|
20
|
-
* @param {string} root 扫描根目录绝对路径
|
|
21
|
-
* @param {string} abs_path 文件绝对路径
|
|
22
|
-
* @returns {{ file: string, name: string, root: string, dir: string, rel: string, ext: string, type: string }} 条目
|
|
23
|
-
*/
|
|
24
|
-
function createItem(type, root, abs_path) {
|
|
25
|
-
let rel = path.relative(root, abs_path).replace(/\\/g, '/');
|
|
26
|
-
let ext = path.extname(rel).toLowerCase();
|
|
27
|
-
let dir_part = path.posix.dirname(rel);
|
|
28
|
-
return {
|
|
29
|
-
file: abs_path,
|
|
30
|
-
name: path.basename(rel),
|
|
31
|
-
root,
|
|
32
|
-
dir: dir_part === '.' ? '' : dir_part,
|
|
33
|
-
rel,
|
|
34
|
-
ext,
|
|
35
|
-
type,
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
module.exports = { normalizeKey, createItem };
|
package/lib/type_store.js
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 轻量类型表
|
|
5
|
-
* @description 按资源类型隔离条目,以主键(file)建 Map 索引,读写为 O(1)。
|
|
6
|
-
* 不依赖 mm_store/mm_admin:后者会把 config.name 覆盖为作用域名,
|
|
7
|
-
* 与本表「name 为名称字段名」的语义冲突。
|
|
8
|
-
*/
|
|
9
|
-
class TypeStore {
|
|
10
|
-
/**
|
|
11
|
-
* @param {string} type 资源类型
|
|
12
|
-
*/
|
|
13
|
-
constructor(type) {
|
|
14
|
-
this.config = { key: 'file', name: 'name' };
|
|
15
|
-
this.type = type;
|
|
16
|
-
/** @type {Map<string, object>} 主键 -> 条目 */
|
|
17
|
-
this._map = new Map();
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 获取条目
|
|
22
|
-
* @param {string} id 主键值(文件路径)
|
|
23
|
-
* @returns {object|undefined} 条目
|
|
24
|
-
*/
|
|
25
|
-
getById(id) {
|
|
26
|
-
return this._map.get(id);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* 新增条目
|
|
31
|
-
* @param {object} item 条目(须含 file 与 name)
|
|
32
|
-
* @returns {boolean} 是否新增成功
|
|
33
|
-
*/
|
|
34
|
-
add(item) {
|
|
35
|
-
let id = item && item[this.config.key];
|
|
36
|
-
if (!id || this._map.has(id)) return false;
|
|
37
|
-
this._map.set(id, item);
|
|
38
|
-
return true;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 合并更新条目,保留原主键
|
|
43
|
-
* @param {string} id 主键值
|
|
44
|
-
* @param {object} item 更新的字段
|
|
45
|
-
* @returns {boolean} 是否存在并更新
|
|
46
|
-
*/
|
|
47
|
-
setById(id, item) {
|
|
48
|
-
let old = this._map.get(id);
|
|
49
|
-
if (!old) return false;
|
|
50
|
-
this._map.set(id, { ...old, ...item, [this.config.key]: id });
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 删除条目
|
|
56
|
-
* @param {string} id 主键值
|
|
57
|
-
* @returns {boolean} 是否删除成功
|
|
58
|
-
*/
|
|
59
|
-
delById(id) {
|
|
60
|
-
return this._map.delete(id);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 按属性匹配删除
|
|
65
|
-
* @param {object} query 匹配条件(如 { root } 或 { root, all: true })
|
|
66
|
-
* @param {boolean} [all] 是否删除全部匹配项
|
|
67
|
-
* @returns {boolean} 是否删除成功
|
|
68
|
-
*/
|
|
69
|
-
del(query, all = false) {
|
|
70
|
-
if (!query) {
|
|
71
|
-
let had = this._map.size > 0;
|
|
72
|
-
this._map.clear();
|
|
73
|
-
return had;
|
|
74
|
-
}
|
|
75
|
-
let { all: query_all, ...filter } = query;
|
|
76
|
-
let remove_all = all || query_all === true;
|
|
77
|
-
let found = false;
|
|
78
|
-
for (let [id, item] of this._map) {
|
|
79
|
-
if (!this._match(item, filter)) continue;
|
|
80
|
-
found = true;
|
|
81
|
-
this._map.delete(id);
|
|
82
|
-
if (!remove_all) break;
|
|
83
|
-
}
|
|
84
|
-
return found;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* 按属性匹配条目
|
|
89
|
-
* @param {object} [query] 匹配条件,可含 root 与分页字段
|
|
90
|
-
* @returns {Array<object>} 条目数组
|
|
91
|
-
*/
|
|
92
|
-
list(query) {
|
|
93
|
-
if (!query) return [...this._map.values()];
|
|
94
|
-
let { page_now, page_size, keyword, orderby, groupby, ...filter } = query;
|
|
95
|
-
let list = [];
|
|
96
|
-
for (let item of this._map.values()) {
|
|
97
|
-
if (this._match(item, filter)) list.push(item);
|
|
98
|
-
}
|
|
99
|
-
if (!page_now) return list;
|
|
100
|
-
let size = page_size || 10;
|
|
101
|
-
let offset = (page_now - 1) * size;
|
|
102
|
-
return list.slice(offset, offset + size);
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* 按属性匹配计数
|
|
107
|
-
* @param {object} [query] 匹配条件
|
|
108
|
-
* @returns {number} 匹配数量
|
|
109
|
-
*/
|
|
110
|
-
count(query) {
|
|
111
|
-
if (!query) return this._map.size;
|
|
112
|
-
let { page_now, page_size, keyword, orderby, groupby, ...filter } = query;
|
|
113
|
-
let total = 0;
|
|
114
|
-
for (let item of this._map.values()) {
|
|
115
|
-
if (this._match(item, filter)) total++;
|
|
116
|
-
}
|
|
117
|
-
return total;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* 判断条目是否匹配全部过滤条件
|
|
122
|
-
* @param {object} item 条目
|
|
123
|
-
* @param {object} filter 过滤条件
|
|
124
|
-
* @returns {boolean} 是否匹配
|
|
125
|
-
* @private
|
|
126
|
-
*/
|
|
127
|
-
_match(item, filter) {
|
|
128
|
-
for (let key of Object.keys(filter)) {
|
|
129
|
-
if (item[key] !== filter[key]) return false;
|
|
130
|
-
}
|
|
131
|
-
return true;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* 清空条目
|
|
136
|
-
* @returns {void}
|
|
137
|
-
*/
|
|
138
|
-
clear() {
|
|
139
|
-
this._map.clear();
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/** @type {Map<string, TypeStore>} 类型 -> 类型表 */
|
|
144
|
-
let stores = new Map();
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* 获取资源类型对应的类型表(同类型同实例)
|
|
148
|
-
* @param {string} type 资源类型
|
|
149
|
-
* @returns {TypeStore} 类型表
|
|
150
|
-
*/
|
|
151
|
-
function typeStore(type) {
|
|
152
|
-
let store = stores.get(type);
|
|
153
|
-
if (!store) {
|
|
154
|
-
store = new TypeStore(type);
|
|
155
|
-
stores.set(type, store);
|
|
156
|
-
}
|
|
157
|
-
return store;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* 清空全部类型表
|
|
162
|
-
* @returns {void}
|
|
163
|
-
*/
|
|
164
|
-
function clearTypeStores() {
|
|
165
|
-
for (let store of stores.values()) store.clear();
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
module.exports = { typeStore, clearTypeStores };
|
package/tool/scan/index.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
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 };
|
package/tool/scan/tool.json
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,88 +0,0 @@
|
|
|
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 };
|
|
@@ -1,39 +0,0 @@
|
|
|
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
|
-
}
|