mm_machine 3.5.4 → 3.5.5
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 +69 -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 +144 -97
- package/lib/manager.js +122 -50
- package/lib/mod.js +693 -168
- package/lib/scanner.js +5 -5
- package/package.json +15 -5
- package/lib/auto_require.js +0 -174
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,
|
|
@@ -34,7 +64,7 @@ class Drive extends Mod {
|
|
|
34
64
|
// 回调函数名 用于决定调用脚本的哪个函数
|
|
35
65
|
func_name: 'main',
|
|
36
66
|
// 作用域(同时作为检索的后缀名)
|
|
37
|
-
scope:
|
|
67
|
+
scope: 'server',
|
|
38
68
|
// 排序
|
|
39
69
|
sort: 10,
|
|
40
70
|
// 状态, 0表示未启用, 1表示启用
|
|
@@ -47,6 +77,9 @@ class Drive extends Mod {
|
|
|
47
77
|
// 当前配置文件路径
|
|
48
78
|
config_file = '';
|
|
49
79
|
|
|
80
|
+
// 选项值容器,由 config.options 声明的 schema 解析生成(config.options 仅存 schema)
|
|
81
|
+
_options = null;
|
|
82
|
+
|
|
50
83
|
// 当前脚本文件路径
|
|
51
84
|
script_file = '';
|
|
52
85
|
/**
|
|
@@ -68,15 +101,15 @@ class Drive extends Mod {
|
|
|
68
101
|
Drive.prototype._onInit = function () {
|
|
69
102
|
// 初始化
|
|
70
103
|
this.on('init:before', async (ctx) => {
|
|
71
|
-
this.setState('initing');
|
|
104
|
+
await this.setState('initing');
|
|
72
105
|
this._initOptions();
|
|
73
106
|
await this._initCore(...ctx.params);
|
|
74
107
|
});
|
|
75
|
-
this.on('init:success', (ctx) => {
|
|
76
|
-
this.isInited(true);
|
|
108
|
+
this.on('init:success', async (ctx) => {
|
|
109
|
+
await this.isInited(true);
|
|
77
110
|
});
|
|
78
|
-
this.on('init:after', (ctx) => {
|
|
79
|
-
this.setState('inited');
|
|
111
|
+
this.on('init:after', async (ctx) => {
|
|
112
|
+
await this.setState('inited');
|
|
80
113
|
});
|
|
81
114
|
};
|
|
82
115
|
|
|
@@ -87,24 +120,16 @@ Drive.prototype._onInit = function () {
|
|
|
87
120
|
Drive.prototype._onLoad = async function (ctx) {
|
|
88
121
|
// 1. 加载
|
|
89
122
|
this.on('load:before', async (ctx) => {
|
|
90
|
-
this.setState('loading');
|
|
123
|
+
await this.setState('loading');
|
|
91
124
|
this.loadScript();
|
|
92
125
|
await this._loadCore(...ctx.params);
|
|
93
126
|
});
|
|
94
|
-
this.on('load:after', (ctx) => {
|
|
95
|
-
this.setState('loaded');
|
|
96
|
-
this.isLoaded(true);
|
|
127
|
+
this.on('load:after', async (ctx) => {
|
|
128
|
+
await this.setState('loaded');
|
|
129
|
+
await this.isLoaded(true);
|
|
97
130
|
});
|
|
98
131
|
};
|
|
99
132
|
|
|
100
|
-
/**
|
|
101
|
-
* 获取事件触发器
|
|
102
|
-
* @returns {object} 事件触发器
|
|
103
|
-
*/
|
|
104
|
-
Drive.prototype.getEventer = function () {
|
|
105
|
-
return $.eventer;
|
|
106
|
-
};
|
|
107
|
-
|
|
108
133
|
/**
|
|
109
134
|
* 获取配置
|
|
110
135
|
* @param {string} file 配置文件路径
|
|
@@ -163,10 +188,11 @@ Drive.prototype.setConfig = function (config) {
|
|
|
163
188
|
}
|
|
164
189
|
var file = this.config.main;
|
|
165
190
|
if (file) {
|
|
166
|
-
this.script_file =
|
|
191
|
+
this.script_file = fullname(file, this.getDir());
|
|
167
192
|
}
|
|
168
193
|
// 用 script_file 字符串比较,避免对象引用比较导致误判
|
|
169
|
-
|
|
194
|
+
// 构造阶段 script_file 由空变为配置值时无需重置(默认即为未加载)
|
|
195
|
+
if (old_script && old_script !== this.script_file) {
|
|
170
196
|
this.isLoaded(false);
|
|
171
197
|
}
|
|
172
198
|
this._preset();
|
|
@@ -182,13 +208,26 @@ Drive.prototype.getConfig = function () {
|
|
|
182
208
|
};
|
|
183
209
|
|
|
184
210
|
/**
|
|
185
|
-
*
|
|
186
|
-
* @
|
|
211
|
+
* 是否声明了选项 schema
|
|
212
|
+
* @returns {boolean} config.options 存在且非空时返回true
|
|
213
|
+
*/
|
|
214
|
+
Drive.prototype._hasOptions = function () {
|
|
215
|
+
let options = this.config.options;
|
|
216
|
+
if (!options) return false;
|
|
217
|
+
if (Array.isArray(options)) return options.length > 0;
|
|
218
|
+
return Object.keys(options).length > 0;
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* 按数组形式的 schema 生成选项值
|
|
223
|
+
* @param {object[]} schema 选项 schema 数组,每项含 name 与 default
|
|
224
|
+
* @param {object} cached 已缓存的选项值
|
|
225
|
+
* @returns {object} 选项值对象
|
|
187
226
|
*/
|
|
188
|
-
Drive.prototype._buildOptionsFromArr = function (
|
|
227
|
+
Drive.prototype._buildOptionsFromArr = function (schema, cached) {
|
|
189
228
|
var values = {};
|
|
190
|
-
for (let i = 0; i <
|
|
191
|
-
let option =
|
|
229
|
+
for (let i = 0; i < schema.length; i++) {
|
|
230
|
+
let option = schema[i];
|
|
192
231
|
let key = option.name;
|
|
193
232
|
let value = cached[key];
|
|
194
233
|
if (cached.hasOwnProperty(key) && value !== undefined) {
|
|
@@ -201,15 +240,15 @@ Drive.prototype._buildOptionsFromArr = function (options, cached) {
|
|
|
201
240
|
};
|
|
202
241
|
|
|
203
242
|
/**
|
|
204
|
-
*
|
|
205
|
-
* @param {object}
|
|
206
|
-
* @param {object} cached
|
|
207
|
-
* @returns {object}
|
|
243
|
+
* 按字典形式的 schema 生成选项值
|
|
244
|
+
* @param {object} schema 选项 schema 对象,值为含 default 的定义
|
|
245
|
+
* @param {object} cached 已缓存的选项值
|
|
246
|
+
* @returns {object} 选项值对象
|
|
208
247
|
*/
|
|
209
|
-
Drive.prototype._buildOptionsFromDict = function (
|
|
248
|
+
Drive.prototype._buildOptionsFromDict = function (schema, cached) {
|
|
210
249
|
var values = {};
|
|
211
|
-
for (let key in
|
|
212
|
-
let option =
|
|
250
|
+
for (let key in schema) {
|
|
251
|
+
let option = schema[key];
|
|
213
252
|
let value = cached[key];
|
|
214
253
|
if (cached.hasOwnProperty(key) && value !== undefined) {
|
|
215
254
|
values[key] = value;
|
|
@@ -222,25 +261,25 @@ Drive.prototype._buildOptionsFromDict = function (options, cached) {
|
|
|
222
261
|
|
|
223
262
|
|
|
224
263
|
/**
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
* 缓存存在时会补全
|
|
264
|
+
* 初始化选项值
|
|
265
|
+
* config.options 是选项 schema,本方法按 schema 的 default 与缓存文件生成选项值
|
|
266
|
+
* 缓存存在时会补全 schema 中新增键的默认值
|
|
228
267
|
*/
|
|
229
268
|
Drive.prototype._initOptions = function () {
|
|
230
|
-
|
|
231
|
-
|
|
269
|
+
if (!this._hasOptions()) return;
|
|
270
|
+
let schema = this.config.options;
|
|
232
271
|
let cache_file = this._getOptionsFile();
|
|
233
272
|
|
|
234
273
|
let cached = {};
|
|
235
274
|
// 尝试从缓存文件加载已有键值对
|
|
236
|
-
if (cache_file &&
|
|
237
|
-
cached =
|
|
275
|
+
if (cache_file && hasFile(cache_file)) {
|
|
276
|
+
cached = loadJson(cache_file) || {};
|
|
238
277
|
}
|
|
239
|
-
if (Array.isArray(
|
|
240
|
-
this._options = this._buildOptionsFromArr(
|
|
278
|
+
if (Array.isArray(schema)) {
|
|
279
|
+
this._options = this._buildOptionsFromArr(schema, cached);
|
|
241
280
|
}
|
|
242
281
|
else {
|
|
243
|
-
this._options = this._buildOptionsFromDict(
|
|
282
|
+
this._options = this._buildOptionsFromDict(schema, cached);
|
|
244
283
|
}
|
|
245
284
|
};
|
|
246
285
|
|
|
@@ -249,7 +288,7 @@ Drive.prototype._initOptions = function () {
|
|
|
249
288
|
* @returns {string} 缓存目录
|
|
250
289
|
*/
|
|
251
290
|
Drive.prototype._getCacheDir = function () {
|
|
252
|
-
return './cache/options'
|
|
291
|
+
return fullname('./cache/options');
|
|
253
292
|
};
|
|
254
293
|
|
|
255
294
|
/**
|
|
@@ -284,15 +323,15 @@ Drive.prototype._getOptionsFile = function () {
|
|
|
284
323
|
};
|
|
285
324
|
|
|
286
325
|
/**
|
|
287
|
-
*
|
|
288
|
-
* @returns {object}
|
|
326
|
+
* 获取所有选项值
|
|
327
|
+
* @returns {object} 所有选项值
|
|
289
328
|
*/
|
|
290
329
|
Drive.prototype.getOptions = function () {
|
|
291
330
|
return this._options || {};
|
|
292
331
|
};
|
|
293
332
|
|
|
294
333
|
/**
|
|
295
|
-
*
|
|
334
|
+
* 获取选项值
|
|
296
335
|
* @param {string} key 选项键
|
|
297
336
|
* @returns {*} 选项值
|
|
298
337
|
*/
|
|
@@ -302,17 +341,17 @@ Drive.prototype.getOption = function (key) {
|
|
|
302
341
|
};
|
|
303
342
|
|
|
304
343
|
/**
|
|
305
|
-
*
|
|
306
|
-
* @param {object} options
|
|
344
|
+
* 设置所有选项值
|
|
345
|
+
* @param {object} options 选项值对象
|
|
307
346
|
*/
|
|
308
347
|
Drive.prototype.setOptions = function (options) {
|
|
309
|
-
if (!options) return;
|
|
310
|
-
|
|
348
|
+
if (!this._hasOptions() || !options) return;
|
|
349
|
+
push(this._options, options || {});
|
|
311
350
|
this.setOptionsToFile();
|
|
312
351
|
};
|
|
313
352
|
|
|
314
353
|
/**
|
|
315
|
-
*
|
|
354
|
+
* 设置选项值并保存到缓存文件
|
|
316
355
|
* @param {string} key 选项键
|
|
317
356
|
* @param {*} value 选项值
|
|
318
357
|
*/
|
|
@@ -320,6 +359,7 @@ Drive.prototype.setOption = function (key, value) {
|
|
|
320
359
|
if (typeof key !== 'string') {
|
|
321
360
|
throw new TypeError('key必须是字符串');
|
|
322
361
|
}
|
|
362
|
+
if (!this._hasOptions()) return;
|
|
323
363
|
if (!this._options) {
|
|
324
364
|
this._options = {};
|
|
325
365
|
}
|
|
@@ -327,12 +367,16 @@ Drive.prototype.setOption = function (key, value) {
|
|
|
327
367
|
this.setOptionsToFile();
|
|
328
368
|
};
|
|
329
369
|
|
|
370
|
+
/**
|
|
371
|
+
* 将选项值保存到缓存文件
|
|
372
|
+
*/
|
|
330
373
|
Drive.prototype.setOptionsToFile = function () {
|
|
374
|
+
if (!this._hasOptions()) return;
|
|
331
375
|
let cache_file = this._getOptionsFile();
|
|
332
376
|
if (cache_file) {
|
|
333
377
|
// 确保目录存在
|
|
334
|
-
|
|
335
|
-
|
|
378
|
+
addDir(cache_file);
|
|
379
|
+
saveJson(cache_file, this._options);
|
|
336
380
|
}
|
|
337
381
|
};
|
|
338
382
|
|
|
@@ -344,17 +388,17 @@ Drive.prototype.getDir = function () {
|
|
|
344
388
|
let dir = this._dir;
|
|
345
389
|
if (!dir) {
|
|
346
390
|
if (this.script_file) {
|
|
347
|
-
dir = this.script_file
|
|
391
|
+
dir = dirname(this.script_file);
|
|
348
392
|
this._dir = dir;
|
|
349
393
|
} else if (this.config_file) {
|
|
350
|
-
dir = this.config_file
|
|
394
|
+
dir = dirname(this.config_file);
|
|
351
395
|
this._dir = dir;
|
|
352
396
|
} else {
|
|
353
397
|
let parent = this.getParent();
|
|
354
398
|
if (parent) {
|
|
355
399
|
let parent_dir = parent.getDir() || '';
|
|
356
400
|
if (parent_dir) {
|
|
357
|
-
dir = ('./' + this.config.name
|
|
401
|
+
dir = fullname('./' + this.config.name, parent_dir);
|
|
358
402
|
}
|
|
359
403
|
}
|
|
360
404
|
}
|
|
@@ -379,7 +423,7 @@ Drive.prototype._remove = function () {
|
|
|
379
423
|
|
|
380
424
|
try {
|
|
381
425
|
if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
|
|
382
|
-
|
|
426
|
+
getHotReload().unload(file);
|
|
383
427
|
} else {
|
|
384
428
|
// 移除模块缓存
|
|
385
429
|
let file_name = require.resolve(file);
|
|
@@ -430,8 +474,8 @@ Drive.prototype._getScriptFile = function () {
|
|
|
430
474
|
let main = this.config.main || '';
|
|
431
475
|
if (!main) return null;
|
|
432
476
|
|
|
433
|
-
let filename =
|
|
434
|
-
if (!
|
|
477
|
+
let filename = fullname(main, this.getDir());
|
|
478
|
+
if (!hasFile(filename)) {
|
|
435
479
|
this.newScript(filename);
|
|
436
480
|
}
|
|
437
481
|
return filename;
|
|
@@ -446,7 +490,7 @@ Drive.prototype.newScript = function (file) {
|
|
|
446
490
|
};
|
|
447
491
|
|
|
448
492
|
Drive.prototype.renderTpl = function (tpl, model) {
|
|
449
|
-
return
|
|
493
|
+
return getTpl().render(tpl, model);
|
|
450
494
|
};
|
|
451
495
|
|
|
452
496
|
/**
|
|
@@ -461,7 +505,7 @@ Drive.prototype._createScriptFile = function (file) {
|
|
|
461
505
|
return '';
|
|
462
506
|
}
|
|
463
507
|
let code = this.prettyCode(content, 'js');
|
|
464
|
-
|
|
508
|
+
saveText(file, code);
|
|
465
509
|
return file;
|
|
466
510
|
};
|
|
467
511
|
|
|
@@ -470,9 +514,9 @@ Drive.prototype._createScriptFile = function (file) {
|
|
|
470
514
|
* @returns {string} 脚本模板内容
|
|
471
515
|
*/
|
|
472
516
|
Drive.prototype._getScriptTpl = function () {
|
|
473
|
-
let f = './script.tpl.js'
|
|
474
|
-
if (
|
|
475
|
-
return
|
|
517
|
+
let f = fullname('./script.tpl.js', this._getTplDir());
|
|
518
|
+
if (hasFile(f)) {
|
|
519
|
+
return loadText(f);
|
|
476
520
|
} else {
|
|
477
521
|
return `module.exports = {
|
|
478
522
|
/**
|
|
@@ -511,7 +555,7 @@ Drive.prototype._getScriptTpl = function () {
|
|
|
511
555
|
|
|
512
556
|
/**
|
|
513
557
|
* 加载脚本文件,支持热重载模式
|
|
514
|
-
* 在开发模式(mode=3或4)
|
|
558
|
+
* 在开发模式(mode=3或4)下使用热重载管理器进行热重载
|
|
515
559
|
* 在生产模式下使用标准的 require 加载
|
|
516
560
|
* @returns {object | null} 加载的模块对象
|
|
517
561
|
*/
|
|
@@ -520,7 +564,7 @@ Drive.prototype.loadScript = function () {
|
|
|
520
564
|
if (!file) return null;
|
|
521
565
|
try {
|
|
522
566
|
if (this.mode === MODE.HOT_RELOAD || this.mode === MODE.HOT_BOTH) {
|
|
523
|
-
let cs =
|
|
567
|
+
let cs = getHotReload().load(file, (mod) => {
|
|
524
568
|
this._setMainMethod(mod);
|
|
525
569
|
});
|
|
526
570
|
this._setMainMethod(cs);
|
|
@@ -563,7 +607,7 @@ Drive.prototype._setMainMethod = function (mod) {
|
|
|
563
607
|
* @returns {string} 配置文件作用域
|
|
564
608
|
*/
|
|
565
609
|
Drive.prototype._getScope = function (file) {
|
|
566
|
-
return file.replace(/\\/g, '/')
|
|
610
|
+
return string.between(file.replace(/\\/g, '/'), 'app/', '/');
|
|
567
611
|
};
|
|
568
612
|
|
|
569
613
|
/**
|
|
@@ -626,7 +670,7 @@ Drive.prototype._createConfigFile = function (file, model) {
|
|
|
626
670
|
let tpl = this._getConfigTpl();
|
|
627
671
|
let content = this.renderTpl(tpl, model || this.getModel('config'));
|
|
628
672
|
let code = this.prettyCode(content, 'json');
|
|
629
|
-
|
|
673
|
+
saveText(file, code);
|
|
630
674
|
return file;
|
|
631
675
|
};
|
|
632
676
|
|
|
@@ -637,7 +681,7 @@ Drive.prototype._createConfigFile = function (file, model) {
|
|
|
637
681
|
* @returns {string} 配置文件内容
|
|
638
682
|
*/
|
|
639
683
|
Drive.prototype.createConfigFile = function (file, model) {
|
|
640
|
-
|
|
684
|
+
addDir(file);
|
|
641
685
|
return this._createConfigFile(file, model);
|
|
642
686
|
};
|
|
643
687
|
|
|
@@ -646,9 +690,9 @@ Drive.prototype.createConfigFile = function (file, model) {
|
|
|
646
690
|
* @returns {string} 配置模板内容
|
|
647
691
|
*/
|
|
648
692
|
Drive.prototype._getConfigTpl = function () {
|
|
649
|
-
let f = './config.tpl.json'
|
|
650
|
-
if (
|
|
651
|
-
return
|
|
693
|
+
let f = fullname('./config.tpl.json', this._getTplDir());
|
|
694
|
+
if (hasFile(f)) {
|
|
695
|
+
return loadText(f);
|
|
652
696
|
} else {
|
|
653
697
|
return `{
|
|
654
698
|
// 模块名称,必填
|
|
@@ -679,13 +723,13 @@ Drive.prototype._getConfigTpl = function () {
|
|
|
679
723
|
*/
|
|
680
724
|
Drive.prototype.loadFile = function (file, name) {
|
|
681
725
|
try {
|
|
682
|
-
let filename =
|
|
683
|
-
let text =
|
|
726
|
+
let filename = fullname(file, this.getDir());
|
|
727
|
+
let text = loadText(filename);
|
|
684
728
|
|
|
685
729
|
// 如果文件不存在,创建新的配置文件
|
|
686
730
|
if (!text) {
|
|
687
731
|
this.newConfig(filename);
|
|
688
|
-
text =
|
|
732
|
+
text = loadText(filename);
|
|
689
733
|
}
|
|
690
734
|
|
|
691
735
|
if (!text) return null;
|
|
@@ -700,15 +744,18 @@ Drive.prototype.loadFile = function (file, name) {
|
|
|
700
744
|
};
|
|
701
745
|
|
|
702
746
|
/**
|
|
703
|
-
*
|
|
747
|
+
* 加载配置文件(JSON5 语法),热更新模式下同时注册文件监听
|
|
704
748
|
* @param {string} file 完整文件路径
|
|
705
|
-
* @returns {object | Array | null} 配置对象
|
|
749
|
+
* @returns {object | Array | null | undefined} 配置对象
|
|
706
750
|
*/
|
|
707
751
|
Drive.prototype._loadHotReload = function (file) {
|
|
752
|
+
if (!file) return null;
|
|
708
753
|
if (this.mode >= MODE.HOT_CONFIG) {
|
|
709
|
-
return
|
|
754
|
+
return getHotReload().load(file, (config) => {
|
|
755
|
+
this._handleHotReload(config);
|
|
756
|
+
});
|
|
710
757
|
}
|
|
711
|
-
return
|
|
758
|
+
return loadJson(file);
|
|
712
759
|
};
|
|
713
760
|
|
|
714
761
|
/**
|
|
@@ -764,8 +811,8 @@ Drive.prototype._reloadIfNeeded = function (old_script) {
|
|
|
764
811
|
*/
|
|
765
812
|
Drive.prototype.delDir = function () {
|
|
766
813
|
let main = this.config.main;
|
|
767
|
-
if (main
|
|
768
|
-
|
|
814
|
+
if (main) {
|
|
815
|
+
delDir(this.getDir());
|
|
769
816
|
}
|
|
770
817
|
};
|
|
771
818
|
|
|
@@ -781,15 +828,15 @@ Drive.prototype.delFile = function () {
|
|
|
781
828
|
let error_message = null;
|
|
782
829
|
try {
|
|
783
830
|
// 删除配置文件(JSON)
|
|
784
|
-
if (cfg_file &&
|
|
785
|
-
if (
|
|
831
|
+
if (cfg_file && hasFile(cfg_file)) {
|
|
832
|
+
if (loadText(cfg_file)) {
|
|
786
833
|
error_message = this._delConfigFile(cfg_file, name);
|
|
787
834
|
}
|
|
788
835
|
}
|
|
789
836
|
// 删除脚本文件(JS,不尝试 JSON 解析)
|
|
790
|
-
if (script_file &&
|
|
837
|
+
if (script_file && hasFile(script_file)) {
|
|
791
838
|
this._remove();
|
|
792
|
-
|
|
839
|
+
delFile(script_file);
|
|
793
840
|
}
|
|
794
841
|
} catch (err) {
|
|
795
842
|
this.log('error', `删除文件失败`, err);
|
|
@@ -805,13 +852,13 @@ Drive.prototype.delFile = function () {
|
|
|
805
852
|
* @returns {string | null} 错误消息
|
|
806
853
|
*/
|
|
807
854
|
Drive.prototype._delConfigFile = function (file, name) {
|
|
808
|
-
let text =
|
|
855
|
+
let text = loadText(file);
|
|
809
856
|
if (!text) {
|
|
810
857
|
this.delDir();
|
|
811
858
|
return null;
|
|
812
859
|
}
|
|
813
860
|
|
|
814
|
-
let config =
|
|
861
|
+
let config = loadJson(file);
|
|
815
862
|
if (Array.isArray(config)) {
|
|
816
863
|
return this._delArray(file, config, name);
|
|
817
864
|
}
|
|
@@ -835,9 +882,9 @@ Drive.prototype._delArray = function (file, config, name) {
|
|
|
835
882
|
config.splice(index, 1);
|
|
836
883
|
|
|
837
884
|
if (config.length > 0) {
|
|
838
|
-
|
|
839
|
-
} else if (file
|
|
840
|
-
|
|
885
|
+
saveText(file, JSON.stringify(config, null, 2));
|
|
886
|
+
} else if (hasFile(file)) {
|
|
887
|
+
delFile(file);
|
|
841
888
|
}
|
|
842
889
|
|
|
843
890
|
return null;
|
|
@@ -849,7 +896,7 @@ Drive.prototype._delArray = function (file, config, name) {
|
|
|
849
896
|
* @returns {string} 配置文件名称
|
|
850
897
|
*/
|
|
851
898
|
Drive.prototype._getName = function (file) {
|
|
852
|
-
let name =
|
|
899
|
+
let name = basename(dirname(file));
|
|
853
900
|
return name;
|
|
854
901
|
// // 提取文件名作为模块名
|
|
855
902
|
// let word = new Word();
|
|
@@ -885,11 +932,11 @@ Drive.prototype.save = function () {
|
|
|
885
932
|
if (!this.config_file) return;
|
|
886
933
|
|
|
887
934
|
let file = this.config_file;
|
|
888
|
-
let text =
|
|
935
|
+
let text = loadText(file);
|
|
889
936
|
|
|
890
937
|
if (text && text.trim().startsWith('[')) {
|
|
891
938
|
// 数组格式配置
|
|
892
|
-
let config =
|
|
939
|
+
let config = loadJson(file);
|
|
893
940
|
if (Array.isArray(config)) {
|
|
894
941
|
// 查找并更新现有配置项
|
|
895
942
|
let index = config.findIndex((item) => item.name === this.config.name);
|
|
@@ -899,13 +946,13 @@ Drive.prototype.save = function () {
|
|
|
899
946
|
// 如果不存在,添加新配置项
|
|
900
947
|
config.push(this.config);
|
|
901
948
|
}
|
|
902
|
-
|
|
949
|
+
saveText(file, JSON.stringify(config, null, 2));
|
|
903
950
|
return;
|
|
904
951
|
}
|
|
905
952
|
}
|
|
906
953
|
|
|
907
954
|
// 单对象格式配置,直接保存
|
|
908
|
-
|
|
955
|
+
saveText(file, JSON.stringify(this.config, null, 2));
|
|
909
956
|
} catch (err) {
|
|
910
957
|
this.log('error', `保存配置失败`, err);
|
|
911
958
|
}
|
|
@@ -917,7 +964,7 @@ Drive.prototype.save = function () {
|
|
|
917
964
|
Drive.prototype.remove = function () {
|
|
918
965
|
let dir = this.getDir();
|
|
919
966
|
if (dir) {
|
|
920
|
-
|
|
967
|
+
delDir(dir);
|
|
921
968
|
}
|
|
922
969
|
return dir;
|
|
923
970
|
};
|