mm_machine 2.9.7 → 2.9.9

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/registry.js CHANGED
@@ -1,634 +1,412 @@
1
1
  /**
2
- * Registry 注册表类
3
- * 持久化存储所有模块的元数据,启动时一次 IO 读取即可恢复全部模块信息
4
- * @class Registry
5
- * @augments Drive
2
+ * 注册类 - 注册表模式
3
+ * 管理模块类的注册、模块配置的持久化、模块实例的懒加载
6
4
  */
7
- var fs = require('fs');
8
- var path = require('path');
9
- var { Drive } = require('./drive');
10
-
11
- /**
12
- * 新增条目自动补全的默认值
13
- */
14
- var defaultEntry = {
15
- title: '',
16
- description: '',
17
- type: 'plugin',
18
- version: '1.0.0',
19
- sort: 100,
20
- main: '',
21
- enabled: true,
22
- auto_start: false,
23
- lazy: 'auto',
24
- state: 'stopped',
25
- // eslint-disable-next-line mm_eslint/variable-name
26
- registered_at: '',
27
- updated_at: ''
28
- };
29
-
30
- class Registry extends Drive {
5
+ class Registry {
31
6
  /**
32
7
  * 构造函数
33
- * @param {object} manager Manager 实例(必需),用于注册模块
34
- * @param {object} [cfg] 配置参数
35
- * @param {string} [cfg.file] 注册表文件绝对路径,默认从 Manager 配置拼接
8
+ * @param {object} config 配置项
36
9
  */
37
- constructor(manager, cfg) {
38
- var config = cfg || {};
39
- super(config);
40
- this._manager = manager;
41
- this._data = { version: '1.0', updated_at: '', modules: [] };
42
- this._file = config.file || Registry._resolveFile(manager);
43
- this._save_timer = null;
44
- this._dirty = false;
10
+ constructor(config) {
11
+ this.config = {
12
+ name: 'default',
13
+ dir: '/data/registry'.fullname(__dirname),
14
+ ...config
15
+ };
16
+ this.class_dict = this.getDefaultClassDict();
17
+ this.module_list = [];
18
+
19
+ // 注册表缓存:类名 -> 类构造函数
20
+ this._class_cache = {};
21
+ // 注册表缓存:模块名 -> { config, instance }
22
+ this._module_cache = {};
23
+ // 类名索引:类名 -> [模块名]
24
+ this._class_index = {};
25
+ this._preset();
45
26
  }
46
27
  }
47
28
 
48
29
  /**
49
- * 解析注册表文件路径
50
- * @param {object} manager Manager 实例
51
- * @returns {string} 注册表文件绝对路径
52
- * @private
53
- * @static
30
+ * 预设默认模块类
54
31
  */
55
- Registry._resolveFile = function (manager) {
56
- // 有显式配置时直接使用
57
- if (manager.config.registry_file) {
58
- return path.resolve(manager.dir || process.cwd(), manager.config.registry_file);
59
- }
60
- // 默认:data/register/{构造函数名}_{config.name}.json
61
- var base_dir = path.resolve(manager.dir || process.cwd(), 'data/register');
62
- var class_name = manager.constructor.name || 'Manager';
63
- var mod_name = manager.config.name || 'default';
64
- return path.join(base_dir, class_name + '_' + mod_name + '.json');
32
+ Registry.prototype._preset = function () {
33
+ this.config.dir.addDir();
65
34
  };
66
35
 
67
- // ==================== 数据加载/保存 ====================
68
-
69
36
  /**
70
- * 从文件加载注册表数据
71
- * 使用 require() 加载(支持缓存),文件不存在返回空数据结构
72
- * @returns {object} 注册表数据 { version, modules[] }
37
+ * 获取模块类字典
38
+ * @returns {object} 模块类字典
73
39
  */
74
- Registry.prototype.load = function () {
75
- try {
76
- if (fs.existsSync(this._file)) {
77
- var data = require(this._file);
78
- if (data && data.modules) {
79
- this._data = data;
80
- return this._data;
81
- }
82
- }
83
- } catch {
84
- // 文件不存在或解析失败,使用空数据
85
- }
86
- this._data = { version: '1.0', updated_at: '', modules: [] };
87
- return this._data;
40
+ Registry.prototype.getClassDict = function () {
41
+ return this.class_dict;
88
42
  };
89
43
 
90
44
  /**
91
- * 保存注册表数据到文件
92
- * 防抖保存(500ms 内多次调用只写一次),写完后清除 require.cache
45
+ * 获取默认模块类
46
+ * @returns {object} 默认模块类
93
47
  */
94
- Registry.prototype.save = function () {
95
- this._dirty = true;
96
-
97
- if (this._save_timer) {
98
- return;
99
- }
100
-
101
- this._save_timer = setTimeout(() => {
102
- this._save_timer = null;
103
- this._flushSave();
104
- }, 500);
48
+ Registry.prototype.getDefaultClassDict = function () {
49
+ return {
50
+ Mod: './mod.js'.fullname(__dirname),
51
+ Drive: './drive.js'.fullname(__dirname),
52
+ Manager: './manager.js'.fullname(__dirname)
53
+ };
105
54
  };
106
55
 
107
56
  /**
108
- * 立即写入文件
109
- * @private
57
+ * 设置模块类
58
+ * @param {string} class_name 模块类名
59
+ * @param {string} file 模块类文件路径
110
60
  */
111
- Registry.prototype._flushSave = function () {
112
- if (!this._dirty) {
113
- return;
114
- }
115
-
116
- this._data.updated_at = new Date().toISOString();
117
-
118
- try {
119
- var dir = path.dirname(this._file);
120
- if (!fs.existsSync(dir)) {
121
- fs.mkdirSync(dir, { recursive: true });
122
- }
123
- fs.writeFileSync(this._file, JSON.stringify(this._data, null, 2), 'utf8');
124
-
125
- // 清除 require.cache 确保下次 load 读取最新数据
126
- try {
127
- delete require.cache[require.resolve(this._file)];
128
- } catch {
129
- // 忽略
130
- }
131
-
132
- this._dirty = false;
133
- } catch {
134
- // 保存失败,记录错误
135
- }
61
+ Registry.prototype.setClass = function (class_name, file) {
62
+ this.class_dict[class_name] = file;
136
63
  };
137
64
 
138
- // ==================== 查询方法 ====================
139
-
140
65
  /**
141
- * 获取所有注册表项的浅拷贝数组
142
- * @returns {Array<object>} 注册表项数组
66
+ * 删除模块类
67
+ * @param {string} class_name 模块类名
143
68
  */
144
- Registry.prototype.getAllEntries = function () {
145
- return this._data.modules.slice();
69
+ Registry.prototype.delClass = function (class_name) {
70
+ delete this.class_dict[class_name];
146
71
  };
147
72
 
148
- /**
149
- * 按名称获取单个注册表项
150
- * @param {string} name 模块名称
151
- * @returns {object|null} 注册表项,不存在返回 null
152
- */
153
- Registry.prototype.getEntry = function (name) {
154
- var modules = this._data.modules;
155
- for (var i = 0; i < modules.length; i++) {
156
- if (modules[i].name === name) {
157
- return modules[i];
158
- }
159
- }
160
- return null;
161
- };
73
+ // ==== 注册表缓存方法 ====
162
74
 
163
75
  /**
164
- * 获取注册表项数量
165
- * @returns {number} 数量
76
+ * 注册模块类到缓存
77
+ * @param {string} class_name 模块类名
78
+ * @param {Function} class_ref 模块类构造函数
166
79
  */
167
- Registry.prototype.getEntryCount = function () {
168
- return this._data.modules.length;
80
+ Registry.prototype.registerClass = function (class_name, class_ref) {
81
+ if (!class_name || !class_ref) {
82
+ throw new TypeError('模块类名和类引用不能为空');
83
+ }
84
+ this._class_cache[class_name] = class_ref;
169
85
  };
170
86
 
171
87
  /**
172
- * 检查注册表项是否存在
173
- * @param {string} name 模块名称
174
- * @returns {boolean} 是否存在
88
+ * 获取模块类构造函数
89
+ * @param {string} class_name 模块类名
90
+ * @returns {Function} 模块类构造函数
175
91
  */
176
- Registry.prototype.hasEntry = function (name) {
177
- return this.getEntry(name) !== null;
92
+ Registry.prototype.getClass = function (class_name) {
93
+ return this._class_cache[class_name];
178
94
  };
179
95
 
180
- // ==================== 增删改方法 ====================
181
-
182
96
  /**
183
- * 添加注册表项
184
- * 自动补全缺失字段,然后保存
185
- * @param {object} data 注册表项数据
97
+ * 注册模块配置到缓存(不实例化)
98
+ * @param {object} config 模块配置项
99
+ * @returns {object} 注册的配置项
100
+ * @throws {Error} 模块配置项错误
186
101
  */
187
- Registry.prototype.addEntry = function (data) {
188
- if (!data || !data.name) {
189
- throw new TypeError('注册表项必须包含 name 字段');
102
+ Registry.prototype.registerModule = function (config) {
103
+ if (!config || !config.name) {
104
+ throw new TypeError('模块配置和模块名称不能为空');
190
105
  }
191
-
192
- if (this.hasEntry(data.name)) {
193
- return;
194
- }
195
-
196
- var now = new Date().toISOString();
197
- var entry = {
198
- ...defaultEntry,
199
- ...data,
200
- // eslint-disable-next-line mm_eslint/variable-name
201
- registered_at: data.registered_at || now,
202
- updated_at: now
106
+ var class_name = config.class_name || 'Drive';
107
+ this._module_cache[config.name] = {
108
+ config: config,
109
+ instance: null
203
110
  };
204
-
205
- this._data.modules.push(entry);
206
- this.save();
207
- };
208
-
209
- /**
210
- * 更新注册表项
211
- * 部分合并,只覆盖 data 中存在的字段
212
- * @param {string} name 模块名称
213
- * @param {object} data 要更新的字段
214
- */
215
- Registry.prototype.updateEntry = function (name, data) {
216
- if (!data) {
217
- return;
218
- }
219
-
220
- var modules = this._data.modules;
221
- for (var i = 0; i < modules.length; i++) {
222
- if (modules[i].name === name) {
223
- for (var k in data) {
224
- modules[i][k] = data[k];
225
- }
226
- modules[i].updated_at = new Date().toISOString();
227
- this.save();
228
- return;
229
- }
111
+ // 按类名建立索引
112
+ if (!this._class_index[class_name]) {
113
+ this._class_index[class_name] = [];
230
114
  }
115
+ this._class_index[class_name].push(config.name);
116
+ return config;
231
117
  };
232
118
 
233
119
  /**
234
- * 移除注册表项并保存
235
- * @param {string} name 模块名称
120
+ * 获取模块实例(懒加载)
121
+ * 首次调用时根据 config 和已注册的类构造函数实例化
122
+ * @param {string} name 模块名
123
+ * @returns {object|null} 模块实例
236
124
  */
237
- Registry.prototype.removeEntry = function (name) {
238
- var modules = this._data.modules;
239
- for (var i = 0; i < modules.length; i++) {
240
- if (modules[i].name === name) {
241
- modules.splice(i, 1);
242
- this.save();
243
- return;
125
+ Registry.prototype.getModule = function (name) {
126
+ var entry = this._module_cache[name];
127
+ if (!entry) return null;
128
+
129
+ if (!entry.instance) {
130
+ var class_name = entry.config.class_name || 'Drive';
131
+ const CS = this._class_cache[class_name];
132
+ if (!CS) {
133
+ throw new Error('模块类 ' + class_name + ' 未注册');
244
134
  }
135
+ entry.instance = new CS(entry.config);
245
136
  }
137
+ return entry.instance;
246
138
  };
247
139
 
248
- // ==================== 自启动管理 ====================
249
-
250
140
  /**
251
- * 设置模块是否自启动
252
- * @param {string} name 模块名称
253
- * @param {boolean} flag 是否自启动
141
+ * 按类名获取模块实例列表
142
+ * @param {string} class_name 模块类名
143
+ * @returns {Array} 模块实例数组
254
144
  */
255
- Registry.prototype.setAutoStart = function (name, flag) {
256
- this.updateEntry(name, { auto_start: !!flag });
145
+ Registry.prototype.getModules = function (class_name) {
146
+ var names = this._class_index[class_name];
147
+ if (!names || names.length === 0) return [];
148
+ return names.map(function (name) {
149
+ return this.getModule(name);
150
+ }, this);
257
151
  };
258
152
 
259
153
  /**
260
- * 获取所有自启动模块列表
261
- * @returns {Array<object>} 自启动模块列表
154
+ * 注销模块
155
+ * @param {string} name 模块名
262
156
  */
263
- Registry.prototype.getAutoStartList = function () {
264
- var modules = this._data.modules;
265
- var list = [];
266
- for (var i = 0; i < modules.length; i++) {
267
- if (modules[i].auto_start) {
268
- list.push(modules[i]);
157
+ Registry.prototype.unregisterModule = function (name) {
158
+ var entry = this._module_cache[name];
159
+ if (!entry) return;
160
+
161
+ var class_name = entry.config.class_name || 'Drive';
162
+ var names = this._class_index[class_name];
163
+ if (names) {
164
+ var idx = names.indexOf(name);
165
+ if (idx !== -1) {
166
+ names.splice(idx, 1);
269
167
  }
270
168
  }
271
- return list;
169
+ delete this._module_cache[name];
272
170
  };
273
171
 
274
- // ==================== 核心操作 ====================
275
-
276
172
  /**
277
- * 同步注册表数据到 Manager
278
- * 遍历 _data.modules,从注册表数据直接创建模块实例(0 IO)
279
- * 已存在的模块跳过(不重复创建)
173
+ * 加载注册数据
174
+ * 从 JSON 文件读取持久化数据,注册所有类和模块到缓存
280
175
  */
281
- Registry.prototype.syncToManager = function () {
282
- var modules = this._data.modules;
283
- for (var i = 0; i < modules.length; i++) {
284
- var entry = modules[i];
285
-
286
- // 跳过 config_file 不存在的模块
287
- if (entry.config_file && !fs.existsSync(entry.config_file)) {
288
- continue;
289
- }
176
+ Registry.prototype.load = function () {
177
+ this.read();
178
+
179
+ // 注册模块类(幂等-safe,Node require 返回同一引用)
180
+ for (var key in this.class_dict) {
181
+ var file = this.class_dict[key];
182
+ var exports = require(file);
183
+ var class_ref = exports[key] || exports;
184
+ this.registerClass(key, class_ref);
185
+ }
290
186
 
291
- if (this._manager && typeof this._manager.registerModEx === 'function') {
292
- this._manager.registerModEx(entry);
187
+ // 注册模块配置(仅新增不覆盖,保留已有实例)
188
+ for (var i = 0; i < this.module_list.length; i++) {
189
+ var mod_cfg = this.module_list[i];
190
+ if (!this._module_cache[mod_cfg.name]) {
191
+ this.registerModule(mod_cfg);
293
192
  }
294
193
  }
295
194
  };
296
195
 
196
+ // ==== 配置项 CRUD ====
197
+
297
198
  /**
298
- * 启动自启动模块
299
- * 遍历 getAutoStartList(),调用 mod.load() 加载,
300
- * 根据 lazy 策略决定是否继续 init/start
301
- * @returns {Promise<void>}
199
+ * 创建新模块配置项
200
+ * @returns {object} 模块配置项
302
201
  */
303
- Registry.prototype.startAutoStart = async function () {
304
- var list = this.getAutoStartList();
305
-
306
- for (var i = 0; i < list.length; i++) {
307
- var entry = list[i];
308
- var mod = this._manager ? this._manager.getMod(entry.name) : null;
309
- if (!mod) {
310
- continue;
311
- }
312
-
313
- try {
314
- // 加载模块
315
- await mod.load();
316
-
317
- // 根据 lazy 策略决定是否继续
318
- if (this._shouldContinue(entry)) {
319
- await mod.init();
320
- await mod.start();
321
- }
322
- } catch {
323
- // 单个模块启动失败不影响其他模块
324
- }
325
- }
202
+ Registry.prototype.newModel = function () {
203
+ return {
204
+ parent: '',
205
+ name: '',
206
+ title: '',
207
+ description: '',
208
+ version: '1.0.0',
209
+ dependencies: {},
210
+ sort: 100,
211
+ enabled: true,
212
+ auto_start: 'auto',
213
+ class_name: 'Drive',
214
+ config_file: '',
215
+ main: '',
216
+ values: {}
217
+ };
326
218
  };
327
219
 
328
220
  /**
329
- * 判断是否继续推进生命周期(init + start)
330
- * @param {object} entry 注册表项
331
- * @returns {boolean} 是否继续
332
- * @private
221
+ * 设置模块
222
+ * @param {object} config 模块配置项
223
+ * @returns {object} 模块配置项
333
224
  */
334
- Registry.prototype._shouldContinue = function (entry) {
335
- var lazy = entry.lazy !== undefined ? entry.lazy : 'auto';
336
-
337
- if (lazy === false) {
338
- return true;
225
+ Registry.prototype.set = function (config) {
226
+ let cg = this.checkConfig(config);
227
+ let has = false;
228
+ for (let i = 0; i < this.module_list.length; i++) {
229
+ let o = this.module_list[i];
230
+ if (o.name === cg.name) {
231
+ if (o.class_name !== cg.class_name) {
232
+ throw new Error('模块类名与配置项类名不一致');
233
+ }
234
+ this.module_list[i] = cg;
235
+ has = true;
236
+ break;
237
+ }
339
238
  }
340
- if (lazy === 'auto') {
341
- var mgr_lazy = this._manager ? this._manager.config.lazy_load : false;
342
- return mgr_lazy === false || mgr_lazy === 'auto';
239
+ if (!has) {
240
+ this.module_list.push(cg);
343
241
  }
344
- return false;
242
+ // 清除外部缓存索引
243
+ this._config_map = null;
244
+ return cg;
345
245
  };
346
246
 
347
247
  /**
348
- * 启动引导
349
- * load() → syncToManager() → 注册表为空时扫描 → startAutoStart() → save()
350
- * @returns {Promise<void>}
248
+ * 获取模块
249
+ * @param {string} name 模块名称
250
+ * @returns {object} 模块配置项
351
251
  */
352
- Registry.prototype.boot = async function () {
353
- // 1. 加载注册表文件
354
- this.load();
355
-
356
- // 2. 注册模块到 Manager
357
- this.syncToManager();
358
-
359
- // 3. 注册表为空时扫描文件系统(首次启动)
360
- // 实际扫描由 Manager.boot() 在调用 registry.boot() 后处理
361
-
362
- // 4. 启动自启动模块
363
- await this.startAutoStart();
364
-
365
- // 5. 保存注册表
366
- this.save();
252
+ Registry.prototype.get = function (name) {
253
+ // 先查缓存索引,无命中再回退线性扫描
254
+ var entry = this._module_cache[name];
255
+ if (entry) return entry.config;
256
+ for (let i = 0; i < this.module_list.length; i++) {
257
+ if (this.module_list[i].name === name) {
258
+ return this.module_list[i];
259
+ }
260
+ }
261
+ return null;
367
262
  };
368
263
 
369
264
  /**
370
- * 一键注册多个模块
371
- * @param {Array<string|object>} files 文件路径数组或 {file, config} 对象数组
372
- * @returns {number} 成功注册的模块数量
265
+ * 删除模块
266
+ * @param {string} name 模块名称
267
+ * @returns {object} 模块配置项
373
268
  */
374
- Registry.prototype.registerAll = function (files) {
375
- if (!files || !Array.isArray(files)) {
376
- return 0;
377
- }
378
-
379
- var count = 0;
380
-
381
- for (var i = 0; i < files.length; i++) {
382
- if (this._registerOne(files[i])) {
383
- count++;
269
+ Registry.prototype.del = function (name) {
270
+ let obj = null;
271
+ for (let i = 0; i < this.module_list.length; i++) {
272
+ if (this.module_list[i].name === name) {
273
+ obj = this.module_list[i];
274
+ this.module_list.splice(i, 1);
275
+ break;
384
276
  }
385
277
  }
386
-
387
- return count;
278
+ // 清除外部缓存索引
279
+ this._config_map = null;
280
+ return obj;
388
281
  };
389
282
 
390
283
  /**
391
- * 注册单个文件条目
392
- * @param {string|object} item 文件路径或 {file, config} 对象
393
- * @returns {boolean} 是否注册成功
394
- * @private
284
+ * 获取模块列表
285
+ * @returns {Array} 模块配置项数组
395
286
  */
396
- // eslint-disable-next-line complexity, max-lines-per-function
397
- Registry.prototype._registerOne = function (item) {
398
- var file_path = '';
399
- var cfg = null;
400
-
401
- if (typeof item === 'string') {
402
- file_path = item;
403
- } else if (item && item.file) {
404
- file_path = item.file;
405
- cfg = item.config || null;
406
- } else {
407
- return false;
408
- }
409
-
410
- try {
411
- // 未提供配置时从文件读取
412
- if (!cfg) {
413
- var content = fs.readFileSync(file_path, 'utf8');
414
- cfg = JSON.parse(content);
415
- }
416
-
417
- if (!cfg || !cfg.name) {
418
- return false;
419
- }
420
-
421
- var entry = {
422
- name: cfg.name,
423
- title: cfg.title || '',
424
- description: cfg.description || '',
425
- type: cfg.type || 'plugin',
426
- version: cfg.version || '1.0.0',
427
- sort: cfg.sort || 100,
428
- config_file: file_path,
429
- main: cfg.main || '',
430
- enabled: cfg.enabled !== false,
431
- auto_start: !!cfg.auto_start,
432
- lazy: cfg.lazy !== undefined ? cfg.lazy : 'auto'
433
- };
434
-
435
- this.addEntry(entry);
436
-
437
- if (this._manager && typeof this._manager.registerModEx === 'function') {
438
- this._manager.registerModEx(entry);
439
- }
440
-
441
- return true;
442
- } catch {
443
- // 单个模块注册失败不影响其他模块
444
- return false;
445
- }
287
+ Registry.prototype.getList = function () {
288
+ return this.module_list;
446
289
  };
447
290
 
448
291
  /**
449
- * 获取全量模块目录
450
- * 三步交叉比对:扫描文件系统 → 交叉比对注册表 → 补充运行时状态
451
- * @param {object} scanner DirScanner 实例
452
- * @param {string[]} dirs 要扫描的目录列表
453
- * @param {boolean} [force] 是否强制重新扫描文件系统
454
- * @returns {Array<object>} 全量目录列表
292
+ * 检查配置项
293
+ * @param {object} config 模块配置项
294
+ * @returns {object} 模块配置项
295
+ * @throws {Error} 模块配置项错误
455
296
  */
456
- Registry.prototype.getCatalog = function (scanner, dirs, force) {
457
- if (!scanner || typeof scanner.scanAndParse !== 'function') {
458
- throw new TypeError('scanner 必须包含 scanAndParse 方法');
297
+ Registry.prototype.checkConfig = function (config) {
298
+ let cg = { ...this.newModel(), ...config };
299
+ let {
300
+ parent,
301
+ name,
302
+ title,
303
+ description,
304
+ version,
305
+ enabled,
306
+ auto_start,
307
+ class_name,
308
+ config_file,
309
+ main,
310
+ dependencies
311
+ } = cg;
312
+ if (!parent) {
313
+ cg.parent = '';
459
314
  }
460
- if (!dirs || !Array.isArray(dirs)) {
461
- return [];
315
+ if (!name) {
316
+ throw new Error('模块名称不能为空');
462
317
  }
463
-
464
- // 1. 扫描文件系统
465
- var file_dict = this._scanFileSystem(scanner, dirs, force);
466
-
467
- // 2. 与注册表交叉比对
468
- var registry_dict = this._buildRegistryMap();
469
-
470
- // 3. 构建全量目录
471
- var result = this._buildCatalog(file_dict, registry_dict);
472
-
473
- // 4. 补充注册表中有但文件系统中不存在的模块
474
- this._appendMissingModules(result.catalog, result.processed, registry_dict);
475
-
476
- return result.catalog;
318
+ if (!title) {
319
+ throw new Error('模块标题不能为空');
320
+ }
321
+ if (!description) {
322
+ throw new Error('模块描述不能为空');
323
+ }
324
+ if (!config_file) {
325
+ throw new Error('模块配置文件不能为空');
326
+ }
327
+ if (!['true', 'false', 'auto'].includes(auto_start)) {
328
+ throw new Error('模块自动启动配置错误,只能为true、false、auto');
329
+ }
330
+ if (!this.class_dict[class_name]) {
331
+ throw new Error('模块类不存在');
332
+ }
333
+ if (!enabled) {
334
+ cg.enabled = false;
335
+ } else if (enabled === 'true') {
336
+ cg.enabled = true;
337
+ }
338
+ if (!version) {
339
+ cg.version = '1.0.0';
340
+ }
341
+ if (!main) {
342
+ cg.main = '';
343
+ }
344
+ if (!dependencies) {
345
+ cg.dependencies = {};
346
+ }
347
+ return cg;
477
348
  };
478
349
 
479
350
  /**
480
- * 扫描文件系统,构建文件映射
481
- * @param {object} scanner DirScanner 实例
482
- * @param {string[]} dirs 目录列表
483
- * @param {boolean} force 是否强制扫描
484
- * @returns {object} 文件路径到配置的映射
485
- * @private
351
+ * 获取模块类文件
352
+ * @param {string} class_name 模块类名
353
+ * @returns {string} 模块类文件路径
486
354
  */
487
- Registry.prototype._scanFileSystem = function (scanner, dirs, force) {
488
- var file_dict = {};
489
- for (var i = 0; i < dirs.length; i++) {
490
- var dir = dirs[i];
491
- var entries = scanner.scanAndParse(dir, '*.json', null, !!force);
492
- for (var j = 0; j < entries.length; j++) {
493
- var entry = entries[j];
494
- file_dict[entry.file] = entry.config;
495
- }
496
- }
497
- return file_dict;
355
+ Registry.prototype.getClassFile = function (class_name) {
356
+ let file_name = this.config.name + '_class.json';
357
+ let file = file_name.fullname(this.config.dir);
358
+ file.addDir();
359
+ return file;
498
360
  };
499
361
 
500
362
  /**
501
- * 构建注册表名称映射
502
- * @returns {object} 名称到注册表项的映射
503
- * @private
363
+ * 获取模块文件
364
+ * @returns {string} 模块文件路径
504
365
  */
505
- Registry.prototype._buildRegistryMap = function () {
506
- var registry_dict = {};
507
- var reg_modules = this._data.modules;
508
- for (var k = 0; k < reg_modules.length; k++) {
509
- var reg = reg_modules[k];
510
- registry_dict[reg.name] = reg;
511
- }
512
- return registry_dict;
366
+ Registry.prototype.getModuleFile = function () {
367
+ let file_name = this.config.name + '_module.json';
368
+ let file = file_name.fullname(this.config.dir);
369
+ file.addDir();
370
+ return file;
513
371
  };
514
372
 
515
373
  /**
516
- * 从文件系统扫描结果构建目录条目
517
- * @param {object} file_dict 文件映射
518
- * @param {object} registry_dict 注册表映射
519
- * @returns {Array} 目录条目列表
520
- * @private
374
+ * 读取模块
375
+ * @returns {Array} 模块配置项数组
521
376
  */
522
- Registry.prototype._buildCatalog = function (file_dict, registry_dict) {
523
- var catalog = [];
524
- var processed = {};
525
-
526
- for (var file_path in file_dict) {
527
- var cfg = file_dict[file_path];
528
- var name = cfg.name || path.basename(file_path, path.extname(file_path));
529
- var reg_ref = registry_dict[name] || null;
530
-
531
- var item = {
532
- name: name,
533
- title: cfg.title || '',
534
- type: cfg.type || 'plugin',
535
- sort: cfg.sort || 100,
536
- config_file: file_path,
537
- main: cfg.main || '',
538
- registered: !!reg_ref,
539
- auto_start: reg_ref ? !!reg_ref.auto_start : false,
540
- lazy: reg_ref ? reg_ref.lazy : 'auto',
541
- // eslint-disable-next-line mm_eslint/variable-name
542
- lifecycle_state: null,
543
- file_exists: true,
544
- status: 'unregistered'
545
- };
546
-
547
- this._addRuntimeState(item, reg_ref, name);
548
-
549
- catalog.push(item);
550
- processed[name] = true;
551
- }
552
-
553
- return { catalog: catalog, processed: processed };
377
+ Registry.prototype.read = function () {
378
+ this.class_dict = this.getClassFile().loadJson() || this.getDefaultClassDict();
379
+ this.module_list = this.getModuleFile().loadJson() || [];
554
380
  };
555
381
 
556
382
  /**
557
- * 补充运行时状态
558
- * @param {object} item 目录条目
559
- * @param {object|null} reg_ref 注册表项
560
- * @param {string} name 模块名称
561
- * @private
383
+ * 保存模块
384
+ * @param {object} config 模块配置项
385
+ * @returns {object} 模块配置项
562
386
  */
563
- Registry.prototype._addRuntimeState = function (item, reg_ref, name) {
564
- if (!reg_ref || !this._manager) {
565
- return;
566
- }
567
-
568
- var mod = this._manager.getMod(name);
569
- if (mod) {
570
- // eslint-disable-next-line no-param-reassign
571
- item.lifecycle_state = mod.lifecycle_state;
572
- // eslint-disable-next-line no-param-reassign
573
- item.status = Registry._calcStatus(mod.lifecycle_state);
574
- } else {
575
- // eslint-disable-next-line no-param-reassign
576
- item.status = 'stopped';
577
- }
387
+ Registry.prototype.save = function () {
388
+ this.getClassFile().saveJson(this.class_dict);
389
+ this.getModuleFile().saveJson(this.module_list);
578
390
  };
579
391
 
580
392
  /**
581
- * 追加注册表中有但文件系统中不存在的模块
582
- * @param {Array} catalog 目录列表
583
- * @param {object} processed 已处理模块名称集合
584
- * @param {object} registry_dict 注册表映射
585
- * @private
393
+ * 排序模块
586
394
  */
587
- // eslint-disable-next-line complexity
588
- Registry.prototype._appendMissingModules = function (catalog, processed, registry_dict) {
589
- var reg_modules = this._data.modules;
590
-
591
- for (var n = 0; n < reg_modules.length; n++) {
592
- var reg = reg_modules[n];
593
- if (processed[reg.name]) {
594
- continue;
395
+ Registry.prototype.sort = function () {
396
+ this.module_list.sort((a, b) => {
397
+ if (a.parent !== b.parent) {
398
+ return a.parent.localeCompare(b.parent);
595
399
  }
596
-
597
- var mod = this._manager ? this._manager.getMod(reg.name) : null;
598
- catalog.push({
599
- name: reg.name,
600
- title: reg.title || '',
601
- type: reg.type || 'plugin',
602
- sort: reg.sort || 100,
603
- config_file: reg.config_file || '',
604
- main: reg.main || '',
605
- registered: true,
606
- auto_start: !!reg.auto_start,
607
- lazy: reg.lazy || 'auto',
608
- // eslint-disable-next-line mm_eslint/variable-name
609
- lifecycle_state: mod ? mod.lifecycle_state : null,
610
- file_exists: false,
611
- status: 'file_missing'
612
- });
613
- }
400
+ if (a.sort !== b.sort) {
401
+ return a.sort - b.sort;
402
+ }
403
+ if (a.name !== b.name) {
404
+ return a.name.localeCompare(b.name);
405
+ }
406
+ return 0;
407
+ });
614
408
  };
615
409
 
616
- /**
617
- * 根据 lifecycle_state 计算状态
618
- * @param {string} state 生命周期状态
619
- * @returns {string} 状态值
620
- * @private
621
- * @static
622
- */
623
- Registry._calcStatus = function (state) {
624
- switch (state) {
625
- case 'started':
626
- return 'running';
627
- case 'paused':
628
- return 'paused';
629
- default:
630
- return 'stopped';
631
- }
410
+ module.exports = {
411
+ Registry
632
412
  };
633
-
634
- module.exports = { Registry };