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.
@@ -0,0 +1,147 @@
1
+ /**
2
+ * 路径查询器
3
+ * 获取应用运行路径、当前模块根路径、项目模块目录等关键路径,
4
+ * 支持相对路径转绝对路径与存在性判断
5
+ * 运行路径读取本包 configAdmin('os') 的 run_path 配置,缺省回落到 process.cwd()
6
+ * @param {object} params 步骤参数
7
+ * @param {object} ctx 上下文对象
8
+ * @param {object} options 模块配置
9
+ * @returns {object} 查询结果
10
+ */
11
+ const path = require('path');
12
+ const fs = require('fs');
13
+ const { configAdmin } = require('../../../index.js');
14
+
15
+ /**
16
+ * 获取应用运行路径
17
+ * @returns {Promise<string>} 应用运行路径
18
+ */
19
+ async function _getRunPath() {
20
+ var run_path = (await configAdmin('os').get('run_path')) || '';
21
+ return run_path || process.cwd();
22
+ }
23
+
24
+ /**
25
+ * 获取当前模块根路径
26
+ * 优先取模块 options.self_path 配置,缺省回落到应用运行路径
27
+ * @param {object} mod 模块实例(this)
28
+ * @returns {Promise<string>} 模块根路径
29
+ */
30
+ async function _getSelfPath(mod) {
31
+ var opts = (mod && typeof mod.getOptions === 'function' && mod.getOptions()) || {};
32
+ if (opts.self_path) return opts.self_path;
33
+ return await _getRunPath();
34
+ }
35
+
36
+ /**
37
+ * 获取应用运行路径下的子目录
38
+ * @param {string} sub 子路径
39
+ * @returns {Promise<string>} 子目录绝对路径
40
+ */
41
+ async function _getAppSubPath(sub) {
42
+ return path.join(await _getRunPath(), sub);
43
+ }
44
+
45
+ /**
46
+ * 相对路径转绝对路径,绝对路径原样返回
47
+ * base 为 self 时基于当前模块根路径解析,否则基于应用运行路径
48
+ * @param {string} target 目标路径
49
+ * @param {string} base 基准目录: run/self
50
+ * @param {string} self_path 当前模块根路径
51
+ * @returns {Promise<string>} 绝对路径
52
+ */
53
+ async function _getAbsolute(target, base, self_path) {
54
+ if (path.isAbsolute(target)) return target;
55
+ var root = base === 'self' ? self_path : await _getRunPath();
56
+ return path.resolve(root, target);
57
+ }
58
+
59
+ /**
60
+ * 判断路径是否存在
61
+ * @param {string} file_path 文件路径
62
+ * @returns {boolean} 是否存在
63
+ */
64
+ function _exists(file_path) {
65
+ try {
66
+ return fs.existsSync(file_path);
67
+ } catch (_e) {
68
+ void _e;
69
+ return false;
70
+ }
71
+ }
72
+
73
+ /**
74
+ * 获取全部关键路径
75
+ * @param {string} self_path 当前模块根路径
76
+ * @returns {Promise<object>} 路径集合对象
77
+ */
78
+ async function _getAllPaths(self_path) {
79
+ var run_path = await _getRunPath();
80
+ return {
81
+ run_path: run_path,
82
+ module_dir: path.join(run_path, 'common', 'mod'),
83
+ workflow_dir: path.join(run_path, 'common', 'workflow'),
84
+ script_dir: path.join(run_path, 'common', 'script'),
85
+ templates_dir: path.join(run_path, 'templates'),
86
+ data_dir: path.join(run_path, 'data'),
87
+ self_path: self_path,
88
+ self_module_dir: path.join(self_path, 'common', 'mod'),
89
+ self_templates_dir: path.join(self_path, 'templates')
90
+ };
91
+ }
92
+
93
+ async function main(params, ctx, options) {
94
+ var action = params.action || 'info';
95
+ var target = params.target || '';
96
+ var base = params.base || 'run';
97
+ var self_path = await _getSelfPath(this);
98
+
99
+ switch (action) {
100
+ case 'info':
101
+ ctx.output = await _getAllPaths(self_path);
102
+ break;
103
+ case 'run':
104
+ ctx.output = { path: await _getRunPath() };
105
+ break;
106
+ case 'module':
107
+ ctx.output = { path: await _getAppSubPath('common/mod') };
108
+ break;
109
+ case 'workflow':
110
+ ctx.output = { path: await _getAppSubPath('common/workflow') };
111
+ break;
112
+ case 'script':
113
+ ctx.output = { path: await _getAppSubPath('common/script') };
114
+ break;
115
+ case 'templates':
116
+ ctx.output = { path: await _getAppSubPath('templates') };
117
+ break;
118
+ case 'data':
119
+ ctx.output = { path: await _getAppSubPath('data') };
120
+ break;
121
+ case 'self':
122
+ ctx.output = { path: self_path };
123
+ break;
124
+ case 'self_module':
125
+ ctx.output = { path: path.join(self_path, 'common', 'mod') };
126
+ break;
127
+ case 'self_templates':
128
+ ctx.output = { path: path.join(self_path, 'templates') };
129
+ break;
130
+ case 'absolute': {
131
+ var abs_path = await _getAbsolute(target, base, self_path);
132
+ ctx.output = { path: abs_path, exists: _exists(abs_path) };
133
+ break;
134
+ }
135
+ case 'exists':
136
+ ctx.output = { obj: _exists(await _getAbsolute(target, base, self_path)) };
137
+ break;
138
+ default:
139
+ ctx.output = {
140
+ error: { code: 'QUERY_UNKNOWN_ACTION', message: '不支持的操作: ' + action }
141
+ };
142
+ }
143
+
144
+ return ctx.output;
145
+ }
146
+
147
+ module.exports = { main };
package/package.json CHANGED
@@ -1,50 +1,63 @@
1
- {
2
- "name": "mm_config",
3
- "version": "2.5.2",
4
- "description": "Super Meimei Configuration Synchronizer - Automatically sync configuration changes to JSON files",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "node test.js",
8
- "lint": "eslint index.js",
9
- "lint:fix": "eslint index.js --fix"
10
- },
11
- "repository": {
12
- "type": "git",
13
- "url": "git+https://gitee.com/qiuwenwu91/mm_config.git"
14
- },
15
- "keywords": [
16
- "config",
17
- "JSON",
18
- "file",
19
- "sync",
20
- "async",
21
- "debounce",
22
- "json5",
23
- "configuration",
24
- "settings",
25
- "automation"
26
- ],
27
- "author": "邱文武",
28
- "license": "ISC",
29
- "bugs": {
30
- "url": "https://gitee.com/qiuwenwu91/mm_config/issues"
31
- },
32
- "homepage": "https://gitee.com/qiuwenwu91/mm_config#readme",
33
- "engines": {
34
- "node": ">=14.0.0"
35
- },
36
- "files": [
37
- "index.js",
38
- "README.md",
39
- "README_EN.md",
40
- "LICENSE"
41
- ],
42
- "dependencies": {
43
- "mm_expand": "^2.5.4"
44
- },
45
- "devDependencies": {
46
- "eslint": "^10.9.1",
47
- "eslint-plugin-jsdoc": "^64.2.1",
48
- "mm_eslint": "^1.7.6"
49
- }
50
- }
1
+ {
2
+ "name": "mm_config",
3
+ "version": "2.8.0",
4
+ "description": "Super Meimei Configuration Synchronizer - Automatically sync configuration changes to JSON files",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "node test.js",
8
+ "lint": "eslint index.js",
9
+ "lint:fix": "eslint index.js --fix"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://gitee.com/qiuwenwu91/mm_config.git"
14
+ },
15
+ "keywords": [
16
+ "mm_mod",
17
+ "mm_infra",
18
+ "mm_config",
19
+ "config",
20
+ "JSON",
21
+ "file",
22
+ "sync",
23
+ "async",
24
+ "debounce",
25
+ "json5",
26
+ "configuration",
27
+ "settings",
28
+ "automation"
29
+ ],
30
+ "layer": "infrastructure",
31
+ "group": "state",
32
+ "author": "邱文武",
33
+ "license": "ISC",
34
+ "bugs": {
35
+ "url": "https://gitee.com/qiuwenwu91/mm_config/issues"
36
+ },
37
+ "homepage": "https://gitee.com/qiuwenwu91/mm_config#readme",
38
+ "engines": {
39
+ "node": ">=14.0.0"
40
+ },
41
+ "files": [
42
+ "index.js",
43
+ "lib",
44
+ "tool",
45
+ "ai_tool",
46
+ "README.md",
47
+ "README_EN.md",
48
+ "LICENSE"
49
+ ],
50
+ "dependencies": {
51
+ "mm_admin": "^1.1.0",
52
+ "mm_eslint": "^1.0.0",
53
+ "mm_helper": "^1.0.0",
54
+ "mm_kv_store": "^1.0.0",
55
+ "mm_mod": "^1.0.1",
56
+ "mm_os": "^1.0.0"
57
+ },
58
+ "devDependencies": {
59
+ "eslint": "^10.9.1",
60
+ "eslint-plugin-jsdoc": "^64.2.1",
61
+ "mm_eslint": "^1.7.7"
62
+ }
63
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * config —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/config/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_config').tools.config 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/config');
@@ -0,0 +1,80 @@
1
+ {
2
+ "name": "config",
3
+ "handler": "manager",
4
+ "title": "配置管理器",
5
+ "description": "管理系统配置项的读取与更新。适用场景:需要动态读写全局或分组的键值配置时。工作流提示:action 选 get/set/update/list/values/clear/del/has,scope 指定分组作用域,key 指定配置键、value 传配置值,读取结果在 obj/list、写入结果在 msg。",
6
+ "type": "data",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "action": {
11
+ "type": "string",
12
+ "description": "操作类型: get/set/update/list/values/clear/del/has",
13
+ "enum": [
14
+ "get",
15
+ "set",
16
+ "update",
17
+ "list",
18
+ "values",
19
+ "clear",
20
+ "del",
21
+ "has"
22
+ ]
23
+ },
24
+ "scope": {
25
+ "type": "string",
26
+ "description": "配置分组作用域",
27
+ "default": "default"
28
+ },
29
+ "key": {
30
+ "type": "string",
31
+ "description": "配置键"
32
+ },
33
+ "value": {
34
+ "description": "配置值(set时必填)"
35
+ }
36
+ },
37
+ "required": [
38
+ "action"
39
+ ]
40
+ },
41
+ "returns": {
42
+ "description": "管理操作结果,读操作返回 obj 或 list+count,写操作返回 msg,异常时返回 error",
43
+ "type": "object",
44
+ "properties": {
45
+ "obj": {
46
+ "description": "读取操作的单条结果(get/has 操作)"
47
+ },
48
+ "list": {
49
+ "type": "array",
50
+ "description": "列表结果(list 操作)"
51
+ },
52
+ "count": {
53
+ "type": "number",
54
+ "description": "列表条目数(list 操作)"
55
+ },
56
+ "msg": {
57
+ "type": "string",
58
+ "description": "写操作的结果描述(set/del 操作)"
59
+ }
60
+ }
61
+ },
62
+ "options": {
63
+ "store_type": {
64
+ "type": "string",
65
+ "description": "存储方式: file/db/redis",
66
+ "default": "file",
67
+ "enum": [
68
+ "file",
69
+ "db",
70
+ "redis"
71
+ ]
72
+ },
73
+ "file_path": {
74
+ "type": "string",
75
+ "description": "配置文件路径(store_type=file时有效)",
76
+ "default": "./config"
77
+ }
78
+ },
79
+ "group": "system"
80
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * get_run_path —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/get_run_path/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_config').tools.get_run_path 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/get_run_path');
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "get_run_path",
3
+ "title": "获取运行根目录",
4
+ "description": "获取当前程序运行根目录,检测是否为mm_os项目并返回关键路径信息。当需要进行mm_os开发(应用/插件/API接口/定时任务/挂件)时优先调用此工具,判断当前系统是否基于mm_os开发,如果是则直接在当前运行路径下开发所需功能模块。触发关键词:运行路径、根目录、运行目录、项目路径、mm_os路径、项目根目录、检测mm_os",
5
+ "category": "系统工具",
6
+ "group": "system",
7
+ "type": "function",
8
+ "main": "./index.js",
9
+ "params": {
10
+ "type": "object",
11
+ "properties": {
12
+ "check_only": {
13
+ "type": "boolean",
14
+ "description": "是否仅检查是否为mm_os项目,不返回详细路径信息。设为true只返回is_mm_os判断结果",
15
+ "default": false
16
+ }
17
+ }
18
+ },
19
+ "returns": {
20
+ "type": "object",
21
+ "properties": {
22
+ "run_path": {
23
+ "type": "string",
24
+ "description": "当前程序运行根目录的绝对路径"
25
+ },
26
+ "is_mm_os": {
27
+ "type": "boolean",
28
+ "description": "是否为mm_os框架项目"
29
+ },
30
+ "mm_os_version": {
31
+ "type": "string",
32
+ "description": "mm_os框架版本号(安装在node_modules中的mm_os版本),非mm_os项目时为空"
33
+ },
34
+ "paths": {
35
+ "type": "object",
36
+ "description": "mm_os项目的关键子目录路径,仅is_mm_os为true时返回"
37
+ },
38
+ "code": {
39
+ "type": "number",
40
+ "description": "状态码,0成功"
41
+ },
42
+ "msg": {
43
+ "type": "string",
44
+ "description": "状态信息"
45
+ }
46
+ }
47
+ },
48
+ "handler": "manager"
49
+ }
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+ /**
3
+ * path —— 工具契约适配层
4
+ * 业务实现归位于模块 lib/tools/path/,本文件只把模块能力暴露给加载器。
5
+ * 其它模块可直接 require('mm_config').tools.path 复用。
6
+ */
7
+ module.exports = require('../../lib/tools/path');
@@ -0,0 +1,99 @@
1
+ {
2
+ "name": "path",
3
+ "handler": "querier",
4
+ "title": "路径查询器",
5
+ "description": "获取应用运行路径、当前模块路径、项目模块目录等关键路径,支持相对路径转绝对路径。适用场景:定位项目文件、拼接 SQL 脚本路径、判断资源是否存在。工作流提示:action 选 info/run/module/workflow/script/templates/data/self/self_module/self_templates/absolute/exists,absolute/exists 需传 target 相对路径与 base 基准(run/self),读在 path、exists 在 obj。",
6
+ "type": "core",
7
+ "params": {
8
+ "type": "object",
9
+ "properties": {
10
+ "action": {
11
+ "type": "string",
12
+ "enum": [
13
+ "info",
14
+ "run",
15
+ "module",
16
+ "workflow",
17
+ "script",
18
+ "templates",
19
+ "data",
20
+ "self",
21
+ "self_module",
22
+ "self_templates",
23
+ "absolute",
24
+ "exists"
25
+ ],
26
+ "description": "操作类型: info(全部关键路径)/run(应用运行路径)/module(应用模块目录)/workflow(应用工作流目录)/script(应用脚本目录)/templates(应用模板目录)/data(应用数据目录)/self(当前模块根路径)/self_module(当前模块模块目录)/self_templates(当前模块模板目录)/absolute(相对路径转绝对路径)/exists(判断路径是否存在)",
27
+ "default": "info"
28
+ },
29
+ "target": {
30
+ "type": "string",
31
+ "description": "目标相对路径(absolute/exists 操作使用,基于 base 基准解析,绝对路径原样返回)"
32
+ },
33
+ "base": {
34
+ "type": "string",
35
+ "enum": [
36
+ "run",
37
+ "self"
38
+ ],
39
+ "description": "相对路径基准目录: run(应用运行路径)/self(当前模块根路径),默认 run",
40
+ "default": "run"
41
+ }
42
+ }
43
+ },
44
+ "returns": {
45
+ "description": "路径获取结果,path 为目录绝对路径,info 返回路径集合对象,exists 返回 obj 布尔值",
46
+ "type": "object",
47
+ "properties": {
48
+ "path": {
49
+ "type": "string",
50
+ "description": "绝对路径(run/module/workflow/script/templates/data/self/self_module/self_templates/absolute 操作)"
51
+ },
52
+ "run_path": {
53
+ "type": "string",
54
+ "description": "应用运行路径(info 操作)"
55
+ },
56
+ "module_dir": {
57
+ "type": "string",
58
+ "description": "应用模块目录(info 操作)"
59
+ },
60
+ "workflow_dir": {
61
+ "type": "string",
62
+ "description": "应用工作流目录(info 操作)"
63
+ },
64
+ "script_dir": {
65
+ "type": "string",
66
+ "description": "应用脚本目录(info 操作)"
67
+ },
68
+ "templates_dir": {
69
+ "type": "string",
70
+ "description": "应用模板目录(info 操作)"
71
+ },
72
+ "data_dir": {
73
+ "type": "string",
74
+ "description": "应用数据目录(info 操作)"
75
+ },
76
+ "self_path": {
77
+ "type": "string",
78
+ "description": "当前模块根路径(info 操作)"
79
+ },
80
+ "self_module_dir": {
81
+ "type": "string",
82
+ "description": "当前模块模块目录(info 操作)"
83
+ },
84
+ "self_templates_dir": {
85
+ "type": "string",
86
+ "description": "当前模块模板目录(info 操作)"
87
+ },
88
+ "exists": {
89
+ "type": "boolean",
90
+ "description": "路径是否存在(absolute 操作附带)"
91
+ },
92
+ "obj": {
93
+ "type": "boolean",
94
+ "description": "路径是否存在(exists 操作)"
95
+ }
96
+ }
97
+ },
98
+ "group": "system"
99
+ }