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/drive.js +480 -675
- package/index.js +47 -1497
- package/mod.js +452 -498
- package/package.json +3 -4
- package/registry.js +293 -515
- package/scanner.js +62 -0
- package/dir_scanner.js +0 -262
- package/id_gen.js +0 -138
- package/word.js +0 -209
package/registry.js
CHANGED
|
@@ -1,634 +1,412 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @class Registry
|
|
5
|
-
* @augments Drive
|
|
2
|
+
* 注册类 - 注册表模式
|
|
3
|
+
* 管理模块类的注册、模块配置的持久化、模块实例的懒加载
|
|
6
4
|
*/
|
|
7
|
-
|
|
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}
|
|
34
|
-
* @param {object} [cfg] 配置参数
|
|
35
|
-
* @param {string} [cfg.file] 注册表文件绝对路径,默认从 Manager 配置拼接
|
|
8
|
+
* @param {object} config 配置项
|
|
36
9
|
*/
|
|
37
|
-
constructor(
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
this.
|
|
44
|
-
this.
|
|
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.
|
|
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
|
-
*
|
|
72
|
-
* @returns {object} 注册表数据 { version, modules[] }
|
|
37
|
+
* 获取模块类字典
|
|
38
|
+
* @returns {object} 模块类字典
|
|
73
39
|
*/
|
|
74
|
-
Registry.prototype.
|
|
75
|
-
|
|
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
|
-
*
|
|
45
|
+
* 获取默认模块类
|
|
46
|
+
* @returns {object} 默认模块类
|
|
93
47
|
*/
|
|
94
|
-
Registry.prototype.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
-
* @
|
|
57
|
+
* 设置模块类
|
|
58
|
+
* @param {string} class_name 模块类名
|
|
59
|
+
* @param {string} file 模块类文件路径
|
|
110
60
|
*/
|
|
111
|
-
Registry.prototype.
|
|
112
|
-
|
|
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
|
-
* @
|
|
66
|
+
* 删除模块类
|
|
67
|
+
* @param {string} class_name 模块类名
|
|
143
68
|
*/
|
|
144
|
-
Registry.prototype.
|
|
145
|
-
|
|
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
|
-
* @
|
|
76
|
+
* 注册模块类到缓存
|
|
77
|
+
* @param {string} class_name 模块类名
|
|
78
|
+
* @param {Function} class_ref 模块类构造函数
|
|
166
79
|
*/
|
|
167
|
-
Registry.prototype.
|
|
168
|
-
|
|
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}
|
|
174
|
-
* @returns {
|
|
88
|
+
* 获取模块类构造函数
|
|
89
|
+
* @param {string} class_name 模块类名
|
|
90
|
+
* @returns {Function} 模块类构造函数
|
|
175
91
|
*/
|
|
176
|
-
Registry.prototype.
|
|
177
|
-
return this.
|
|
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
|
-
* @
|
|
97
|
+
* 注册模块配置到缓存(不实例化)
|
|
98
|
+
* @param {object} config 模块配置项
|
|
99
|
+
* @returns {object} 注册的配置项
|
|
100
|
+
* @throws {Error} 模块配置项错误
|
|
186
101
|
*/
|
|
187
|
-
Registry.prototype.
|
|
188
|
-
if (!
|
|
189
|
-
throw new TypeError('
|
|
102
|
+
Registry.prototype.registerModule = function (config) {
|
|
103
|
+
if (!config || !config.name) {
|
|
104
|
+
throw new TypeError('模块配置和模块名称不能为空');
|
|
190
105
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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.
|
|
206
|
-
|
|
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
|
-
*
|
|
120
|
+
* 获取模块实例(懒加载)
|
|
121
|
+
* 首次调用时根据 config 和已注册的类构造函数实例化
|
|
122
|
+
* @param {string} name 模块名
|
|
123
|
+
* @returns {object|null} 模块实例
|
|
236
124
|
*/
|
|
237
|
-
Registry.prototype.
|
|
238
|
-
var
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
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}
|
|
253
|
-
* @
|
|
141
|
+
* 按类名获取模块实例列表
|
|
142
|
+
* @param {string} class_name 模块类名
|
|
143
|
+
* @returns {Array} 模块实例数组
|
|
254
144
|
*/
|
|
255
|
-
Registry.prototype.
|
|
256
|
-
|
|
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
|
-
* @
|
|
154
|
+
* 注销模块
|
|
155
|
+
* @param {string} name 模块名
|
|
262
156
|
*/
|
|
263
|
-
Registry.prototype.
|
|
264
|
-
var
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
-
|
|
169
|
+
delete this._module_cache[name];
|
|
272
170
|
};
|
|
273
171
|
|
|
274
|
-
// ==================== 核心操作 ====================
|
|
275
|
-
|
|
276
172
|
/**
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
* 已存在的模块跳过(不重复创建)
|
|
173
|
+
* 加载注册数据
|
|
174
|
+
* 从 JSON 文件读取持久化数据,注册所有类和模块到缓存
|
|
280
175
|
*/
|
|
281
|
-
Registry.prototype.
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
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
|
-
|
|
292
|
-
|
|
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
|
-
*
|
|
300
|
-
* 根据 lazy 策略决定是否继续 init/start
|
|
301
|
-
* @returns {Promise<void>}
|
|
199
|
+
* 创建新模块配置项
|
|
200
|
+
* @returns {object} 模块配置项
|
|
302
201
|
*/
|
|
303
|
-
Registry.prototype.
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
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
|
-
*
|
|
330
|
-
* @param {object}
|
|
331
|
-
* @returns {
|
|
332
|
-
* @private
|
|
221
|
+
* 设置模块
|
|
222
|
+
* @param {object} config 模块配置项
|
|
223
|
+
* @returns {object} 模块配置项
|
|
333
224
|
*/
|
|
334
|
-
Registry.prototype.
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
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 (
|
|
341
|
-
|
|
342
|
-
return mgr_lazy === false || mgr_lazy === 'auto';
|
|
239
|
+
if (!has) {
|
|
240
|
+
this.module_list.push(cg);
|
|
343
241
|
}
|
|
344
|
-
|
|
242
|
+
// 清除外部缓存索引
|
|
243
|
+
this._config_map = null;
|
|
244
|
+
return cg;
|
|
345
245
|
};
|
|
346
246
|
|
|
347
247
|
/**
|
|
348
|
-
*
|
|
349
|
-
*
|
|
350
|
-
* @returns {
|
|
248
|
+
* 获取模块
|
|
249
|
+
* @param {string} name 模块名称
|
|
250
|
+
* @returns {object} 模块配置项
|
|
351
251
|
*/
|
|
352
|
-
Registry.prototype.
|
|
353
|
-
//
|
|
354
|
-
this.
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
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 {
|
|
372
|
-
* @returns {
|
|
265
|
+
* 删除模块
|
|
266
|
+
* @param {string} name 模块名称
|
|
267
|
+
* @returns {object} 模块配置项
|
|
373
268
|
*/
|
|
374
|
-
Registry.prototype.
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
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
|
-
|
|
278
|
+
// 清除外部缓存索引
|
|
279
|
+
this._config_map = null;
|
|
280
|
+
return obj;
|
|
388
281
|
};
|
|
389
282
|
|
|
390
283
|
/**
|
|
391
|
-
*
|
|
392
|
-
* @
|
|
393
|
-
* @returns {boolean} 是否注册成功
|
|
394
|
-
* @private
|
|
284
|
+
* 获取模块列表
|
|
285
|
+
* @returns {Array} 模块配置项数组
|
|
395
286
|
*/
|
|
396
|
-
|
|
397
|
-
|
|
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
|
-
* @
|
|
452
|
-
* @
|
|
453
|
-
* @param {boolean} [force] 是否强制重新扫描文件系统
|
|
454
|
-
* @returns {Array<object>} 全量目录列表
|
|
292
|
+
* 检查配置项
|
|
293
|
+
* @param {object} config 模块配置项
|
|
294
|
+
* @returns {object} 模块配置项
|
|
295
|
+
* @throws {Error} 模块配置项错误
|
|
455
296
|
*/
|
|
456
|
-
Registry.prototype.
|
|
457
|
-
|
|
458
|
-
|
|
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 (!
|
|
461
|
-
|
|
315
|
+
if (!name) {
|
|
316
|
+
throw new Error('模块名称不能为空');
|
|
462
317
|
}
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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 {
|
|
482
|
-
* @
|
|
483
|
-
* @param {boolean} force 是否强制扫描
|
|
484
|
-
* @returns {object} 文件路径到配置的映射
|
|
485
|
-
* @private
|
|
351
|
+
* 获取模块类文件
|
|
352
|
+
* @param {string} class_name 模块类名
|
|
353
|
+
* @returns {string} 模块类文件路径
|
|
486
354
|
*/
|
|
487
|
-
Registry.prototype.
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
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 {
|
|
503
|
-
* @private
|
|
363
|
+
* 获取模块文件
|
|
364
|
+
* @returns {string} 模块文件路径
|
|
504
365
|
*/
|
|
505
|
-
Registry.prototype.
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
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
|
-
* @
|
|
518
|
-
* @param {object} registry_dict 注册表映射
|
|
519
|
-
* @returns {Array} 目录条目列表
|
|
520
|
-
* @private
|
|
374
|
+
* 读取模块
|
|
375
|
+
* @returns {Array} 模块配置项数组
|
|
521
376
|
*/
|
|
522
|
-
Registry.prototype.
|
|
523
|
-
|
|
524
|
-
|
|
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}
|
|
559
|
-
* @
|
|
560
|
-
* @param {string} name 模块名称
|
|
561
|
-
* @private
|
|
383
|
+
* 保存模块
|
|
384
|
+
* @param {object} config 模块配置项
|
|
385
|
+
* @returns {object} 模块配置项
|
|
562
386
|
*/
|
|
563
|
-
Registry.prototype.
|
|
564
|
-
|
|
565
|
-
|
|
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
|
-
|
|
588
|
-
|
|
589
|
-
|
|
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
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
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
|
-
|
|
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 };
|