mm_options 1.0.2 → 1.1.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/index.js +7 -5
- package/package.json +3 -3
- package/tool/option/index.js +67 -4
- package/tool/option_admin/index.js +36 -0
- package/tool/option_admin/tool.json +11 -0
- package/lib/tools/index.js +0 -16
- package/lib/tools/option/index.js +0 -70
package/index.js
CHANGED
|
@@ -14,7 +14,13 @@ module.exports = {
|
|
|
14
14
|
* @returns {Options} 动态配置实例
|
|
15
15
|
*/
|
|
16
16
|
optionsAdmin(scope, config) {
|
|
17
|
-
|
|
17
|
+
const name = scope || 'os';
|
|
18
|
+
const existed = admin.has(name);
|
|
19
|
+
const options = admin.get(name, config);
|
|
20
|
+
if (!existed) {
|
|
21
|
+
options.restore();
|
|
22
|
+
}
|
|
23
|
+
return options;
|
|
18
24
|
},
|
|
19
25
|
/**
|
|
20
26
|
* 获取缺省动态配置实例
|
|
@@ -24,7 +30,3 @@ module.exports = {
|
|
|
24
30
|
return admin.get('os');
|
|
25
31
|
}
|
|
26
32
|
};
|
|
27
|
-
|
|
28
|
-
/* mm-tool-logic-export 工具能力层:其它模块可 require("mm_options").tools.<name> 直接引用 */
|
|
29
|
-
Object.defineProperty(module.exports, 'tools', { enumerable: true, configurable: true,
|
|
30
|
-
get: function () { return require('./lib/tools'); } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_options",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "动态配置存储模块:适用于站长/用户配置,可在运行时动态修改",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"repository": {
|
|
@@ -15,8 +15,8 @@
|
|
|
15
15
|
"配置",
|
|
16
16
|
"存储"
|
|
17
17
|
],
|
|
18
|
-
"layer": "
|
|
19
|
-
"group": "
|
|
18
|
+
"layer": "store",
|
|
19
|
+
"group": "data",
|
|
20
20
|
"author": "邱文武",
|
|
21
21
|
"license": "ISC",
|
|
22
22
|
"scripts": {
|
package/tool/option/index.js
CHANGED
|
@@ -1,7 +1,70 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
/**
|
|
3
|
-
* option ——
|
|
4
|
-
*
|
|
5
|
-
* 其它模块可直接 require('mm_options').tools.option 复用。
|
|
3
|
+
* option —— 动态配置存取工具
|
|
4
|
+
* 复用模块 Options 实例的读写与落盘能力。
|
|
6
5
|
*/
|
|
7
|
-
|
|
6
|
+
const { optionsAdmin } = require('../../index.js');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 落盘:未配置持久化文件时只保留内存结果
|
|
10
|
+
* @param {object} store 存储实例
|
|
11
|
+
* @returns {boolean} 是否已落盘
|
|
12
|
+
*/
|
|
13
|
+
function _save(store) {
|
|
14
|
+
const file = (store.config && store.config.file) || '';
|
|
15
|
+
if (!file) return false;
|
|
16
|
+
store.save();
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function main(params, ctx, options) {
|
|
21
|
+
void options;
|
|
22
|
+
const p = params || {};
|
|
23
|
+
const action = p.action || 'list';
|
|
24
|
+
const done = (data) => { ctx.output = data; return data; };
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const store = optionsAdmin(p.scope || 'os');
|
|
28
|
+
|
|
29
|
+
if (action === 'set') {
|
|
30
|
+
if (!p.key) throw new TypeError('key 不能为空');
|
|
31
|
+
store.set(p.key, p.value);
|
|
32
|
+
_save(store);
|
|
33
|
+
return done({ code: 0, msg: '写入成功', key: p.key });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (action === 'get') {
|
|
37
|
+
if (!p.key) throw new TypeError('key 不能为空');
|
|
38
|
+
return done({ code: 0, msg: '成功', key: p.key, value: store.get(p.key), exists: store.has(p.key) });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (action === 'del') {
|
|
42
|
+
if (!p.key) throw new TypeError('key 不能为空');
|
|
43
|
+
const deleted = store.del(p.key);
|
|
44
|
+
_save(store);
|
|
45
|
+
return done({ code: 0, msg: deleted ? '删除成功' : '配置不存在', key: p.key, deleted: !!deleted });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (action === 'list') {
|
|
49
|
+
const list = store.list();
|
|
50
|
+
return done({ code: 0, msg: '成功', list, count: Object.keys(list || {}).length });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (action === 'clear') {
|
|
54
|
+
store.clear();
|
|
55
|
+
_save(store);
|
|
56
|
+
return done({ code: 0, msg: '已清空' });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (action === 'save') {
|
|
60
|
+
_save(store);
|
|
61
|
+
return done({ code: 0, msg: '已落盘', ok: true });
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = { main };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const { admin } = require('../../index.js');
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 管理选项存储实例池
|
|
6
|
+
* @param {object} params 参数:action/scope
|
|
7
|
+
* @param {object} ctx 执行上下文
|
|
8
|
+
* @returns {Promise<object>} 执行结果
|
|
9
|
+
*/
|
|
10
|
+
async function main(params, ctx) {
|
|
11
|
+
const p = params || {};
|
|
12
|
+
const action = p.action || 'list';
|
|
13
|
+
const scope = p.scope || 'os';
|
|
14
|
+
const done = (data) => {
|
|
15
|
+
if (ctx) ctx.output = data;
|
|
16
|
+
return data;
|
|
17
|
+
};
|
|
18
|
+
try {
|
|
19
|
+
if (action === 'list') {
|
|
20
|
+
const scopes = [...admin.keys()];
|
|
21
|
+
return done({ code: 0, msg: '获取成功', action, count: scopes.length, scopes });
|
|
22
|
+
}
|
|
23
|
+
if (action === 'inspect') {
|
|
24
|
+
const exists = admin.has(scope);
|
|
25
|
+
const instance = exists ? admin.get(scope) : null;
|
|
26
|
+
return done({ code: 0, msg: exists ? '获取成功' : '实例不存在', action, scope, exists, config: instance && instance.config ? instance.config : null });
|
|
27
|
+
}
|
|
28
|
+
if (action === 'del') return done({ code: 0, msg: '删除成功', action, scope, ok: admin.del(scope) });
|
|
29
|
+
if (action === 'clear') { admin.clear(); return done({ code: 0, msg: '实例池已清空', action }); }
|
|
30
|
+
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
31
|
+
} catch (error) {
|
|
32
|
+
return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = { main };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "option_admin",
|
|
3
|
+
"title": "选项存储实例管理",
|
|
4
|
+
"description": "管理 mm_options 的实例池:list 列出实例、inspect 查看配置、del 删除实例、clear 清空实例池。",
|
|
5
|
+
"category": "选项存储",
|
|
6
|
+
"group": "state",
|
|
7
|
+
"type": "function",
|
|
8
|
+
"main": "./index.js",
|
|
9
|
+
"params": { "type": "object", "properties": { "action": { "type": "string", "description": "操作类型: list/inspect/del/clear", "default": "list" }, "scope": { "type": "string", "description": "实例作用域,缺省 os", "default": "os" } }, "required": ["action"] },
|
|
10
|
+
"handler": "manager"
|
|
11
|
+
}
|
package/lib/tools/index.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const fs = require('fs');
|
|
3
|
-
const path = require('path');
|
|
4
|
-
/** 模块能力层:懒加载工具实现,避免模块入口被 require 时加载全部实现 */
|
|
5
|
-
module.exports = new Proxy({}, {
|
|
6
|
-
get(_t, key) {
|
|
7
|
-
if (typeof key !== 'string') return undefined;
|
|
8
|
-
if (!fs.existsSync(path.join(__dirname, key, 'index.js'))) return undefined;
|
|
9
|
-
return require('./' + key);
|
|
10
|
-
},
|
|
11
|
-
has(_t, key) {
|
|
12
|
-
return typeof key === 'string' && fs.existsSync(path.join(__dirname, key, 'index.js'));
|
|
13
|
-
},
|
|
14
|
-
ownKeys() { return fs.readdirSync(__dirname, { withFileTypes: true }).filter(e => e.isDirectory()).map(e => e.name); },
|
|
15
|
-
getOwnPropertyDescriptor(_t, key) { return this.has(_t, key) ? { enumerable: true, configurable: true } : undefined; }
|
|
16
|
-
});
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
/**
|
|
3
|
-
* option —— 动态配置存取工具
|
|
4
|
-
* 复用模块 Options 实例的读写与落盘能力。
|
|
5
|
-
*/
|
|
6
|
-
const { optionsAdmin } = require('../../../index.js');
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* 落盘:未配置持久化文件时只保留内存结果
|
|
10
|
-
* @param {object} store 存储实例
|
|
11
|
-
* @returns {boolean} 是否已落盘
|
|
12
|
-
*/
|
|
13
|
-
function _save(store) {
|
|
14
|
-
const file = (store.config && store.config.file) || '';
|
|
15
|
-
if (!file) return false;
|
|
16
|
-
store.save();
|
|
17
|
-
return true;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
async function main(params, ctx, options) {
|
|
21
|
-
void options;
|
|
22
|
-
const p = params || {};
|
|
23
|
-
const action = p.action || 'list';
|
|
24
|
-
const done = (data) => { ctx.output = data; return data; };
|
|
25
|
-
|
|
26
|
-
try {
|
|
27
|
-
const store = optionsAdmin(p.scope || 'os');
|
|
28
|
-
|
|
29
|
-
if (action === 'set') {
|
|
30
|
-
if (!p.key) throw new TypeError('key 不能为空');
|
|
31
|
-
store.set(p.key, p.value);
|
|
32
|
-
_save(store);
|
|
33
|
-
return done({ code: 0, msg: '写入成功', key: p.key });
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (action === 'get') {
|
|
37
|
-
if (!p.key) throw new TypeError('key 不能为空');
|
|
38
|
-
return done({ code: 0, msg: '成功', key: p.key, value: store.get(p.key), exists: store.has(p.key) });
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (action === 'del') {
|
|
42
|
-
if (!p.key) throw new TypeError('key 不能为空');
|
|
43
|
-
const deleted = store.del(p.key);
|
|
44
|
-
_save(store);
|
|
45
|
-
return done({ code: 0, msg: deleted ? '删除成功' : '配置不存在', key: p.key, deleted: !!deleted });
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (action === 'list') {
|
|
49
|
-
const list = store.list();
|
|
50
|
-
return done({ code: 0, msg: '成功', list, count: Object.keys(list || {}).length });
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (action === 'clear') {
|
|
54
|
-
store.clear();
|
|
55
|
-
_save(store);
|
|
56
|
-
return done({ code: 0, msg: '已清空' });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (action === 'save') {
|
|
60
|
-
_save(store);
|
|
61
|
-
return done({ code: 0, msg: '已落盘', ok: true });
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return done({ code: 10001, msg: '不支持的操作: ' + action });
|
|
65
|
-
} catch (error) {
|
|
66
|
-
return done({ code: 10000, msg: '执行失败:' + error.message, error: { name: error.name, message: error.message } });
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
module.exports = { main };
|