mm_config 2.5.2 → 2.8.0
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 +23 -41
- package/index.js +18 -331
- package/lib/conf.js +380 -0
- package/lib/config.js +34 -0
- package/lib/run_path_logic.js +134 -0
- package/lib/tools/config/index.js +100 -0
- package/lib/tools/get_run_path/index.js +7 -0
- package/lib/tools/index.js +16 -0
- package/lib/tools/path/index.js +147 -0
- package/package.json +63 -50
- package/tool/config/index.js +7 -0
- package/tool/config/tool.json +80 -0
- package/tool/get_run_path/index.js +7 -0
- package/tool/get_run_path/tool.json +49 -0
- package/tool/path/index.js +7 -0
- package/tool/path/tool.json +99 -0
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
# mm_config
|
|
2
|
-
|
|
1
|
+
# 配置管理器(mm_config)
|
|
2
|
+
## 说明
|
|
3
3
|
超级美眉配置帮助类,动态同步文件修改配置。
|
|
4
4
|
|
|
5
5
|
## 功能特点
|
|
@@ -206,58 +206,40 @@ config.description = "This supports JSON5 format";
|
|
|
206
206
|
config.version = "1.0.0";
|
|
207
207
|
```
|
|
208
208
|
|
|
209
|
-
### 5. 直接使用Config
|
|
209
|
+
### 5. 直接使用Config类(键值存储)
|
|
210
210
|
|
|
211
211
|
```javascript
|
|
212
212
|
const { Config } = require('mm_config');
|
|
213
213
|
|
|
214
|
-
//
|
|
215
|
-
const cfg = new Config({
|
|
216
|
-
|
|
217
|
-
pretty: true
|
|
218
|
-
}, {
|
|
219
|
-
name: "directDemo"
|
|
220
|
-
});
|
|
214
|
+
// 创建配置实例(固定配置,每个键只能设置一次)
|
|
215
|
+
const cfg = new Config({ file: './config.json' });
|
|
216
|
+
cfg.set('name', 'demo').set('state', 1);
|
|
221
217
|
|
|
222
|
-
//
|
|
223
|
-
|
|
218
|
+
// 读取配置
|
|
219
|
+
console.log('get(name):', cfg.get('name')); // demo
|
|
220
|
+
console.log('has(state):', cfg.has('state')); // true
|
|
221
|
+
console.log('配置列表:', cfg.list());
|
|
224
222
|
|
|
225
|
-
//
|
|
226
|
-
|
|
223
|
+
// 保存到文件
|
|
224
|
+
cfg.save();
|
|
227
225
|
|
|
228
|
-
//
|
|
229
|
-
|
|
230
|
-
|
|
226
|
+
// 从文件重新加载(不覆盖已存在的键)
|
|
227
|
+
const loaded = new Config({ file: './config.json' });
|
|
228
|
+
loaded.load();
|
|
229
|
+
console.log('重新加载后 name:', loaded.get('name'));
|
|
231
230
|
```
|
|
232
231
|
|
|
233
|
-
### 6.
|
|
232
|
+
### 6. 使用configAdmin获取实例
|
|
234
233
|
|
|
235
234
|
```javascript
|
|
236
|
-
const {
|
|
235
|
+
const { configAdmin } = require('mm_config');
|
|
237
236
|
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
// 设置配置值
|
|
242
|
-
await cfg.set("name", "test");
|
|
243
|
-
|
|
244
|
-
// 获取配置值
|
|
245
|
-
const name = await cfg.get("name");
|
|
246
|
-
console.log('获取到的名称:', name); // "test"
|
|
247
|
-
|
|
248
|
-
// 检查配置是否存在
|
|
249
|
-
const hasName = await cfg.has("name");
|
|
250
|
-
console.log('是否存在名称:', hasName); // true
|
|
251
|
-
|
|
252
|
-
// 删除配置项
|
|
253
|
-
await cfg.del("name");
|
|
254
|
-
|
|
255
|
-
// 获取所有键
|
|
256
|
-
const keys = await cfg.keys();
|
|
257
|
-
console.log('所有键:', keys);
|
|
258
|
-
}
|
|
237
|
+
// 通过管理器按作用域获取实例(相同作用域返回同一实例)
|
|
238
|
+
const scoped = configAdmin('app', { file: './app_config.json' });
|
|
239
|
+
scoped.set('version', '1.0.0');
|
|
259
240
|
|
|
260
|
-
|
|
241
|
+
// 读取作用域配置
|
|
242
|
+
console.log('get(version):', scoped.get('version')); // 1.0.0
|
|
261
243
|
```
|
|
262
244
|
|
|
263
245
|
### 7. 缓存管理功能
|
package/index.js
CHANGED
|
@@ -1,335 +1,22 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const { reg } = require('mm_admin');
|
|
2
|
+
const Config = require('./lib/config.js');
|
|
3
|
+
const all = require('./lib/conf.js');
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* @type {object}
|
|
6
|
-
*/
|
|
7
|
-
const dict = {};
|
|
5
|
+
const admin = reg('config');
|
|
6
|
+
admin.setClass(Config);
|
|
8
7
|
|
|
9
|
-
/**
|
|
10
|
-
* 配置类
|
|
11
|
-
* @class
|
|
12
|
-
*/
|
|
13
|
-
class Config extends Base {
|
|
14
|
-
/**
|
|
15
|
-
* 配置选项
|
|
16
|
-
* @type {object}
|
|
17
|
-
*/
|
|
18
|
-
static config = {
|
|
19
|
-
debounce_time: 0, // 默认不防抖
|
|
20
|
-
pretty: true,
|
|
21
|
-
format: 'json',
|
|
22
|
-
file: ''
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* 预设选项
|
|
27
|
-
* @type {object}
|
|
28
|
-
*/
|
|
29
|
-
static options = {
|
|
30
|
-
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 创建配置实例
|
|
35
|
-
* @param {object} config - 配置选项
|
|
36
|
-
* @param {number} config.debounce_time - 防抖时间(ms)
|
|
37
|
-
* @param {boolean} config.pretty - 是否美化输出
|
|
38
|
-
* @param {string} config.format - 格式(json/json5)
|
|
39
|
-
* @param {string} config.file - 配置文件路径
|
|
40
|
-
* @param {object} options - 选项
|
|
41
|
-
*/
|
|
42
|
-
constructor(config, options = {}) {
|
|
43
|
-
super(config);
|
|
44
|
-
this.options = { ...Config.options, ...options };
|
|
45
|
-
this._promise = Promise.resolve();
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 设置配置
|
|
51
|
-
* @param {object} options - 配置对象
|
|
52
|
-
*/
|
|
53
|
-
Config.prototype.setOptions = function (options) {
|
|
54
|
-
$.push(this.options, options, true);
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 获取配置
|
|
59
|
-
* @returns {object} 配置对象
|
|
60
|
-
*/
|
|
61
|
-
Config.prototype.getOptions = function () {
|
|
62
|
-
return this.options;
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* 保存配置到文件(同步)
|
|
67
|
-
* @returns {boolean} 保存是否成功
|
|
68
|
-
*/
|
|
69
|
-
Config.prototype.saveSync = function () {
|
|
70
|
-
if (!this.config.file) return false;
|
|
71
|
-
try {
|
|
72
|
-
this.config.file.saveJson(this.options);
|
|
73
|
-
return true;
|
|
74
|
-
} catch (err) {
|
|
75
|
-
this.log('error', `Error saving options to ${this.config.file}:`, err.message);
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* 保存配置到文件(异步)
|
|
82
|
-
* @returns {Promise<boolean>} 保存操作是否成功
|
|
83
|
-
*/
|
|
84
|
-
Config.prototype.saveAsync = async function () {
|
|
85
|
-
if (!this.config.file) return Promise.resolve(true);
|
|
86
|
-
|
|
87
|
-
// 确保之前的写操作完成
|
|
88
|
-
await this._promise;
|
|
89
|
-
|
|
90
|
-
this._promise = Promise.resolve()
|
|
91
|
-
.then(() => {
|
|
92
|
-
this.config.file.saveJson(this.options);
|
|
93
|
-
return true;
|
|
94
|
-
})
|
|
95
|
-
.catch(err => {
|
|
96
|
-
this.log('error', `Error saving options to ${this.config.file}:`, err.message);
|
|
97
|
-
return false;
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
return this._promise;
|
|
101
|
-
};
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* 获取代理对象
|
|
105
|
-
* @returns {Proxy} 配置对象的代理实例
|
|
106
|
-
*/
|
|
107
|
-
Config.prototype.getProxy = function () {
|
|
108
|
-
let self = this;
|
|
109
|
-
|
|
110
|
-
return new Proxy(this.options, {
|
|
111
|
-
set(obj, prop, val) {
|
|
112
|
-
obj[prop] = val;
|
|
113
|
-
|
|
114
|
-
if (self.config.debounce_time > 0) {
|
|
115
|
-
// 防抖处理
|
|
116
|
-
if (self._timer) {
|
|
117
|
-
clearTimeout(self._timer);
|
|
118
|
-
}
|
|
119
|
-
self._timer = setTimeout(() => {
|
|
120
|
-
self.saveSync();
|
|
121
|
-
}, self.config.debounce_time);
|
|
122
|
-
} else {
|
|
123
|
-
// 无防抖,直接保存
|
|
124
|
-
self.saveSync();
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return true;
|
|
128
|
-
},
|
|
129
|
-
|
|
130
|
-
get(obj, prop) {
|
|
131
|
-
// 提供一些特殊属性访问
|
|
132
|
-
if (prop === '_saveAsync') {
|
|
133
|
-
return () => self.saveAsync();
|
|
134
|
-
}
|
|
135
|
-
if (prop === '_saveSync') {
|
|
136
|
-
return () => self.saveSync();
|
|
137
|
-
}
|
|
138
|
-
if (prop === '_raw') {
|
|
139
|
-
return obj;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return obj[prop];
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* 获取异步代理对象
|
|
149
|
-
* @returns {Proxy} 异步配置对象的代理实例
|
|
150
|
-
*/
|
|
151
|
-
Config.prototype.getAsyncProxy = function () {
|
|
152
|
-
let self = this;
|
|
153
|
-
|
|
154
|
-
return new Proxy(this.options, {
|
|
155
|
-
set(obj, prop, val) {
|
|
156
|
-
obj[prop] = val;
|
|
157
|
-
|
|
158
|
-
if (self.config.debounce_time > 0) {
|
|
159
|
-
if (self._timer) {
|
|
160
|
-
clearTimeout(self._timer);
|
|
161
|
-
}
|
|
162
|
-
self._timer = setTimeout(() => {
|
|
163
|
-
self.saveAsync();
|
|
164
|
-
}, self.config.debounce_time);
|
|
165
|
-
} else {
|
|
166
|
-
self.saveAsync();
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return true;
|
|
170
|
-
},
|
|
171
|
-
|
|
172
|
-
get(obj, prop) {
|
|
173
|
-
if (prop === '_saveAsync') {
|
|
174
|
-
return () => self.saveAsync();
|
|
175
|
-
}
|
|
176
|
-
if (prop === '_saveSync') {
|
|
177
|
-
return () => self.saveSync();
|
|
178
|
-
}
|
|
179
|
-
if (prop === '_raw') {
|
|
180
|
-
return obj;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
return obj[prop];
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* 获取所有配置项
|
|
190
|
-
* @returns {object} 所有配置项
|
|
191
|
-
*/
|
|
192
|
-
Config.prototype.all = function () {
|
|
193
|
-
return this.options;
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* 获取配置值
|
|
198
|
-
* @param {string} key - 配置键
|
|
199
|
-
* @returns {*} 配置值
|
|
200
|
-
*/
|
|
201
|
-
Config.prototype.get = function (key) {
|
|
202
|
-
return this.options[key];
|
|
203
|
-
};
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* 设置配置值
|
|
207
|
-
* @param {string} key - 配置键
|
|
208
|
-
* @param {*} value - 配置值
|
|
209
|
-
*/
|
|
210
|
-
Config.prototype.set = function (key, value) {
|
|
211
|
-
this.options[key] = value;
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* 检查配置是否存在
|
|
216
|
-
* @param {string} key - 配置键
|
|
217
|
-
* @returns {boolean} 配置项是否存在
|
|
218
|
-
*/
|
|
219
|
-
Config.prototype.has = async function (key) {
|
|
220
|
-
return this.options.hasOwnProperty(key);
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* 删除配置项
|
|
225
|
-
* @param {string} key - 配置键
|
|
226
|
-
*/
|
|
227
|
-
Config.prototype.del = async function (key) {
|
|
228
|
-
delete this.options[key];
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* 获取所有配置键
|
|
233
|
-
* @returns {Array<string>} 配置键列表
|
|
234
|
-
*/
|
|
235
|
-
Config.prototype.keys = async function () {
|
|
236
|
-
let keys = [];
|
|
237
|
-
let local_keys = Object.keys(this.options);
|
|
238
|
-
keys = keys.concat(local_keys);
|
|
239
|
-
return keys;
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* 销毁
|
|
244
|
-
*/
|
|
245
|
-
Config.prototype.destroy = function () {
|
|
246
|
-
delete dict[this.config.file];
|
|
247
|
-
};
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* 从配置文件加载配置
|
|
251
|
-
* @returns {boolean} 加载是否成功
|
|
252
|
-
*/
|
|
253
|
-
Config.prototype.load = function () {
|
|
254
|
-
if (!this.config.file) return false;
|
|
255
|
-
this.options = this.config.file.loadJson() || {};
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* 创建配置(同步)
|
|
260
|
-
* @param {string} file - 配置文件路径
|
|
261
|
-
* @param {object} options - 配置对象
|
|
262
|
-
* @param {object} config - 配置选项
|
|
263
|
-
* @returns {Proxy} 配置代理对象
|
|
264
|
-
*/
|
|
265
|
-
function create(file, options = {}, config = {}) {
|
|
266
|
-
let f = file.fullname();
|
|
267
|
-
let cfg = new Config({ file: f, ...config }, options);
|
|
268
|
-
if (!file.hasFile()) {
|
|
269
|
-
cfg.saveSync(); // 确保文件存在
|
|
270
|
-
}
|
|
271
|
-
// 注册配置字典
|
|
272
|
-
dict[f] = cfg;
|
|
273
|
-
return dict[f].getProxy();
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
/**
|
|
277
|
-
* 创建配置(异步)
|
|
278
|
-
* @param {string} file - 配置文件路径
|
|
279
|
-
* @param {object} options - 配置对象
|
|
280
|
-
* @param {object} config - 配置选项
|
|
281
|
-
* @returns {Promise<Proxy>} 异步配置代理对象
|
|
282
|
-
*/
|
|
283
|
-
async function createAsync(file, options = {}, config = {}) {
|
|
284
|
-
let f = file.fullname();
|
|
285
|
-
let cfg = new Config({ file: f, ...config }, options);
|
|
286
|
-
if (!file.hasFile()) {
|
|
287
|
-
await cfg.saveAsync(); // 确保文件存在
|
|
288
|
-
}
|
|
289
|
-
// 注册配置字典
|
|
290
|
-
dict[f] = cfg;
|
|
291
|
-
return dict[f].getAsyncProxy();
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/**
|
|
295
|
-
* 清理指定文件配置缓存
|
|
296
|
-
* @param {string} file - 配置文件路径
|
|
297
|
-
* @returns {void}
|
|
298
|
-
*/
|
|
299
|
-
function clear(file) {
|
|
300
|
-
if (!file) return clearAll();
|
|
301
|
-
let f = file.fullname();
|
|
302
|
-
if (dict[f]) {
|
|
303
|
-
delete dict[f];
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
/**
|
|
308
|
-
* 清理所有配置缓存
|
|
309
|
-
*/
|
|
310
|
-
function clearAll() {
|
|
311
|
-
Object.keys(dict).forEach(key => {
|
|
312
|
-
delete dict[key];
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
/**
|
|
317
|
-
* 获取配置代理对象
|
|
318
|
-
* @param {object} options - 配置对象
|
|
319
|
-
* @param {string} file - 配置文件路径
|
|
320
|
-
* @param {object} config - 配置选项
|
|
321
|
-
* @returns {Proxy} 配置代理对象
|
|
322
|
-
*/
|
|
323
|
-
function conf(options, file, config = {}) {
|
|
324
|
-
return create(file, options, config);
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
// 模块导出
|
|
328
8
|
module.exports = {
|
|
9
|
+
...all,
|
|
329
10
|
Config,
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
11
|
+
admin,
|
|
12
|
+
configAdmin(scope, config) {
|
|
13
|
+
return admin.get(scope || 'os', config);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/* mm-tool-logic-export 工具能力层:其它模块可 require("<pkg>").tools.<name> 直接引用 */
|
|
18
|
+
Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
|
|
19
|
+
get: function () { return require('./lib/tools'); } });
|
|
20
|
+
|
|
21
|
+
/* S1 能力下沉:业务实现可在模块之间直接引用(不经过工具注册面) */
|
|
22
|
+
Object.defineProperty(module.exports, 'runPathLogic', { enumerable: true, value: require('./lib/run_path_logic.js') });
|