mm_scanner 5.0.0 → 5.0.2
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/data/scanner.const.json +6 -0
- package/example/demo.js +7 -7
- package/index.js +3 -3
- package/lib/diff_util.js +9 -15
- package/lib/path_util.js +3 -3
- package/lib/scanner.js +86 -67
- package/lib/type_store.js +21 -9
- package/package.json +13 -6
- package/tool/scan/index.js +105 -0
- package/tool/scan/tool.json +57 -0
- package/tool/scanner_admin/index.js +88 -0
- package/tool/scanner_admin/tool.json +39 -0
package/example/demo.js
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const path = require('path');
|
|
4
|
-
|
|
4
|
+
let { Scanner, scannerAdmin, typeStore } = require('../index');
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* 使用示例:扫描 example/data 目录,按资源类型分类并写入 mm_store
|
|
8
8
|
*/
|
|
9
9
|
function demo() {
|
|
10
|
-
|
|
10
|
+
let dir = path.join(__dirname, 'data');
|
|
11
11
|
|
|
12
12
|
// 1. 资源类型表:键为类型名,值为 { ext }
|
|
13
|
-
|
|
13
|
+
let resources = {
|
|
14
14
|
app: { ext: 'app.json' },
|
|
15
15
|
api: { ext: 'api.json' },
|
|
16
16
|
config: { ext: '.json' },
|
|
@@ -18,7 +18,7 @@ function demo() {
|
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
// 2. 创建扫描器,扫描时即完成分类
|
|
21
|
-
|
|
21
|
+
let scanner = new Scanner({ resources, ignore: ['dist'] });
|
|
22
22
|
scanner.scan(dir);
|
|
23
23
|
console.log('=== 扫描结果 ===');
|
|
24
24
|
console.log(' 基目录: ' + scanner.baseDir);
|
|
@@ -30,7 +30,7 @@ function demo() {
|
|
|
30
30
|
// 3. 一类一个 Store:作用域就是资源类型名
|
|
31
31
|
console.log('=== 按类型取切片 ===');
|
|
32
32
|
for (const type of Object.keys(resources)) {
|
|
33
|
-
|
|
33
|
+
let list = typeStore(type).list({ root: scanner.baseDir });
|
|
34
34
|
console.log(` ${type}: ` + list.map((one) => one.name).join(', '));
|
|
35
35
|
}
|
|
36
36
|
|
|
@@ -40,8 +40,8 @@ function demo() {
|
|
|
40
40
|
console.log(' 文件数: ' + scanner.size);
|
|
41
41
|
|
|
42
42
|
// 5. 通过 admin 按目录复用扫描器实例
|
|
43
|
-
|
|
44
|
-
|
|
43
|
+
let first = scannerAdmin(dir, { resources });
|
|
44
|
+
let second = scannerAdmin(dir);
|
|
45
45
|
console.log('=== 目录实例 ===');
|
|
46
46
|
console.log(' 实例类型正确: ' + (first instanceof Scanner));
|
|
47
47
|
console.log(' 同目录复用同一实例: ' + (second === first));
|
package/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
2
|
const Scanner = require('./lib/scanner');
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
let { typeStore } = require('./lib/type_store');
|
|
4
|
+
let { reg } = require('mm_admin');
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
let admin = reg('scanner');
|
|
7
7
|
admin.setClass(Scanner);
|
|
8
8
|
|
|
9
9
|
module.exports = {
|
package/lib/diff_util.js
CHANGED
|
@@ -2,30 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* 对比两份条目集合,生成增量变更
|
|
5
|
+
* @description 条目为纯路径元信息,主键(绝对路径)相同即条目相同,只产生新增与删除
|
|
5
6
|
* @param {Array<object>} before 变更前的条目
|
|
6
7
|
* @param {Array<object>} after 变更后的条目
|
|
7
|
-
* @returns {Array<{ type: string, key: string, item: object }>}
|
|
8
|
+
* @returns {Array<{ type: string, key: string, item: object }>} 变更列表,type 为 add/unlink
|
|
8
9
|
*/
|
|
9
10
|
function diffItems(before, after) {
|
|
10
|
-
|
|
11
|
+
let before_map = new Map();
|
|
11
12
|
for (let i = 0; i < before.length; i++) before_map.set(before[i].file, before[i]);
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
let after_map = new Map();
|
|
14
15
|
for (let i = 0; i < after.length; i++) after_map.set(after[i].file, after[i]);
|
|
15
16
|
|
|
16
|
-
|
|
17
|
-
for (
|
|
18
|
-
|
|
19
|
-
if (!old) {
|
|
20
|
-
changes.push({ type: 'add', key, item: { ...item } });
|
|
21
|
-
continue;
|
|
22
|
-
}
|
|
23
|
-
if (old.rel !== item.rel) {
|
|
24
|
-
changes.push({ type: 'change', key, item: { ...item } });
|
|
25
|
-
}
|
|
17
|
+
let changes = [];
|
|
18
|
+
for (let [key, item] of after_map) {
|
|
19
|
+
if (!before_map.has(key)) changes.push({ type: 'add', key, item });
|
|
26
20
|
}
|
|
27
|
-
for (
|
|
28
|
-
if (!after_map.has(key)) changes.push({ type: 'unlink', key, item
|
|
21
|
+
for (let [key, item] of before_map) {
|
|
22
|
+
if (!after_map.has(key)) changes.push({ type: 'unlink', key, item });
|
|
29
23
|
}
|
|
30
24
|
return changes;
|
|
31
25
|
}
|
package/lib/path_util.js
CHANGED
|
@@ -22,9 +22,9 @@ function normalizeKey(p) {
|
|
|
22
22
|
* @returns {{ file: string, name: string, root: string, dir: string, rel: string, ext: string, type: string }} 条目
|
|
23
23
|
*/
|
|
24
24
|
function createItem(type, root, abs_path) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
28
|
return {
|
|
29
29
|
file: abs_path,
|
|
30
30
|
name: path.basename(rel),
|
package/lib/scanner.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
const fs = require('fs');
|
|
4
4
|
const path = require('path');
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
let { normalizeKey, createItem } = require('./path_util');
|
|
6
|
+
let { diffItems } = require('./diff_util');
|
|
7
|
+
let { typeStore, clearTypeStores } = require('./type_store');
|
|
8
8
|
|
|
9
9
|
/**
|
|
10
10
|
* 目录扫描器
|
|
@@ -63,7 +63,12 @@ class Scanner {
|
|
|
63
63
|
*/
|
|
64
64
|
#mergeResources(resources) {
|
|
65
65
|
if (!resources || typeof resources !== 'object' || Array.isArray(resources)) return;
|
|
66
|
-
|
|
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
|
+
}
|
|
67
72
|
if (this.#sameResources(merged)) return;
|
|
68
73
|
this.config.resources = merged;
|
|
69
74
|
if (this.#scanned) this.#stale = true;
|
|
@@ -76,11 +81,11 @@ class Scanner {
|
|
|
76
81
|
* @private
|
|
77
82
|
*/
|
|
78
83
|
#sameResources(resources) {
|
|
79
|
-
|
|
80
|
-
|
|
84
|
+
let current = this.config.resources;
|
|
85
|
+
let keys = Object.keys(resources);
|
|
81
86
|
if (keys.length !== Object.keys(current).length) return false;
|
|
82
|
-
for (
|
|
83
|
-
if (
|
|
87
|
+
for (let type of keys) {
|
|
88
|
+
if (current[type].ext !== resources[type].ext) return false;
|
|
84
89
|
}
|
|
85
90
|
return true;
|
|
86
91
|
}
|
|
@@ -93,7 +98,7 @@ class Scanner {
|
|
|
93
98
|
*/
|
|
94
99
|
#mergeIgnore(ignore) {
|
|
95
100
|
if (!Array.isArray(ignore)) return;
|
|
96
|
-
|
|
101
|
+
let merged = [...new Set([...this.config.ignore, ...ignore])];
|
|
97
102
|
if (merged.length === this.config.ignore.length) return;
|
|
98
103
|
this.config.ignore = merged;
|
|
99
104
|
if (this.#scanned) this.#stale = true;
|
|
@@ -106,11 +111,10 @@ class Scanner {
|
|
|
106
111
|
* @private
|
|
107
112
|
*/
|
|
108
113
|
#typeOf(name) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
for (
|
|
112
|
-
|
|
113
|
-
if (ext && file.endsWith(ext)) return type;
|
|
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;
|
|
114
118
|
}
|
|
115
119
|
return '';
|
|
116
120
|
}
|
|
@@ -121,7 +125,7 @@ class Scanner {
|
|
|
121
125
|
* @private
|
|
122
126
|
*/
|
|
123
127
|
#walk() {
|
|
124
|
-
|
|
128
|
+
let entries = new Map();
|
|
125
129
|
this.#walkDir(this.#base_dir, entries);
|
|
126
130
|
this.#scanned = true;
|
|
127
131
|
this.#stale = false;
|
|
@@ -136,18 +140,18 @@ class Scanner {
|
|
|
136
140
|
* @private
|
|
137
141
|
*/
|
|
138
142
|
#walkDir(dir, entries) {
|
|
139
|
-
|
|
140
|
-
for (
|
|
141
|
-
|
|
143
|
+
let list = fs.readdirSync(dir, { withFileTypes: true });
|
|
144
|
+
for (let entry of list) {
|
|
145
|
+
let full_path = path.join(dir, entry.name);
|
|
142
146
|
if (entry.isDirectory()) {
|
|
143
147
|
if (this.config.ignore.includes(entry.name)) continue;
|
|
144
148
|
this.#walkDir(full_path, entries);
|
|
145
149
|
continue;
|
|
146
150
|
}
|
|
147
151
|
if (!entry.isFile()) continue;
|
|
148
|
-
|
|
152
|
+
let type = this.#typeOf(entry.name);
|
|
149
153
|
if (!type) continue;
|
|
150
|
-
|
|
154
|
+
let item = createItem(type, this.#base_dir, full_path);
|
|
151
155
|
entries.set(item.file, item);
|
|
152
156
|
}
|
|
153
157
|
}
|
|
@@ -161,17 +165,20 @@ class Scanner {
|
|
|
161
165
|
* @private
|
|
162
166
|
*/
|
|
163
167
|
#sync(entries) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
168
|
+
let groups = new Map();
|
|
169
|
+
for (let item of entries.values()) {
|
|
170
|
+
let list = groups.get(item.type);
|
|
171
|
+
if (list) list.push(item);
|
|
172
|
+
else groups.set(item.type, [item]);
|
|
173
|
+
}
|
|
174
|
+
for (let type of Object.keys(this.config.resources)) {
|
|
175
|
+
let store = typeStore(type);
|
|
176
|
+
let list = groups.get(type) || [];
|
|
177
|
+
let keep = new Set(list.map((one) => one.file));
|
|
178
|
+
for (let old of store.list({ root: this.#base_dir })) {
|
|
172
179
|
if (!keep.has(old.file)) store.delById(old.file);
|
|
173
180
|
}
|
|
174
|
-
for (
|
|
181
|
+
for (let item of list) {
|
|
175
182
|
if (!store.getById(item.file)) store.add(item);
|
|
176
183
|
}
|
|
177
184
|
}
|
|
@@ -184,9 +191,9 @@ class Scanner {
|
|
|
184
191
|
* @private
|
|
185
192
|
*/
|
|
186
193
|
#entries() {
|
|
187
|
-
|
|
188
|
-
for (
|
|
189
|
-
for (
|
|
194
|
+
let list = [];
|
|
195
|
+
for (let type of Object.keys(this.config.resources)) {
|
|
196
|
+
for (let item of typeStore(type).list({ root: this.#base_dir })) list.push(item);
|
|
190
197
|
}
|
|
191
198
|
return list;
|
|
192
199
|
}
|
|
@@ -198,17 +205,17 @@ class Scanner {
|
|
|
198
205
|
}
|
|
199
206
|
|
|
200
207
|
/**
|
|
201
|
-
*
|
|
208
|
+
* 取相对路径对应的资源类型
|
|
202
209
|
* @param {string} rel_path 规范化后的相对路径
|
|
203
|
-
* @returns {
|
|
210
|
+
* @returns {string} 资源类型,忽略目录下或未命中类型返回空字符串
|
|
204
211
|
* @private
|
|
205
212
|
*/
|
|
206
|
-
#
|
|
207
|
-
|
|
213
|
+
#typeOfRel(rel_path) {
|
|
214
|
+
let parts = rel_path.split('/');
|
|
208
215
|
for (let i = 0; i < parts.length - 1; i++) {
|
|
209
|
-
if (this.config.ignore.includes(parts[i])) return
|
|
216
|
+
if (this.config.ignore.includes(parts[i])) return '';
|
|
210
217
|
}
|
|
211
|
-
return this.#typeOf(parts[parts.length - 1])
|
|
218
|
+
return this.#typeOf(parts[parts.length - 1]);
|
|
212
219
|
}
|
|
213
220
|
|
|
214
221
|
/**
|
|
@@ -220,22 +227,18 @@ class Scanner {
|
|
|
220
227
|
* @private
|
|
221
228
|
*/
|
|
222
229
|
#planAdd(rel_path) {
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
let
|
|
228
|
-
|
|
229
|
-
stat = fs.statSync(abs_path);
|
|
230
|
-
} catch {
|
|
231
|
-
stat = null;
|
|
232
|
-
}
|
|
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 });
|
|
233
236
|
if (!stat || !stat.isFile()) {
|
|
234
237
|
return this.#planDel(rel_path);
|
|
235
238
|
}
|
|
236
239
|
|
|
237
|
-
|
|
238
|
-
|
|
240
|
+
let item = createItem(type, this.#base_dir, abs_path);
|
|
241
|
+
let old = typeStore(type).getById(item.file);
|
|
239
242
|
return { type: old ? 'change' : 'add', key: item.file, item };
|
|
240
243
|
}
|
|
241
244
|
|
|
@@ -247,12 +250,12 @@ class Scanner {
|
|
|
247
250
|
* @private
|
|
248
251
|
*/
|
|
249
252
|
#planDel(rel_path) {
|
|
250
|
-
|
|
251
|
-
|
|
253
|
+
let key = normalizeKey(rel_path);
|
|
254
|
+
let type = this.#typeOfRel(key);
|
|
255
|
+
if (!type) return null;
|
|
252
256
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
const old = store.getById(file);
|
|
257
|
+
let file = path.join(this.#base_dir, key);
|
|
258
|
+
let old = typeStore(type).getById(file);
|
|
256
259
|
if (!old) return false;
|
|
257
260
|
return { type: 'unlink', key: file, item: old };
|
|
258
261
|
}
|
|
@@ -264,7 +267,7 @@ class Scanner {
|
|
|
264
267
|
* @private
|
|
265
268
|
*/
|
|
266
269
|
#commit(change) {
|
|
267
|
-
|
|
270
|
+
let store = typeStore(change.item.type);
|
|
268
271
|
if (change.type === 'unlink') {
|
|
269
272
|
store.delById(change.key);
|
|
270
273
|
} else if (change.type === 'change') {
|
|
@@ -284,21 +287,23 @@ class Scanner {
|
|
|
284
287
|
* @returns {Scanner} 当前实例
|
|
285
288
|
*/
|
|
286
289
|
scan(dir, options = {}) {
|
|
287
|
-
|
|
290
|
+
let resolved = path.resolve(dir);
|
|
288
291
|
|
|
289
292
|
if (this.#scanned) {
|
|
290
293
|
if (this.#base_dir !== resolved) {
|
|
291
294
|
console.warn(`[scanner] 已使用 ${this.#base_dir} 扫描过,忽略本次 scan(${resolved})`);
|
|
292
295
|
return this;
|
|
293
296
|
}
|
|
297
|
+
this.setConfig(options);
|
|
294
298
|
if (this.#stale) {
|
|
295
299
|
this.#sync(this.#walk());
|
|
296
300
|
}
|
|
297
301
|
return this;
|
|
298
302
|
}
|
|
299
303
|
|
|
300
|
-
|
|
301
|
-
if (!
|
|
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}`);
|
|
302
307
|
|
|
303
308
|
this.setConfig(options);
|
|
304
309
|
this.#base_dir = resolved;
|
|
@@ -319,14 +324,14 @@ class Scanner {
|
|
|
319
324
|
this.#ensureReady();
|
|
320
325
|
|
|
321
326
|
let rebuild = changes.some((one) => {
|
|
322
|
-
|
|
327
|
+
let type = one.type;
|
|
323
328
|
return type === 'reload' || !one.path || (type !== 'add' && type !== 'set' && type !== 'del');
|
|
324
329
|
});
|
|
325
330
|
|
|
326
|
-
|
|
331
|
+
let planned = [];
|
|
327
332
|
if (!rebuild) {
|
|
328
|
-
for (
|
|
329
|
-
|
|
333
|
+
for (let one of changes) {
|
|
334
|
+
let change = one.type === 'del' ? this.#planDel(one.path) : this.#planAdd(one.path);
|
|
330
335
|
if (change === null) continue;
|
|
331
336
|
if (change === false) {
|
|
332
337
|
rebuild = true;
|
|
@@ -337,13 +342,13 @@ class Scanner {
|
|
|
337
342
|
}
|
|
338
343
|
|
|
339
344
|
if (rebuild) {
|
|
340
|
-
|
|
341
|
-
|
|
345
|
+
let before = this.#entries();
|
|
346
|
+
let entries = this.#walk();
|
|
342
347
|
this.#sync(entries);
|
|
343
348
|
return diffItems(before, [...entries.values()]);
|
|
344
349
|
}
|
|
345
350
|
|
|
346
|
-
for (
|
|
351
|
+
for (let change of planned) this.#commit(change);
|
|
347
352
|
return planned;
|
|
348
353
|
}
|
|
349
354
|
|
|
@@ -375,10 +380,24 @@ class Scanner {
|
|
|
375
380
|
clearTypeStores();
|
|
376
381
|
}
|
|
377
382
|
|
|
383
|
+
/**
|
|
384
|
+
* 已扫描条目总数
|
|
385
|
+
* @description 按本实例目录下各资源类型的 Store 条目计数累加
|
|
386
|
+
* @returns {number} 条目总数
|
|
387
|
+
*/
|
|
378
388
|
get size() {
|
|
379
|
-
|
|
389
|
+
if (!this.#scanned) return 0;
|
|
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;
|
|
380
395
|
}
|
|
381
396
|
|
|
397
|
+
/**
|
|
398
|
+
* 获取本实例绑定的扫描根目录
|
|
399
|
+
* @returns {string} 扫描根目录绝对路径
|
|
400
|
+
*/
|
|
382
401
|
get baseDir() {
|
|
383
402
|
return this.#base_dir;
|
|
384
403
|
}
|
package/lib/type_store.js
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
let { storeAdmin } = require('mm_store');
|
|
4
|
+
let { constAdmin, loadSync } = require('mm_consts');
|
|
5
|
+
let { varAdmin } = require('mm_vars');
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
constAdmin('scanner');
|
|
8
|
+
loadSync('scanner', require('path').join(__dirname, '..', 'data'));
|
|
7
9
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
+
/**
|
|
11
|
+
* 获取本模块使用过的类型集合
|
|
12
|
+
* @description 集合保存在 mm_vars 实例池中,随作用域实例统一管理,避免模块级散落状态
|
|
13
|
+
* @returns {Set<string>} 类型集合
|
|
14
|
+
*/
|
|
15
|
+
function typeSet() {
|
|
16
|
+
let vars = varAdmin('mm_scanner');
|
|
17
|
+
if (!vars.has('TYPES')) {
|
|
18
|
+
vars.set('TYPES', new Set());
|
|
19
|
+
}
|
|
20
|
+
return vars.get('TYPES');
|
|
21
|
+
}
|
|
10
22
|
|
|
11
23
|
/**
|
|
12
24
|
* 获取资源类型对应的存储实例
|
|
@@ -16,8 +28,8 @@ const types = new Set();
|
|
|
16
28
|
* @returns {object} mm_store 实例
|
|
17
29
|
*/
|
|
18
30
|
function typeStore(type) {
|
|
19
|
-
|
|
20
|
-
return storeAdmin(type, STORE_CONFIG);
|
|
31
|
+
typeSet().add(type);
|
|
32
|
+
return storeAdmin(type, constAdmin('scanner').get('STORE_CONFIG'));
|
|
21
33
|
}
|
|
22
34
|
|
|
23
35
|
/**
|
|
@@ -25,9 +37,9 @@ function typeStore(type) {
|
|
|
25
37
|
* @returns {void}
|
|
26
38
|
*/
|
|
27
39
|
function clearTypeStores() {
|
|
28
|
-
for (
|
|
40
|
+
for (let type of typeSet()) {
|
|
29
41
|
typeStore(type).clear();
|
|
30
42
|
}
|
|
31
43
|
}
|
|
32
44
|
|
|
33
|
-
module.exports = {
|
|
45
|
+
module.exports = { typeStore, clearTypeStores };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_scanner",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.2",
|
|
4
4
|
"description": "目录扫描器,按资源类型表递归扫描目录并在扫描时完成分类,一类一个 Store 写入 mm_store,支持按类型取文件切片,默认忽略 .git/node_modules 等常见目录,并支持接收监听变更做增量更新。",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,15 +15,17 @@
|
|
|
15
15
|
"文件检索",
|
|
16
16
|
"scanner"
|
|
17
17
|
],
|
|
18
|
-
"layer": "
|
|
19
|
-
"group": "
|
|
18
|
+
"layer": "util",
|
|
19
|
+
"group": "io",
|
|
20
20
|
"author": "邱文武",
|
|
21
21
|
"license": "ISC",
|
|
22
22
|
"main": "index.js",
|
|
23
23
|
"files": [
|
|
24
24
|
"index.js",
|
|
25
25
|
"lib",
|
|
26
|
-
"
|
|
26
|
+
"tool",
|
|
27
|
+
"example",
|
|
28
|
+
"data"
|
|
27
29
|
],
|
|
28
30
|
"directories": {
|
|
29
31
|
"example": "example",
|
|
@@ -35,10 +37,15 @@
|
|
|
35
37
|
"prepublishOnly": "npm test"
|
|
36
38
|
},
|
|
37
39
|
"dependencies": {
|
|
38
|
-
"mm_admin": "^1.
|
|
39
|
-
"
|
|
40
|
+
"mm_admin": "^1.2.2",
|
|
41
|
+
"mm_consts": "^1.1.3",
|
|
42
|
+
"mm_store": "^1.1.2",
|
|
43
|
+
"mm_vars": "^1.2.3"
|
|
40
44
|
},
|
|
41
45
|
"devDependencies": {
|
|
42
46
|
"jest": "^30.5.2"
|
|
47
|
+
},
|
|
48
|
+
"overrides": {
|
|
49
|
+
"mm_persist": "0.0.0-stage"
|
|
43
50
|
}
|
|
44
51
|
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* scan —— 目录扫描工具
|
|
4
|
+
* 调用 mm_scanner 按资源类型表扫描目录,或查看 / 重新扫描已扫描目录。
|
|
5
|
+
*/
|
|
6
|
+
let { scannerAdmin } = require('../../index.js');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 扫描、查看或重新扫描目录
|
|
10
|
+
* @param {object} params 参数:action/dir/type/resources/ignore
|
|
11
|
+
* @param {object} ctx 执行上下文
|
|
12
|
+
* @returns {Promise<object>} 执行结果
|
|
13
|
+
*/
|
|
14
|
+
async function main(params, ctx, options) {
|
|
15
|
+
void options;
|
|
16
|
+
let p = params || {};
|
|
17
|
+
let action = p.action || 'scan';
|
|
18
|
+
/**
|
|
19
|
+
* 写入执行结果到上下文并返回。
|
|
20
|
+
* @param {object} data - 结果数据
|
|
21
|
+
* @returns {object} 结果数据本身
|
|
22
|
+
*/
|
|
23
|
+
const done = (data) => {
|
|
24
|
+
if (ctx) ctx.output = data;
|
|
25
|
+
return data;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
if (action === 'scan') {
|
|
30
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
31
|
+
let config = { resources: p.resources, ignore: p.ignore };
|
|
32
|
+
let scanner = scannerAdmin(p.dir, config);
|
|
33
|
+
scanner.scan(p.dir, config);
|
|
34
|
+
return done({
|
|
35
|
+
code: 0,
|
|
36
|
+
msg: '扫描完成',
|
|
37
|
+
action,
|
|
38
|
+
dir: scanner.baseDir,
|
|
39
|
+
count: scanner.size,
|
|
40
|
+
files: scanner.getAll()
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (action === 'rescan') {
|
|
45
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
46
|
+
let scanner = scannerAdmin(p.dir);
|
|
47
|
+
scanner.rescan();
|
|
48
|
+
return done({
|
|
49
|
+
code: 0,
|
|
50
|
+
msg: '重新扫描完成',
|
|
51
|
+
action,
|
|
52
|
+
dir: scanner.baseDir,
|
|
53
|
+
count: scanner.size
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (action === 'list') {
|
|
58
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
59
|
+
let scanner = scannerAdmin(p.dir);
|
|
60
|
+
let files = scanner.getAll();
|
|
61
|
+
if (p.type) {
|
|
62
|
+
files = files.filter((item) => item.type === p.type);
|
|
63
|
+
}
|
|
64
|
+
return done({
|
|
65
|
+
code: 0,
|
|
66
|
+
msg: '获取成功',
|
|
67
|
+
action,
|
|
68
|
+
dir: scanner.baseDir,
|
|
69
|
+
count: files.length,
|
|
70
|
+
files
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (action === 'apply') {
|
|
75
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
76
|
+
if (!Array.isArray(p.changes)) throw new TypeError('changes 必须是数组');
|
|
77
|
+
let scanner = scannerAdmin(p.dir);
|
|
78
|
+
let changes = scanner.apply(p.changes);
|
|
79
|
+
return done({
|
|
80
|
+
code: 0,
|
|
81
|
+
msg: '增量更新完成',
|
|
82
|
+
action,
|
|
83
|
+
dir: scanner.baseDir,
|
|
84
|
+
count: changes.length,
|
|
85
|
+
changes
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (action === 'config') {
|
|
90
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
91
|
+
let scanner = scannerAdmin(p.dir);
|
|
92
|
+
return done({ code: 0, msg: '获取成功', action, dir: scanner.baseDir, config: scanner.config });
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
96
|
+
} catch (error) {
|
|
97
|
+
return done({
|
|
98
|
+
code: 10000,
|
|
99
|
+
msg: '执行失败:' + error.message,
|
|
100
|
+
error: { name: error.name, message: error.message }
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
module.exports = { main };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "scan",
|
|
3
|
+
"title": "目录扫描",
|
|
4
|
+
"description": "调用 mm_scanner 按资源类型表递归扫描目录并分类,或查看 / 重新扫描 / 增量更新已扫描目录、读取扫描配置。适用场景:加载配置与脚本前获取文件清单、按类型筛选资源、热更新后强制重扫或按变更增量更新、确认资源类型与忽略目录。工作流提示:scan 需传 dir,list/rescan/apply/config 需传已扫描过的 dir;type 用于 list 时按资源类型过滤,apply 需传 changes。",
|
|
5
|
+
"category": "资源管理",
|
|
6
|
+
"group": "resources",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"properties": {
|
|
12
|
+
"action": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "操作类型: scan 扫描 / list 查看 / rescan 重新扫描 / apply 增量更新 / config 查看配置",
|
|
15
|
+
"enum": ["scan", "list", "rescan", "apply", "config"],
|
|
16
|
+
"default": "scan"
|
|
17
|
+
},
|
|
18
|
+
"dir": {
|
|
19
|
+
"type": "string",
|
|
20
|
+
"description": "扫描目录(scan/list/rescan/apply/config 必填)"
|
|
21
|
+
},
|
|
22
|
+
"changes": {
|
|
23
|
+
"type": "array",
|
|
24
|
+
"description": "监听变更列表(apply 必填),元素为 { type, path },type 为 add/set/del/reload",
|
|
25
|
+
"items": { "type": "object" }
|
|
26
|
+
},
|
|
27
|
+
"type": {
|
|
28
|
+
"type": "string",
|
|
29
|
+
"description": "资源类型,仅 list 时用于过滤"
|
|
30
|
+
},
|
|
31
|
+
"resources": {
|
|
32
|
+
"type": "object",
|
|
33
|
+
"description": "资源类型表,键为类型名,值为 { ext },仅 scan 首次创建实例时生效"
|
|
34
|
+
},
|
|
35
|
+
"ignore": {
|
|
36
|
+
"type": "array",
|
|
37
|
+
"items": { "type": "string" },
|
|
38
|
+
"description": "忽略的目录名列表,与默认忽略目录取并集"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"required": ["action", "dir"]
|
|
42
|
+
},
|
|
43
|
+
"returns": {
|
|
44
|
+
"type": "object",
|
|
45
|
+
"properties": {
|
|
46
|
+
"code": { "type": "number", "description": "0 表示成功" },
|
|
47
|
+
"msg": { "type": "string", "description": "结果描述" },
|
|
48
|
+
"dir": { "type": "string", "description": "扫描根目录绝对路径" },
|
|
49
|
+
"count": { "type": "number", "description": "文件或变更数量" },
|
|
50
|
+
"files": { "type": "array", "description": "扫描条目数组" },
|
|
51
|
+
"changes": { "type": "array", "description": "标准化后的增量变更数组(apply)" },
|
|
52
|
+
"config": { "type": "object", "description": "扫描器配置(config)" },
|
|
53
|
+
"error": { "type": "object", "description": "异常信息" }
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"handler": "manager"
|
|
57
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* scanner_admin —— 扫描器实例管理工具
|
|
4
|
+
* 管理 mm_scanner 的实例池与类型 Store 缓存,不执行具体扫描。
|
|
5
|
+
*/
|
|
6
|
+
const path = require('path');
|
|
7
|
+
let { admin, Scanner } = require('../../index.js');
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 获取实例键
|
|
11
|
+
* @param {object} p 参数
|
|
12
|
+
* @returns {string} 绝对路径
|
|
13
|
+
*/
|
|
14
|
+
function keyOf(p) {
|
|
15
|
+
if (!p.dir) throw new TypeError('dir 不能为空');
|
|
16
|
+
return path.resolve(p.dir);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 管理扫描器实例池
|
|
21
|
+
* @param {object} params 参数:action/dir
|
|
22
|
+
* @param {object} ctx 执行上下文
|
|
23
|
+
* @returns {Promise<object>} 执行结果
|
|
24
|
+
*/
|
|
25
|
+
async function main(params, ctx, options) {
|
|
26
|
+
void options;
|
|
27
|
+
let p = params || {};
|
|
28
|
+
let action = p.action || 'list';
|
|
29
|
+
/**
|
|
30
|
+
* 写入执行结果到上下文并返回。
|
|
31
|
+
* @param {object} data - 结果数据
|
|
32
|
+
* @returns {object} 结果数据本身
|
|
33
|
+
*/
|
|
34
|
+
const done = (data) => {
|
|
35
|
+
if (ctx) ctx.output = data;
|
|
36
|
+
return data;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
if (action === 'list') {
|
|
41
|
+
let dirs = [...admin.keys()];
|
|
42
|
+
return done({ code: 0, msg: '获取成功', action, count: dirs.length, dirs });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (action === 'inspect') {
|
|
46
|
+
let key = keyOf(p);
|
|
47
|
+
let exists = admin.has(key);
|
|
48
|
+
if (!exists) {
|
|
49
|
+
return done({ code: 0, msg: '实例不存在', action, dir: key, exists: false });
|
|
50
|
+
}
|
|
51
|
+
let scanner = admin.get(key);
|
|
52
|
+
return done({
|
|
53
|
+
code: 0,
|
|
54
|
+
msg: '获取成功',
|
|
55
|
+
action,
|
|
56
|
+
dir: key,
|
|
57
|
+
exists: true,
|
|
58
|
+
count: scanner.size,
|
|
59
|
+
config: scanner.config
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (action === 'del') {
|
|
64
|
+
let key = keyOf(p);
|
|
65
|
+
return done({ code: 0, msg: '删除成功', action, dir: key, ok: admin.del(key) });
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (action === 'clear') {
|
|
69
|
+
admin.clear();
|
|
70
|
+
return done({ code: 0, msg: '实例池已清空', action });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (action === 'clear_cache') {
|
|
74
|
+
Scanner.clearCache();
|
|
75
|
+
return done({ code: 0, msg: '类型 Store 已清空', action });
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
79
|
+
} catch (error) {
|
|
80
|
+
return done({
|
|
81
|
+
code: 10000,
|
|
82
|
+
msg: '执行失败:' + error.message,
|
|
83
|
+
error: { name: error.name, message: error.message }
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
module.exports = { main };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "scanner_admin",
|
|
3
|
+
"title": "扫描器实例管理",
|
|
4
|
+
"description": "管理 mm_scanner 的实例池与类型 Store 缓存:列出已扫描目录、查看实例状态、删除实例、清空实例池或清空扫描缓存。适用场景:排查扫描实例未创建、清理热更新残留、释放目录扫描状态。",
|
|
5
|
+
"category": "资源管理",
|
|
6
|
+
"group": "resources",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": {
|
|
10
|
+
"type": "object",
|
|
11
|
+
"properties": {
|
|
12
|
+
"action": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"description": "操作类型: list 实例列表 / inspect 查看实例 / del 删除实例 / clear 清空实例池 / clear_cache 清空扫描缓存",
|
|
15
|
+
"enum": ["list", "inspect", "del", "clear", "clear_cache"],
|
|
16
|
+
"default": "list"
|
|
17
|
+
},
|
|
18
|
+
"dir": {
|
|
19
|
+
"type": "string",
|
|
20
|
+
"description": "扫描目录(inspect/del 必填)"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"required": ["action"]
|
|
24
|
+
},
|
|
25
|
+
"returns": {
|
|
26
|
+
"type": "object",
|
|
27
|
+
"properties": {
|
|
28
|
+
"code": { "type": "number", "description": "0 表示成功" },
|
|
29
|
+
"msg": { "type": "string", "description": "结果描述" },
|
|
30
|
+
"count": { "type": "number", "description": "实例或文件数量" },
|
|
31
|
+
"dirs": { "type": "array", "items": { "type": "string" }, "description": "实例目录列表" },
|
|
32
|
+
"exists": { "type": "boolean", "description": "实例是否存在" },
|
|
33
|
+
"config": { "type": "object", "description": "扫描器配置" },
|
|
34
|
+
"ok": { "type": "boolean", "description": "操作是否成功" },
|
|
35
|
+
"error": { "type": "object", "description": "异常信息" }
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"handler": "manager"
|
|
39
|
+
}
|