mm_config 2.8.1 → 2.9.1

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.
@@ -1,134 +0,0 @@
1
- /**
2
- * get_run_path - 获取当前程序运行根目录
3
- *
4
- * 检测当前运行路径是否为mm_os项目,若是则返回关键路径信息,
5
- * 方便智能体直接在当前项目下开发插件/应用/API等模块。
6
- */
7
- const fs = require('fs');
8
- const path = require('path');
9
-
10
- /**
11
- * 判断指定目录是否为mm_os项目
12
- */
13
- function checkIsMmOs(dir_path) {
14
- // 检测指标1: index.js中存在 MM_os 或 require('mm_os')
15
- const index_js_path = path.join(dir_path, 'index.js');
16
- if (fs.existsSync(index_js_path)) {
17
- try {
18
- const content = fs.readFileSync(index_js_path, 'utf-8');
19
- if (content.includes('MM_os') || content.includes("require('mm_os')") || content.includes('require("mm_os")')) {
20
- return true;
21
- }
22
- } catch (e) {
23
- // 忽略读取错误
24
- }
25
- }
26
-
27
- // 检测指标2: node_modules/mm_os 存在
28
- const mm_os_module_path = path.join(dir_path, 'node_modules', 'mm_os');
29
- if (fs.existsSync(mm_os_module_path)) {
30
- return true;
31
- }
32
-
33
- // 检测指标3: app/ 目录 + config/ 目录同时存在(典型mm_os结构)
34
- const app_path = path.join(dir_path, 'app');
35
- const config_path = path.join(dir_path, 'config');
36
- if (fs.existsSync(app_path) && fs.existsSync(config_path)) {
37
- // 进一步验证:检查app下是否有多个应用模块目录
38
- try {
39
- const apps = fs.readdirSync(app_path);
40
- const has_app_modules = apps.some(name => {
41
- const app_dir = path.join(app_path, name);
42
- return fs.statSync(app_dir).isDirectory() &&
43
- (fs.existsSync(path.join(app_dir, 'app.json')) || fs.existsSync(path.join(app_dir, 'index.js')));
44
- });
45
- if (has_app_modules) {
46
- return true;
47
- }
48
- } catch (e) {
49
- // 忽略
50
- }
51
- }
52
-
53
- return false;
54
- }
55
-
56
- /**
57
- * 获取mm_os版本号
58
- */
59
- function getMmOsVersion(dir_path) {
60
- try {
61
- const pkg_path = path.join(dir_path, 'node_modules', 'mm_os', 'package.json');
62
- if (fs.existsSync(pkg_path)) {
63
- const pkg = JSON.parse(fs.readFileSync(pkg_path, 'utf-8'));
64
- return pkg.version || '';
65
- }
66
- } catch (e) {
67
- // 忽略
68
- }
69
- return '';
70
- }
71
-
72
- /**
73
- * 获取mm_os项目的关键子目录路径
74
- */
75
- function getProjectPaths(dir_path) {
76
- return {
77
- app: path.join(dir_path, 'app'),
78
- config: path.join(dir_path, 'config'),
79
- com: path.join(dir_path, 'com'),
80
- static: path.join(dir_path, 'static')
81
- };
82
- }
83
-
84
- /**
85
- * 主函数
86
- */
87
- async function main(params, ctx, options) {
88
- try {
89
- const check_only = params && params.check_only === true;
90
- const run_path = process.cwd() + path.sep;
91
-
92
- // 检测是否为mm_os项目
93
- const is_mm_os = checkIsMmOs(run_path);
94
-
95
- let result = {
96
- run_path: run_path,
97
- is_mm_os: is_mm_os,
98
- code: 0,
99
- msg: is_mm_os ? '当前为mm_os项目' : '当前不是mm_os项目'
100
- };
101
-
102
- if (is_mm_os && !check_only) {
103
- // 获取mm_os版本
104
- result.mm_os_version = getMmOsVersion(run_path);
105
-
106
- // 获取关键子目录路径
107
- const sub_paths = getProjectPaths(run_path);
108
- result.paths = {};
109
-
110
- // 只返回实际存在的目录
111
- for (const [key, dir_path] of Object.entries(sub_paths)) {
112
- if (fs.existsSync(dir_path)) {
113
- result.paths[key] = dir_path;
114
- }
115
- }
116
- }
117
-
118
- return result;
119
- } catch (error) {
120
- return {
121
- run_path: process.cwd(),
122
- is_mm_os: false,
123
- code: -1,
124
- msg: '获取运行路径失败: ' + error.message,
125
- error: {
126
- name: error.name,
127
- message: error.message,
128
- stack: error.stack
129
- }
130
- };
131
- }
132
- }
133
-
134
- exports.main = main;
@@ -1,100 +0,0 @@
1
- const mm_config = require('mm_config');
2
-
3
- /**
4
- * 配置管理器
5
- * 基于 mm_config 实现文件持久化配置,支持 scope 命名空间
6
- * @param {object} ctx 上下文对象
7
- * @param {object} params 步骤参数
8
- */
9
- async function main(params, ctx, options) {
10
- var action = params.action || 'get';
11
- var key = params.key;
12
- var value = params.value;
13
- var scope = params.scope || 'default';
14
-
15
- var file_path = _getFilePath(params, this);
16
- // 从磁盘加载已有配置,避免每次 create 新建空配置导致数据丢失
17
- var loaded = {};
18
- try {
19
- loaded = file_path.loadJson() || {};
20
- } catch (_e) {
21
- void _e;
22
- }
23
- var config = mm_config.create(file_path, loaded);
24
-
25
- var scope_obj;
26
- switch (action) {
27
- case 'get':
28
- scope_obj = config[scope] || {};
29
- ctx.output = { obj: scope_obj[key] };
30
- break;
31
- case 'list':
32
- scope_obj = config[scope] || {};
33
- ctx.output = { list: Object.keys(scope_obj), count: Object.keys(scope_obj).length };
34
- break;
35
- case 'values':
36
- scope_obj = config[scope] || {};
37
- ctx.output = { list: Object.values(scope_obj), count: Object.keys(scope_obj).length };
38
- break;
39
- case 'set':
40
- scope_obj = config[scope] || {};
41
- scope_obj[key] = value;
42
- config[scope] = scope_obj;
43
- ctx.output = { msg: '配置设置成功' };
44
- break;
45
- case 'update':
46
- scope_obj = config[scope] || {};
47
- scope_obj[key] = { ...(scope_obj[key] || {}), ...value };
48
- config[scope] = scope_obj;
49
- ctx.output = { msg: '配置更新成功' };
50
- break;
51
- case 'clear':
52
- config[scope] = {};
53
- ctx.output = { msg: '配置清空成功' };
54
- break;
55
- case 'del':
56
- scope_obj = config[scope] || {};
57
- if (key in scope_obj) {
58
- delete scope_obj[key];
59
- config[scope] = scope_obj;
60
- ctx.output = { msg: '配置删除成功' };
61
- } else {
62
- ctx.output = { msg: '配置删除失败' };
63
- }
64
- break;
65
- case 'has':
66
- scope_obj = config[scope] || {};
67
- ctx.output = { obj: key in scope_obj };
68
- break;
69
- default:
70
- ctx.output = {
71
- error: { code: 'MGR_UNKNOWN_ACTION', message: '不支持的操作: ' + action }
72
- };
73
- }
74
-
75
- return ctx.output;
76
- }
77
-
78
- /**
79
- * 获取配置文件路径
80
- * 优先从 params.file_path 获取,fallback 到模块 options 配置
81
- * @param {object} params 步骤参数
82
- * @param {object} ctx_this 模块上下文
83
- * @returns {string} 配置文件路径
84
- */
85
- function _getFilePath(params, ctx_this) {
86
- var opts = (ctx_this && ctx_this.config && ctx_this.config.options) || {};
87
- var default_path = './config';
88
- if (
89
- opts.file_path &&
90
- typeof opts.file_path === 'object' &&
91
- opts.file_path.default !== undefined
92
- ) {
93
- default_path = opts.file_path.default;
94
- } else if (opts.file_path) {
95
- default_path = opts.file_path;
96
- }
97
- return params.file_path || default_path;
98
- }
99
-
100
- module.exports = { main };
@@ -1,7 +0,0 @@
1
- 'use strict';
2
- /**
3
- * get_run_path —— 工具契约适配层
4
- * 业务实现归位于模块 lib/run_path_logic.js,本文件只把模块能力暴露给工具加载器。
5
- * 其它模块可直接 require('mm_config').runPathLogic 复用,无需经过工具注册面。
6
- */
7
- module.exports = require('../../run_path_logic.js');
@@ -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,147 +0,0 @@
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 };