mm_machine 3.5.4 → 3.5.6
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/README.md +1 -1
- package/example/index.js +66 -0
- package/example/modules/hello/index.js +50 -0
- package/example/modules/hello/manager.json +24 -0
- package/index.js +3 -3
- package/lib/drive.js +150 -165
- package/lib/manager.js +280 -133
- package/lib/mod.js +499 -212
- package/package.json +19 -8
- package/lib/auto_require.js +0 -174
- package/lib/scanner.js +0 -225
package/lib/drive.js
CHANGED
|
@@ -1,8 +1,38 @@
|
|
|
1
1
|
const path = require('path');
|
|
2
|
-
require('mm_tpl');
|
|
3
2
|
const { conf } = require('mm_config');
|
|
3
|
+
const { getTpl } = require('mm_tpl');
|
|
4
|
+
const { hotReloadAdmin } = require('mm_hot_reload');
|
|
5
|
+
const {
|
|
6
|
+
string,
|
|
7
|
+
fullname,
|
|
8
|
+
hasFile,
|
|
9
|
+
loadText,
|
|
10
|
+
saveText,
|
|
11
|
+
loadJson,
|
|
12
|
+
saveJson,
|
|
13
|
+
push,
|
|
14
|
+
addDir,
|
|
15
|
+
delFile,
|
|
16
|
+
delDir,
|
|
17
|
+
dirname,
|
|
18
|
+
basename
|
|
19
|
+
} = require('mm_helper');
|
|
4
20
|
const { Mod } = require('./mod');
|
|
5
21
|
|
|
22
|
+
// 热重载管理器,按作用域惰性创建,避免模块加载时立即启动文件监听
|
|
23
|
+
let hot_reload_admin = null;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 获取热重载管理器
|
|
27
|
+
* @returns {object} 热重载管理器实例
|
|
28
|
+
*/
|
|
29
|
+
function getHotReload() {
|
|
30
|
+
if (!hot_reload_admin) {
|
|
31
|
+
hot_reload_admin = hotReloadAdmin('server');
|
|
32
|
+
}
|
|
33
|
+
return hot_reload_admin;
|
|
34
|
+
}
|
|
35
|
+
|
|
6
36
|
const MODE = {
|
|
7
37
|
// 生产模式
|
|
8
38
|
PRODUCTION: 1,
|
|
@@ -23,8 +53,6 @@ const MODE = {
|
|
|
23
53
|
class Drive extends Mod {
|
|
24
54
|
// 配置项
|
|
25
55
|
static config = {
|
|
26
|
-
// 名称, 由中英文和下"_"组成, 用于卸载接口 例如: demo
|
|
27
|
-
name: '',
|
|
28
56
|
// 标题, 介绍作用
|
|
29
57
|
title: '',
|
|
30
58
|
// 描述, 用于描述该有什么用的
|
|
@@ -34,7 +62,7 @@ class Drive extends Mod {
|
|
|
34
62
|
// 回调函数名 用于决定调用脚本的哪个函数
|
|
35
63
|
func_name: 'main',
|
|
36
64
|
// 作用域(同时作为检索的后缀名)
|
|
37
|
-
scope:
|
|
65
|
+
scope: 'server',
|
|
38
66
|
// 排序
|
|
39
67
|
sort: 10,
|
|
40
68
|
// 状态, 0表示未启用, 1表示启用
|
|
@@ -47,6 +75,9 @@ class Drive extends Mod {
|
|
|
47
75
|
// 当前配置文件路径
|
|
48
76
|
config_file = '';
|
|
49
77
|
|
|
78
|
+
// 选项值容器,由 config.options 声明的 schema 解析生成(config.options 仅存 schema)
|
|
79
|
+
_options = null;
|
|
80
|
+
|
|
50
81
|
// 当前脚本文件路径
|
|
51
82
|
script_file = '';
|
|
52
83
|
/**
|
|
@@ -68,16 +99,9 @@ class Drive extends Mod {
|
|
|
68
99
|
Drive.prototype._onInit = function () {
|
|
69
100
|
// 初始化
|
|
70
101
|
this.on('init:before', async (ctx) => {
|
|
71
|
-
this.setState('initing');
|
|
72
102
|
this._initOptions();
|
|
73
103
|
await this._initCore(...ctx.params);
|
|
74
104
|
});
|
|
75
|
-
this.on('init:success', (ctx) => {
|
|
76
|
-
this.isInited(true);
|
|
77
|
-
});
|
|
78
|
-
this.on('init:after', (ctx) => {
|
|
79
|
-
this.setState('inited');
|
|
80
|
-
});
|
|
81
105
|
};
|
|
82
106
|
|
|
83
107
|
/**
|
|
@@ -87,57 +111,41 @@ Drive.prototype._onInit = function () {
|
|
|
87
111
|
Drive.prototype._onLoad = async function (ctx) {
|
|
88
112
|
// 1. 加载
|
|
89
113
|
this.on('load:before', async (ctx) => {
|
|
90
|
-
this.setState('loading');
|
|
91
114
|
this.loadScript();
|
|
92
115
|
await this._loadCore(...ctx.params);
|
|
93
116
|
});
|
|
94
|
-
this.on('load:after', (ctx) => {
|
|
95
|
-
this.setState('loaded');
|
|
96
|
-
this.isLoaded(true);
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* 获取事件触发器
|
|
102
|
-
* @returns {object} 事件触发器
|
|
103
|
-
*/
|
|
104
|
-
Drive.prototype.getEventer = function () {
|
|
105
|
-
return $.eventer;
|
|
106
117
|
};
|
|
107
118
|
|
|
108
119
|
/**
|
|
109
|
-
*
|
|
120
|
+
* 应用配置(配置由管理器读取后传入)
|
|
110
121
|
* @param {string} file 配置文件路径
|
|
122
|
+
* @param {object} config 配置对象
|
|
111
123
|
* @returns {object} 配置项
|
|
112
124
|
*/
|
|
113
|
-
Drive.prototype.
|
|
125
|
+
Drive.prototype.applyConfig = function (file, config) {
|
|
114
126
|
if (file) {
|
|
115
127
|
this.config_file = file;
|
|
116
|
-
//
|
|
128
|
+
// 清除构造阶段缓存的目录与脚本路径,按配置文件重新推导
|
|
117
129
|
this._dir = '';
|
|
118
130
|
this.script_file = '';
|
|
119
131
|
}
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
let config = this._loadHotReload(this.config_file);
|
|
123
|
-
if (!config) {
|
|
124
|
-
this.newConfig(this.config_file);
|
|
125
|
-
config = this._loadHotReload(this.config_file);
|
|
126
|
-
}
|
|
127
|
-
this.setConfig(config);
|
|
128
|
-
return config;
|
|
129
|
-
} catch (err) {
|
|
130
|
-
this.log('error', '加载配置失败', this._wrapError(err));
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
132
|
+
this.setConfig(config);
|
|
133
|
+
return this.config;
|
|
133
134
|
};
|
|
134
135
|
|
|
135
136
|
/**
|
|
136
|
-
*
|
|
137
|
+
* 监听配置文件变化(热更新模式下生效)
|
|
137
138
|
* @param {string} file 配置文件路径
|
|
139
|
+
* @returns {object|null} 热更新管理器加载的配置,未开启热更新时返回 null
|
|
138
140
|
*/
|
|
139
|
-
Drive.prototype.
|
|
140
|
-
|
|
141
|
+
Drive.prototype.watchConfig = function (file) {
|
|
142
|
+
if (!file) return null;
|
|
143
|
+
if (this.mode >= MODE.HOT_CONFIG) {
|
|
144
|
+
return getHotReload().load(file, (config) => {
|
|
145
|
+
this._handleHotReload(config);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
141
149
|
};
|
|
142
150
|
|
|
143
151
|
/**
|
|
@@ -146,7 +154,6 @@ Drive.prototype.loadConfig = function (file) {
|
|
|
146
154
|
* @returns {object} 配置项
|
|
147
155
|
*/
|
|
148
156
|
Drive.prototype.setConfig = function (config) {
|
|
149
|
-
var old_script = this.script_file;
|
|
150
157
|
if (config) {
|
|
151
158
|
if (this.config_file) {
|
|
152
159
|
// 合并配置并应用配置处理
|
|
@@ -163,11 +170,7 @@ Drive.prototype.setConfig = function (config) {
|
|
|
163
170
|
}
|
|
164
171
|
var file = this.config.main;
|
|
165
172
|
if (file) {
|
|
166
|
-
this.script_file =
|
|
167
|
-
}
|
|
168
|
-
// 用 script_file 字符串比较,避免对象引用比较导致误判
|
|
169
|
-
if (old_script !== this.script_file) {
|
|
170
|
-
this.isLoaded(false);
|
|
173
|
+
this.script_file = fullname(file, this.getDir());
|
|
171
174
|
}
|
|
172
175
|
this._preset();
|
|
173
176
|
return this.config;
|
|
@@ -182,13 +185,26 @@ Drive.prototype.getConfig = function () {
|
|
|
182
185
|
};
|
|
183
186
|
|
|
184
187
|
/**
|
|
185
|
-
*
|
|
186
|
-
* @
|
|
188
|
+
* 是否声明了选项 schema
|
|
189
|
+
* @returns {boolean} config.options 存在且非空时返回true
|
|
187
190
|
*/
|
|
188
|
-
Drive.prototype.
|
|
191
|
+
Drive.prototype._hasOptions = function () {
|
|
192
|
+
let options = this.config.options;
|
|
193
|
+
if (!options) return false;
|
|
194
|
+
if (Array.isArray(options)) return options.length > 0;
|
|
195
|
+
return Object.keys(options).length > 0;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 按数组形式的 schema 生成选项值
|
|
200
|
+
* @param {object[]} schema 选项 schema 数组,每项含 name 与 default
|
|
201
|
+
* @param {object} cached 已缓存的选项值
|
|
202
|
+
* @returns {object} 选项值对象
|
|
203
|
+
*/
|
|
204
|
+
Drive.prototype._buildOptionsFromArr = function (schema, cached) {
|
|
189
205
|
var values = {};
|
|
190
|
-
for (let i = 0; i <
|
|
191
|
-
let option =
|
|
206
|
+
for (let i = 0; i < schema.length; i++) {
|
|
207
|
+
let option = schema[i];
|
|
192
208
|
let key = option.name;
|
|
193
209
|
let value = cached[key];
|
|
194
210
|
if (cached.hasOwnProperty(key) && value !== undefined) {
|
|
@@ -201,15 +217,15 @@ Drive.prototype._buildOptionsFromArr = function (options, cached) {
|
|
|
201
217
|
};
|
|
202
218
|
|
|
203
219
|
/**
|
|
204
|
-
*
|
|
205
|
-
* @param {object}
|
|
206
|
-
* @param {object} cached
|
|
207
|
-
* @returns {object}
|
|
220
|
+
* 按字典形式的 schema 生成选项值
|
|
221
|
+
* @param {object} schema 选项 schema 对象,值为含 default 的定义
|
|
222
|
+
* @param {object} cached 已缓存的选项值
|
|
223
|
+
* @returns {object} 选项值对象
|
|
208
224
|
*/
|
|
209
|
-
Drive.prototype._buildOptionsFromDict = function (
|
|
225
|
+
Drive.prototype._buildOptionsFromDict = function (schema, cached) {
|
|
210
226
|
var values = {};
|
|
211
|
-
for (let key in
|
|
212
|
-
let option =
|
|
227
|
+
for (let key in schema) {
|
|
228
|
+
let option = schema[key];
|
|
213
229
|
let value = cached[key];
|
|
214
230
|
if (cached.hasOwnProperty(key) && value !== undefined) {
|
|
215
231
|
values[key] = value;
|
|
@@ -222,25 +238,25 @@ Drive.prototype._buildOptionsFromDict = function (options, cached) {
|
|
|
222
238
|
|
|
223
239
|
|
|
224
240
|
/**
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
* 缓存存在时会补全
|
|
241
|
+
* 初始化选项值
|
|
242
|
+
* config.options 是选项 schema,本方法按 schema 的 default 与缓存文件生成选项值
|
|
243
|
+
* 缓存存在时会补全 schema 中新增键的默认值
|
|
228
244
|
*/
|
|
229
245
|
Drive.prototype._initOptions = function () {
|
|
230
|
-
|
|
231
|
-
|
|
246
|
+
if (!this._hasOptions()) return;
|
|
247
|
+
let schema = this.config.options;
|
|
232
248
|
let cache_file = this._getOptionsFile();
|
|
233
249
|
|
|
234
250
|
let cached = {};
|
|
235
251
|
// 尝试从缓存文件加载已有键值对
|
|
236
|
-
if (cache_file &&
|
|
237
|
-
cached =
|
|
252
|
+
if (cache_file && hasFile(cache_file)) {
|
|
253
|
+
cached = loadJson(cache_file) || {};
|
|
238
254
|
}
|
|
239
|
-
if (Array.isArray(
|
|
240
|
-
this._options = this._buildOptionsFromArr(
|
|
255
|
+
if (Array.isArray(schema)) {
|
|
256
|
+
this._options = this._buildOptionsFromArr(schema, cached);
|
|
241
257
|
}
|
|
242
258
|
else {
|
|
243
|
-
this._options = this._buildOptionsFromDict(
|
|
259
|
+
this._options = this._buildOptionsFromDict(schema, cached);
|
|
244
260
|
}
|
|
245
261
|
};
|
|
246
262
|
|
|
@@ -249,7 +265,7 @@ Drive.prototype._initOptions = function () {
|
|
|
249
265
|
* @returns {string} 缓存目录
|
|
250
266
|
*/
|
|
251
267
|
Drive.prototype._getCacheDir = function () {
|
|
252
|
-
return './cache/options'
|
|
268
|
+
return fullname('./cache/options');
|
|
253
269
|
};
|
|
254
270
|
|
|
255
271
|
/**
|
|
@@ -284,15 +300,15 @@ Drive.prototype._getOptionsFile = function () {
|
|
|
284
300
|
};
|
|
285
301
|
|
|
286
302
|
/**
|
|
287
|
-
*
|
|
288
|
-
* @returns {object}
|
|
303
|
+
* 获取所有选项值
|
|
304
|
+
* @returns {object} 所有选项值
|
|
289
305
|
*/
|
|
290
306
|
Drive.prototype.getOptions = function () {
|
|
291
307
|
return this._options || {};
|
|
292
308
|
};
|
|
293
309
|
|
|
294
310
|
/**
|
|
295
|
-
*
|
|
311
|
+
* 获取选项值
|
|
296
312
|
* @param {string} key 选项键
|
|
297
313
|
* @returns {*} 选项值
|
|
298
314
|
*/
|
|
@@ -302,17 +318,17 @@ Drive.prototype.getOption = function (key) {
|
|
|
302
318
|
};
|
|
303
319
|
|
|
304
320
|
/**
|
|
305
|
-
*
|
|
306
|
-
* @param {object} options
|
|
321
|
+
* 设置所有选项值
|
|
322
|
+
* @param {object} options 选项值对象
|
|
307
323
|
*/
|
|
308
324
|
Drive.prototype.setOptions = function (options) {
|
|
309
|
-
if (!options) return;
|
|
310
|
-
|
|
325
|
+
if (!this._hasOptions() || !options) return;
|
|
326
|
+
push(this._options, options);
|
|
311
327
|
this.setOptionsToFile();
|
|
312
328
|
};
|
|
313
329
|
|
|
314
330
|
/**
|
|
315
|
-
*
|
|
331
|
+
* 设置选项值并保存到缓存文件
|
|
316
332
|
* @param {string} key 选项键
|
|
317
333
|
* @param {*} value 选项值
|
|
318
334
|
*/
|
|
@@ -320,6 +336,7 @@ Drive.prototype.setOption = function (key, value) {
|
|
|
320
336
|
if (typeof key !== 'string') {
|
|
321
337
|
throw new TypeError('key必须是字符串');
|
|
322
338
|
}
|
|
339
|
+
if (!this._hasOptions()) return;
|
|
323
340
|
if (!this._options) {
|
|
324
341
|
this._options = {};
|
|
325
342
|
}
|
|
@@ -327,12 +344,16 @@ Drive.prototype.setOption = function (key, value) {
|
|
|
327
344
|
this.setOptionsToFile();
|
|
328
345
|
};
|
|
329
346
|
|
|
347
|
+
/**
|
|
348
|
+
* 将选项值保存到缓存文件
|
|
349
|
+
*/
|
|
330
350
|
Drive.prototype.setOptionsToFile = function () {
|
|
351
|
+
if (!this._hasOptions()) return;
|
|
331
352
|
let cache_file = this._getOptionsFile();
|
|
332
353
|
if (cache_file) {
|
|
333
354
|
// 确保目录存在
|
|
334
|
-
|
|
335
|
-
|
|
355
|
+
addDir(cache_file);
|
|
356
|
+
saveJson(cache_file, this._options);
|
|
336
357
|
}
|
|
337
358
|
};
|
|
338
359
|
|
|
@@ -344,17 +365,17 @@ Drive.prototype.getDir = function () {
|
|
|
344
365
|
let dir = this._dir;
|
|
345
366
|
if (!dir) {
|
|
346
367
|
if (this.script_file) {
|
|
347
|
-
dir = this.script_file
|
|
368
|
+
dir = dirname(this.script_file);
|
|
348
369
|
this._dir = dir;
|
|
349
370
|
} else if (this.config_file) {
|
|
350
|
-
dir = this.config_file
|
|
371
|
+
dir = dirname(this.config_file);
|
|
351
372
|
this._dir = dir;
|
|
352
373
|
} else {
|
|
353
374
|
let parent = this.getParent();
|
|
354
375
|
if (parent) {
|
|
355
376
|
let parent_dir = parent.getDir() || '';
|
|
356
377
|
if (parent_dir) {
|
|
357
|
-
dir = ('./' + this.config.name
|
|
378
|
+
dir = fullname('./' + this.config.name, parent_dir);
|
|
358
379
|
}
|
|
359
380
|
}
|
|
360
381
|
}
|
|
@@ -379,7 +400,7 @@ Drive.prototype._remove = function () {
|
|
|
379
400
|
|
|
380
401
|
try {
|
|
381
402
|
if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
|
|
382
|
-
|
|
403
|
+
getHotReload().unload(file);
|
|
383
404
|
} else {
|
|
384
405
|
// 移除模块缓存
|
|
385
406
|
let file_name = require.resolve(file);
|
|
@@ -398,7 +419,6 @@ Drive.prototype._remove = function () {
|
|
|
398
419
|
*/
|
|
399
420
|
Drive.prototype.unloadScript = function () {
|
|
400
421
|
this._remove();
|
|
401
|
-
this.isLoaded(false);
|
|
402
422
|
};
|
|
403
423
|
|
|
404
424
|
/**
|
|
@@ -410,18 +430,6 @@ Drive.prototype.reloadScript = function () {
|
|
|
410
430
|
return this.loadScript();
|
|
411
431
|
};
|
|
412
432
|
|
|
413
|
-
/**
|
|
414
|
-
* 卸载之后处理
|
|
415
|
-
* @param {boolean} remove 是否删除文件
|
|
416
|
-
*/
|
|
417
|
-
Drive.prototype.unloadCore = async function (remove) {
|
|
418
|
-
// 删除脚本
|
|
419
|
-
this.unloadScript();
|
|
420
|
-
if (remove) {
|
|
421
|
-
this.delFile();
|
|
422
|
-
}
|
|
423
|
-
};
|
|
424
|
-
|
|
425
433
|
/**
|
|
426
434
|
* 获取脚本文件路径
|
|
427
435
|
* @returns {string | null} 完整文件路径
|
|
@@ -430,8 +438,8 @@ Drive.prototype._getScriptFile = function () {
|
|
|
430
438
|
let main = this.config.main || '';
|
|
431
439
|
if (!main) return null;
|
|
432
440
|
|
|
433
|
-
let filename =
|
|
434
|
-
if (!
|
|
441
|
+
let filename = fullname(main, this.getDir());
|
|
442
|
+
if (!hasFile(filename)) {
|
|
435
443
|
this.newScript(filename);
|
|
436
444
|
}
|
|
437
445
|
return filename;
|
|
@@ -446,7 +454,7 @@ Drive.prototype.newScript = function (file) {
|
|
|
446
454
|
};
|
|
447
455
|
|
|
448
456
|
Drive.prototype.renderTpl = function (tpl, model) {
|
|
449
|
-
return
|
|
457
|
+
return getTpl().render(tpl, model);
|
|
450
458
|
};
|
|
451
459
|
|
|
452
460
|
/**
|
|
@@ -461,7 +469,7 @@ Drive.prototype._createScriptFile = function (file) {
|
|
|
461
469
|
return '';
|
|
462
470
|
}
|
|
463
471
|
let code = this.prettyCode(content, 'js');
|
|
464
|
-
|
|
472
|
+
saveText(file, code);
|
|
465
473
|
return file;
|
|
466
474
|
};
|
|
467
475
|
|
|
@@ -470,9 +478,9 @@ Drive.prototype._createScriptFile = function (file) {
|
|
|
470
478
|
* @returns {string} 脚本模板内容
|
|
471
479
|
*/
|
|
472
480
|
Drive.prototype._getScriptTpl = function () {
|
|
473
|
-
let f = './script.tpl.js'
|
|
474
|
-
if (
|
|
475
|
-
return
|
|
481
|
+
let f = fullname('./script.tpl.js', this._getTplDir());
|
|
482
|
+
if (hasFile(f)) {
|
|
483
|
+
return loadText(f);
|
|
476
484
|
} else {
|
|
477
485
|
return `module.exports = {
|
|
478
486
|
/**
|
|
@@ -511,7 +519,7 @@ Drive.prototype._getScriptTpl = function () {
|
|
|
511
519
|
|
|
512
520
|
/**
|
|
513
521
|
* 加载脚本文件,支持热重载模式
|
|
514
|
-
* 在开发模式(mode=3或4)
|
|
522
|
+
* 在开发模式(mode=3或4)下使用热重载管理器进行热重载
|
|
515
523
|
* 在生产模式下使用标准的 require 加载
|
|
516
524
|
* @returns {object | null} 加载的模块对象
|
|
517
525
|
*/
|
|
@@ -520,7 +528,7 @@ Drive.prototype.loadScript = function () {
|
|
|
520
528
|
if (!file) return null;
|
|
521
529
|
try {
|
|
522
530
|
if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
|
|
523
|
-
let cs =
|
|
531
|
+
let cs = getHotReload().load(file, (mod) => {
|
|
524
532
|
this._setMainMethod(mod);
|
|
525
533
|
});
|
|
526
534
|
this._setMainMethod(cs);
|
|
@@ -543,7 +551,7 @@ Drive.prototype.loadScript = function () {
|
|
|
543
551
|
Drive.prototype._setMainMethod = function (mod) {
|
|
544
552
|
if (!mod) return;
|
|
545
553
|
let name = this.config.func_name || 'main';
|
|
546
|
-
if (
|
|
554
|
+
if (typeof mod[name] === 'function') {
|
|
547
555
|
this.main = mod[name];
|
|
548
556
|
}
|
|
549
557
|
for (let key in mod) {
|
|
@@ -554,7 +562,6 @@ Drive.prototype._setMainMethod = function (mod) {
|
|
|
554
562
|
this.setData(key, obj);
|
|
555
563
|
}
|
|
556
564
|
}
|
|
557
|
-
this.isLoaded(true);
|
|
558
565
|
};
|
|
559
566
|
|
|
560
567
|
/**
|
|
@@ -563,7 +570,7 @@ Drive.prototype._setMainMethod = function (mod) {
|
|
|
563
570
|
* @returns {string} 配置文件作用域
|
|
564
571
|
*/
|
|
565
572
|
Drive.prototype._getScope = function (file) {
|
|
566
|
-
return file.replace(/\\/g, '/')
|
|
573
|
+
return string.between(file.replace(/\\/g, '/'), 'app/', '/');
|
|
567
574
|
};
|
|
568
575
|
|
|
569
576
|
/**
|
|
@@ -626,7 +633,7 @@ Drive.prototype._createConfigFile = function (file, model) {
|
|
|
626
633
|
let tpl = this._getConfigTpl();
|
|
627
634
|
let content = this.renderTpl(tpl, model || this.getModel('config'));
|
|
628
635
|
let code = this.prettyCode(content, 'json');
|
|
629
|
-
|
|
636
|
+
saveText(file, code);
|
|
630
637
|
return file;
|
|
631
638
|
};
|
|
632
639
|
|
|
@@ -637,7 +644,7 @@ Drive.prototype._createConfigFile = function (file, model) {
|
|
|
637
644
|
* @returns {string} 配置文件内容
|
|
638
645
|
*/
|
|
639
646
|
Drive.prototype.createConfigFile = function (file, model) {
|
|
640
|
-
|
|
647
|
+
addDir(file);
|
|
641
648
|
return this._createConfigFile(file, model);
|
|
642
649
|
};
|
|
643
650
|
|
|
@@ -646,9 +653,9 @@ Drive.prototype.createConfigFile = function (file, model) {
|
|
|
646
653
|
* @returns {string} 配置模板内容
|
|
647
654
|
*/
|
|
648
655
|
Drive.prototype._getConfigTpl = function () {
|
|
649
|
-
let f = './config.tpl.json'
|
|
650
|
-
if (
|
|
651
|
-
return
|
|
656
|
+
let f = fullname('./config.tpl.json', this._getTplDir());
|
|
657
|
+
if (hasFile(f)) {
|
|
658
|
+
return loadText(f);
|
|
652
659
|
} else {
|
|
653
660
|
return `{
|
|
654
661
|
// 模块名称,必填
|
|
@@ -679,13 +686,13 @@ Drive.prototype._getConfigTpl = function () {
|
|
|
679
686
|
*/
|
|
680
687
|
Drive.prototype.loadFile = function (file, name) {
|
|
681
688
|
try {
|
|
682
|
-
let filename =
|
|
683
|
-
let text =
|
|
689
|
+
let filename = fullname(file, this.getDir());
|
|
690
|
+
let text = loadText(filename);
|
|
684
691
|
|
|
685
692
|
// 如果文件不存在,创建新的配置文件
|
|
686
693
|
if (!text) {
|
|
687
694
|
this.newConfig(filename);
|
|
688
|
-
text =
|
|
695
|
+
text = loadText(filename);
|
|
689
696
|
}
|
|
690
697
|
|
|
691
698
|
if (!text) return null;
|
|
@@ -700,15 +707,18 @@ Drive.prototype.loadFile = function (file, name) {
|
|
|
700
707
|
};
|
|
701
708
|
|
|
702
709
|
/**
|
|
703
|
-
*
|
|
710
|
+
* 加载配置文件(JSON5 语法),热更新模式下同时注册文件监听
|
|
704
711
|
* @param {string} file 完整文件路径
|
|
705
|
-
* @returns {object | Array | null} 配置对象
|
|
712
|
+
* @returns {object | Array | null | undefined} 配置对象
|
|
706
713
|
*/
|
|
707
714
|
Drive.prototype._loadHotReload = function (file) {
|
|
715
|
+
if (!file) return null;
|
|
708
716
|
if (this.mode >= MODE.HOT_CONFIG) {
|
|
709
|
-
return
|
|
717
|
+
return getHotReload().load(file, (config) => {
|
|
718
|
+
this._handleHotReload(config);
|
|
719
|
+
});
|
|
710
720
|
}
|
|
711
|
-
return
|
|
721
|
+
return loadJson(file);
|
|
712
722
|
};
|
|
713
723
|
|
|
714
724
|
/**
|
|
@@ -764,8 +774,8 @@ Drive.prototype._reloadIfNeeded = function (old_script) {
|
|
|
764
774
|
*/
|
|
765
775
|
Drive.prototype.delDir = function () {
|
|
766
776
|
let main = this.config.main;
|
|
767
|
-
if (main
|
|
768
|
-
|
|
777
|
+
if (main) {
|
|
778
|
+
delDir(this.getDir());
|
|
769
779
|
}
|
|
770
780
|
};
|
|
771
781
|
|
|
@@ -781,15 +791,15 @@ Drive.prototype.delFile = function () {
|
|
|
781
791
|
let error_message = null;
|
|
782
792
|
try {
|
|
783
793
|
// 删除配置文件(JSON)
|
|
784
|
-
if (cfg_file &&
|
|
785
|
-
if (
|
|
794
|
+
if (cfg_file && hasFile(cfg_file)) {
|
|
795
|
+
if (loadText(cfg_file)) {
|
|
786
796
|
error_message = this._delConfigFile(cfg_file, name);
|
|
787
797
|
}
|
|
788
798
|
}
|
|
789
799
|
// 删除脚本文件(JS,不尝试 JSON 解析)
|
|
790
|
-
if (script_file &&
|
|
800
|
+
if (script_file && hasFile(script_file)) {
|
|
791
801
|
this._remove();
|
|
792
|
-
|
|
802
|
+
delFile(script_file);
|
|
793
803
|
}
|
|
794
804
|
} catch (err) {
|
|
795
805
|
this.log('error', `删除文件失败`, err);
|
|
@@ -805,13 +815,13 @@ Drive.prototype.delFile = function () {
|
|
|
805
815
|
* @returns {string | null} 错误消息
|
|
806
816
|
*/
|
|
807
817
|
Drive.prototype._delConfigFile = function (file, name) {
|
|
808
|
-
let text =
|
|
818
|
+
let text = loadText(file);
|
|
809
819
|
if (!text) {
|
|
810
820
|
this.delDir();
|
|
811
821
|
return null;
|
|
812
822
|
}
|
|
813
823
|
|
|
814
|
-
let config =
|
|
824
|
+
let config = loadJson(file);
|
|
815
825
|
if (Array.isArray(config)) {
|
|
816
826
|
return this._delArray(file, config, name);
|
|
817
827
|
}
|
|
@@ -835,9 +845,9 @@ Drive.prototype._delArray = function (file, config, name) {
|
|
|
835
845
|
config.splice(index, 1);
|
|
836
846
|
|
|
837
847
|
if (config.length > 0) {
|
|
838
|
-
|
|
839
|
-
} else if (file
|
|
840
|
-
|
|
848
|
+
saveText(file, JSON.stringify(config, null, 2));
|
|
849
|
+
} else if (hasFile(file)) {
|
|
850
|
+
delFile(file);
|
|
841
851
|
}
|
|
842
852
|
|
|
843
853
|
return null;
|
|
@@ -849,34 +859,9 @@ Drive.prototype._delArray = function (file, config, name) {
|
|
|
849
859
|
* @returns {string} 配置文件名称
|
|
850
860
|
*/
|
|
851
861
|
Drive.prototype._getName = function (file) {
|
|
852
|
-
|
|
853
|
-
return name;
|
|
854
|
-
// // 提取文件名作为模块名
|
|
855
|
-
// let word = new Word();
|
|
856
|
-
// // 将文件名改为驼峰命名法
|
|
857
|
-
// return word.toCamelCase(name);
|
|
862
|
+
return basename(dirname(file));
|
|
858
863
|
};
|
|
859
864
|
|
|
860
|
-
/**
|
|
861
|
-
* 加载前处理
|
|
862
|
-
*/
|
|
863
|
-
Drive.prototype.loadBefore = function () {
|
|
864
|
-
try {
|
|
865
|
-
let mod = this.loadScript();
|
|
866
|
-
if (mod) {
|
|
867
|
-
this.isLoaded(true);
|
|
868
|
-
}
|
|
869
|
-
} catch (err) {
|
|
870
|
-
this.log('error', `加载前处理失败`, this._wrapError(err));
|
|
871
|
-
this.isLoaded(false);
|
|
872
|
-
}
|
|
873
|
-
};
|
|
874
|
-
|
|
875
|
-
/**
|
|
876
|
-
* 加载处理
|
|
877
|
-
*/
|
|
878
|
-
Drive.prototype._loadCore = function () { };
|
|
879
|
-
|
|
880
865
|
/**
|
|
881
866
|
* 保存配置
|
|
882
867
|
*/
|
|
@@ -885,11 +870,11 @@ Drive.prototype.save = function () {
|
|
|
885
870
|
if (!this.config_file) return;
|
|
886
871
|
|
|
887
872
|
let file = this.config_file;
|
|
888
|
-
let text =
|
|
873
|
+
let text = loadText(file);
|
|
889
874
|
|
|
890
875
|
if (text && text.trim().startsWith('[')) {
|
|
891
876
|
// 数组格式配置
|
|
892
|
-
let config =
|
|
877
|
+
let config = loadJson(file);
|
|
893
878
|
if (Array.isArray(config)) {
|
|
894
879
|
// 查找并更新现有配置项
|
|
895
880
|
let index = config.findIndex((item) => item.name === this.config.name);
|
|
@@ -899,13 +884,13 @@ Drive.prototype.save = function () {
|
|
|
899
884
|
// 如果不存在,添加新配置项
|
|
900
885
|
config.push(this.config);
|
|
901
886
|
}
|
|
902
|
-
|
|
887
|
+
saveText(file, JSON.stringify(config, null, 2));
|
|
903
888
|
return;
|
|
904
889
|
}
|
|
905
890
|
}
|
|
906
891
|
|
|
907
892
|
// 单对象格式配置,直接保存
|
|
908
|
-
|
|
893
|
+
saveText(file, JSON.stringify(this.config, null, 2));
|
|
909
894
|
} catch (err) {
|
|
910
895
|
this.log('error', `保存配置失败`, err);
|
|
911
896
|
}
|
|
@@ -917,7 +902,7 @@ Drive.prototype.save = function () {
|
|
|
917
902
|
Drive.prototype.remove = function () {
|
|
918
903
|
let dir = this.getDir();
|
|
919
904
|
if (dir) {
|
|
920
|
-
|
|
905
|
+
delDir(dir);
|
|
921
906
|
}
|
|
922
907
|
return dir;
|
|
923
908
|
};
|