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.
- package/index.js +5 -11
- package/lib/admin.js +27 -0
- package/lib/conf.js +41 -309
- package/lib/config.js +204 -1
- package/lib/config_file.js +239 -0
- package/package.json +6 -4
- package/tool/config/index.js +59 -5
- package/tool/config/tool.json +14 -5
- package/tool/get_run_path/index.js +22 -5
- package/tool/path/index.js +144 -5
- package/lib/run_path_logic.js +0 -134
- package/lib/tools/config/index.js +0 -100
- package/lib/tools/get_run_path/index.js +0 -7
- package/lib/tools/index.js +0 -16
- package/lib/tools/path/index.js +0 -147
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
const { Base } = require('mm_mod');
|
|
2
|
+
const { push } = require('mm_helper');
|
|
3
|
+
const { loadJson, saveJson } = require('mm_files');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 配置文件包装类(与 lib/config.js 的 Config 固定配置存储类无关)
|
|
7
|
+
* 一个实例对应一个配置文件,提供 get/set/save 等配置读写方法。
|
|
8
|
+
* @class
|
|
9
|
+
*/
|
|
10
|
+
class ConfigFile extends Base {
|
|
11
|
+
/**
|
|
12
|
+
* 配置选项
|
|
13
|
+
* @type {object}
|
|
14
|
+
*/
|
|
15
|
+
static config = {
|
|
16
|
+
debounce_time: 0, // 默认不防抖
|
|
17
|
+
pretty: true,
|
|
18
|
+
format: 'json',
|
|
19
|
+
file: ''
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 预设选项
|
|
24
|
+
* @type {object}
|
|
25
|
+
*/
|
|
26
|
+
static options = {
|
|
27
|
+
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 创建配置实例
|
|
32
|
+
* @param {object} config - 配置选项
|
|
33
|
+
* @param {number} config.debounce_time - 防抖时间(ms)
|
|
34
|
+
* @param {boolean} config.pretty - 是否美化输出
|
|
35
|
+
* @param {string} config.format - 格式(json/json5)
|
|
36
|
+
* @param {string} config.file - 配置文件路径
|
|
37
|
+
* @param {object} options - 选项
|
|
38
|
+
*/
|
|
39
|
+
constructor(config, options = {}) {
|
|
40
|
+
super(config);
|
|
41
|
+
this.options = { ...ConfigFile.options, ...options };
|
|
42
|
+
this._promise = Promise.resolve();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 设置配置
|
|
48
|
+
* @param {object} options - 配置对象
|
|
49
|
+
* @returns {void}
|
|
50
|
+
*/
|
|
51
|
+
ConfigFile.prototype.setOptions = function (options) {
|
|
52
|
+
push(this.options, options, true);
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 获取配置
|
|
57
|
+
* @returns {object} 配置对象
|
|
58
|
+
*/
|
|
59
|
+
ConfigFile.prototype.getOptions = function () {
|
|
60
|
+
return this.options;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 保存配置到文件(同步)
|
|
65
|
+
* @returns {boolean} 保存是否成功;未配置文件路径返回 false
|
|
66
|
+
* @throws {Error} 写文件失败时抛出
|
|
67
|
+
*/
|
|
68
|
+
ConfigFile.prototype.saveSync = function () {
|
|
69
|
+
if (!this.config.file) return false;
|
|
70
|
+
saveJson(this.config.file, this.options);
|
|
71
|
+
return true;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 保存配置到文件(异步)
|
|
76
|
+
* 与此前的写操作串行执行,避免并发写坏文件
|
|
77
|
+
* @returns {Promise<boolean>} 保存操作是否成功;未配置文件路径返回 false
|
|
78
|
+
* @throws {Error} 写文件失败时抛出
|
|
79
|
+
*/
|
|
80
|
+
ConfigFile.prototype.saveAsync = async function () {
|
|
81
|
+
if (!this.config.file) return false;
|
|
82
|
+
let do_save = this._promise.then(() => saveJson(this.config.file, this.options));
|
|
83
|
+
this._promise = do_save;
|
|
84
|
+
await do_save;
|
|
85
|
+
return true;
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 获取配置代理对象(写入时按防抖策略落盘)
|
|
90
|
+
* @param {Function} save - 落盘函数(saveSync / saveAsync)
|
|
91
|
+
* @returns {Proxy} 配置对象的代理实例
|
|
92
|
+
* @private
|
|
93
|
+
*/
|
|
94
|
+
ConfigFile.prototype._getProxy = function (save) {
|
|
95
|
+
return new Proxy(this.options, {
|
|
96
|
+
set: (obj, prop, val) => {
|
|
97
|
+
obj[prop] = val;
|
|
98
|
+
if (this.config.debounce_time > 0) {
|
|
99
|
+
clearTimeout(this._timer);
|
|
100
|
+
this._timer = setTimeout(() => save.call(this), this.config.debounce_time);
|
|
101
|
+
} else {
|
|
102
|
+
save.call(this);
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
},
|
|
106
|
+
get: (obj, prop) => {
|
|
107
|
+
if (prop === '_saveAsync') return () => this.saveAsync();
|
|
108
|
+
if (prop === '_saveSync') return () => this.saveSync();
|
|
109
|
+
if (prop === '_raw') return obj;
|
|
110
|
+
return obj[prop];
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 获取代理对象(同步落盘)
|
|
117
|
+
* @returns {Proxy} 配置对象的代理实例
|
|
118
|
+
*/
|
|
119
|
+
ConfigFile.prototype.getProxy = function () {
|
|
120
|
+
return this._getProxy(this.saveSync);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* 获取异步代理对象(异步落盘)
|
|
125
|
+
* @returns {Proxy} 异步配置对象的代理实例
|
|
126
|
+
*/
|
|
127
|
+
ConfigFile.prototype.getAsyncProxy = function () {
|
|
128
|
+
return this._getProxy(this.saveAsync);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* 获取所有配置项
|
|
133
|
+
* @returns {object} 所有配置项
|
|
134
|
+
*/
|
|
135
|
+
ConfigFile.prototype.all = function () {
|
|
136
|
+
return this.options;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 获取配置值
|
|
141
|
+
* @param {string} key - 配置键
|
|
142
|
+
* @returns {*} 配置值
|
|
143
|
+
*/
|
|
144
|
+
ConfigFile.prototype.get = function (key) {
|
|
145
|
+
return this.options[key];
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* 获取配置分节
|
|
150
|
+
* @param {string} [name] - 分节名称,缺省时返回全部配置
|
|
151
|
+
* @returns {object} 配置分节或全部配置
|
|
152
|
+
*/
|
|
153
|
+
ConfigFile.prototype.conf = function (name) {
|
|
154
|
+
if (name) {
|
|
155
|
+
return this.options[name];
|
|
156
|
+
}
|
|
157
|
+
return this.options;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 设置配置值
|
|
162
|
+
* @param {string} key - 配置键
|
|
163
|
+
* @param {*} value - 配置值
|
|
164
|
+
* @returns {void}
|
|
165
|
+
*/
|
|
166
|
+
ConfigFile.prototype.set = function (key, value) {
|
|
167
|
+
this.options[key] = value;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* 更新配置并保存到文件
|
|
172
|
+
* @param {string} [name] - 分节名称,存在时确保该分节存在后保存
|
|
173
|
+
* @returns {boolean} 更新是否成功
|
|
174
|
+
*/
|
|
175
|
+
ConfigFile.prototype.update = function (name) {
|
|
176
|
+
if (name && this.options[name] === undefined) {
|
|
177
|
+
this.options[name] = {};
|
|
178
|
+
}
|
|
179
|
+
return this.saveSync();
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 检查配置是否存在
|
|
184
|
+
* @param {string} key - 配置键
|
|
185
|
+
* @returns {boolean} 配置项是否存在
|
|
186
|
+
*/
|
|
187
|
+
ConfigFile.prototype.has = function (key) {
|
|
188
|
+
return this.options.hasOwnProperty(key);
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 删除配置项
|
|
193
|
+
* @param {string} key - 配置键
|
|
194
|
+
* @returns {void}
|
|
195
|
+
*/
|
|
196
|
+
ConfigFile.prototype.del = function (key) {
|
|
197
|
+
delete this.options[key];
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* 获取所有配置键
|
|
202
|
+
* @returns {Array<string>} 配置键列表
|
|
203
|
+
*/
|
|
204
|
+
ConfigFile.prototype.keys = function () {
|
|
205
|
+
return Object.keys(this.options);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 销毁:从配置缓存字典移除本实例
|
|
210
|
+
* @returns {void}
|
|
211
|
+
*/
|
|
212
|
+
ConfigFile.prototype.destroy = function () {
|
|
213
|
+
delete getDict()[this.config.file];
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 从配置文件加载配置
|
|
218
|
+
* @returns {boolean} 加载是否成功;未配置文件路径返回 false
|
|
219
|
+
* @throws {Error} 文件不存在或内容损坏时抛出
|
|
220
|
+
*/
|
|
221
|
+
ConfigFile.prototype.load = function () {
|
|
222
|
+
if (!this.config.file) return false;
|
|
223
|
+
this.options = loadJson(this.config.file);
|
|
224
|
+
return true;
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* 获取配置缓存字典(可变缓存,存于 mm_vars,键为配置文件全路径)
|
|
229
|
+
* @returns {object} 配置缓存字典
|
|
230
|
+
*/
|
|
231
|
+
function getDict() {
|
|
232
|
+
const vars = require('mm_vars').varAdmin('mm_config');
|
|
233
|
+
if (!vars.has('CONF_DICT')) {
|
|
234
|
+
vars.set('CONF_DICT', {});
|
|
235
|
+
}
|
|
236
|
+
return vars.get('CONF_DICT');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
module.exports = ConfigFile;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_config",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.9.1",
|
|
4
4
|
"description": "Super Meimei Configuration Synchronizer - Automatically sync configuration changes to JSON files",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -27,8 +27,8 @@
|
|
|
27
27
|
"settings",
|
|
28
28
|
"automation"
|
|
29
29
|
],
|
|
30
|
-
"layer": "
|
|
31
|
-
"group": "
|
|
30
|
+
"layer": "store",
|
|
31
|
+
"group": "data",
|
|
32
32
|
"author": "邱文武",
|
|
33
33
|
"license": "ISC",
|
|
34
34
|
"bugs": {
|
|
@@ -50,9 +50,11 @@
|
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"mm_admin": "^1.1.0",
|
|
52
52
|
"mm_eslint": "^1.0.0",
|
|
53
|
+
"mm_files": "^1.4.0",
|
|
53
54
|
"mm_helper": "^1.0.0",
|
|
54
55
|
"mm_kv_store": "^1.0.0",
|
|
55
|
-
"mm_mod": "^1.0.1"
|
|
56
|
+
"mm_mod": "^1.0.1",
|
|
57
|
+
"mm_vars": "^1.1.0"
|
|
56
58
|
},
|
|
57
59
|
"devDependencies": {
|
|
58
60
|
"eslint": "^10.9.1",
|
package/tool/config/index.js
CHANGED
|
@@ -1,7 +1,61 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
/**
|
|
3
|
-
* config
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* config - 配置管理器
|
|
3
|
+
*
|
|
4
|
+
* 架构约定:本文件只保留 main(params, ctx, options) 作为 LLM 参数路由,
|
|
5
|
+
* 真实读写能力由模块根 index.js 导出的 configAdmin 提供(LLM 操作模块的途径)。
|
|
6
|
+
* 工具内不得直接 require 底层库、不得另行声明业务函数。
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
|
+
const { configAdmin } = require('../../index.js');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 配置操作入口
|
|
12
|
+
* @param {object} params 步骤参数
|
|
13
|
+
* @param {object} ctx 上下文对象
|
|
14
|
+
* @param {object} options 运行选项
|
|
15
|
+
* @returns {object} 操作结果
|
|
16
|
+
*/
|
|
17
|
+
async function main(params, ctx, options) {
|
|
18
|
+
void options;
|
|
19
|
+
let p = params || {};
|
|
20
|
+
let config = configAdmin(p.scope || 'os');
|
|
21
|
+
if (p.action === 'batch') {
|
|
22
|
+
return _runBatch(config, p, ctx, this);
|
|
23
|
+
}
|
|
24
|
+
return config.runConfig(p, ctx, this);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 批量写入配置项,逐项复用模块的 set 能力
|
|
29
|
+
* @param {object} config 配置实例
|
|
30
|
+
* @param {object} p 步骤参数
|
|
31
|
+
* @param {object} ctx 上下文对象
|
|
32
|
+
* @param {object} host 调用方宿主
|
|
33
|
+
* @returns {object} 操作结果
|
|
34
|
+
*/
|
|
35
|
+
async function _runBatch(config, p, ctx, host) {
|
|
36
|
+
let items = p.items;
|
|
37
|
+
if (!items || typeof items !== 'object' || Array.isArray(items)) {
|
|
38
|
+
ctx.output = {
|
|
39
|
+
code: 10000,
|
|
40
|
+
msg: '执行失败:items 必须是配置项对象',
|
|
41
|
+
error: { name: 'TypeError', message: 'items 必须是配置项对象' }
|
|
42
|
+
};
|
|
43
|
+
return ctx.output;
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
let keys = Object.keys(items);
|
|
47
|
+
for (let key of keys) {
|
|
48
|
+
await config.runConfig({ ...p, action: 'set', key, value: items[key] }, ctx, host);
|
|
49
|
+
}
|
|
50
|
+
ctx.output = { code: 0, msg: '批量设置成功', count: keys.length, keys };
|
|
51
|
+
} catch (error) {
|
|
52
|
+
ctx.output = {
|
|
53
|
+
code: 10000,
|
|
54
|
+
msg: '执行失败:' + error.message,
|
|
55
|
+
error: { name: error.name, message: error.message }
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
return ctx.output;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
module.exports = { main };
|
package/tool/config/tool.json
CHANGED
|
@@ -2,14 +2,14 @@
|
|
|
2
2
|
"name": "config",
|
|
3
3
|
"handler": "manager",
|
|
4
4
|
"title": "配置管理器",
|
|
5
|
-
"description": "管理系统配置项的读取与更新。适用场景:需要动态读写全局或分组的键值配置时。工作流提示:action 选 get/set/update/list/values/clear/del/has,scope 指定分组作用域,key 指定配置键、value
|
|
5
|
+
"description": "管理系统配置项的读取与更新。适用场景:需要动态读写全局或分组的键值配置时。工作流提示:action 选 get/set/update/list/values/clear/del/has/batch,scope 指定分组作用域,key 指定配置键、value 传配置值,batch 用 items 一次写入多个配置项,读取结果在 obj/list、写入结果在 msg。",
|
|
6
6
|
"type": "data",
|
|
7
7
|
"params": {
|
|
8
8
|
"type": "object",
|
|
9
9
|
"properties": {
|
|
10
10
|
"action": {
|
|
11
11
|
"type": "string",
|
|
12
|
-
"description": "操作类型: get/set/update/list/values/clear/del/has",
|
|
12
|
+
"description": "操作类型: get/set/update/list/values/clear/del/has/batch",
|
|
13
13
|
"enum": [
|
|
14
14
|
"get",
|
|
15
15
|
"set",
|
|
@@ -18,7 +18,8 @@
|
|
|
18
18
|
"values",
|
|
19
19
|
"clear",
|
|
20
20
|
"del",
|
|
21
|
-
"has"
|
|
21
|
+
"has",
|
|
22
|
+
"batch"
|
|
22
23
|
]
|
|
23
24
|
},
|
|
24
25
|
"scope": {
|
|
@@ -32,6 +33,10 @@
|
|
|
32
33
|
},
|
|
33
34
|
"value": {
|
|
34
35
|
"description": "配置值(set时必填)"
|
|
36
|
+
},
|
|
37
|
+
"items": {
|
|
38
|
+
"type": "object",
|
|
39
|
+
"description": "批量配置项对象(batch时必填,键为配置键、值为配置值)"
|
|
35
40
|
}
|
|
36
41
|
},
|
|
37
42
|
"required": [
|
|
@@ -51,11 +56,15 @@
|
|
|
51
56
|
},
|
|
52
57
|
"count": {
|
|
53
58
|
"type": "number",
|
|
54
|
-
"description": "
|
|
59
|
+
"description": "列表条目数(list 操作)或批量写入条目数(batch 操作)"
|
|
60
|
+
},
|
|
61
|
+
"keys": {
|
|
62
|
+
"type": "array",
|
|
63
|
+
"description": "批量写入的配置键(batch 操作)"
|
|
55
64
|
},
|
|
56
65
|
"msg": {
|
|
57
66
|
"type": "string",
|
|
58
|
-
"description": "写操作的结果描述(set/del 操作)"
|
|
67
|
+
"description": "写操作的结果描述(set/del/batch 操作)"
|
|
59
68
|
}
|
|
60
69
|
}
|
|
61
70
|
},
|
|
@@ -1,7 +1,24 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
/**
|
|
3
|
-
* get_run_path
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* get_run_path - 获取当前程序运行根目录
|
|
3
|
+
*
|
|
4
|
+
* 架构约定:本文件只保留 main(params, ctx, options) 作为 LLM 参数路由,
|
|
5
|
+
* 真实检测能力由模块根 index.js 导出的 configAdmin 提供(LLM 操作模块的途径)。
|
|
6
|
+
* 工具内不得直接 require 底层库、不得另行声明业务函数。
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
|
+
const { configAdmin } = require('../../index.js');
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 主函数
|
|
12
|
+
* @param {object} params 步骤参数
|
|
13
|
+
* @param {object} ctx 上下文对象
|
|
14
|
+
* @param {object} options 运行选项
|
|
15
|
+
* @returns {object} 运行路径信息
|
|
16
|
+
*/
|
|
17
|
+
async function main(params, ctx, options) {
|
|
18
|
+
void ctx;
|
|
19
|
+
void options;
|
|
20
|
+
let config = configAdmin((params && params.scope) || 'os');
|
|
21
|
+
return config.getRunPath(!!(params && params.check_only === true));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = { main };
|
package/tool/path/index.js
CHANGED
|
@@ -1,7 +1,146 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
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} 查询结果
|
|
6
10
|
*/
|
|
7
|
-
|
|
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
|
+
let 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
|
+
let opts = (mod && typeof mod.getOptions === 'function' && mod.getOptions()) || {};
|
|
32
|
+
if (opts.self_path) return opts.self_path;
|
|
33
|
+
return _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
|
+
let 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 {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 获取全部关键路径
|
|
74
|
+
* @param {string} self_path 当前模块根路径
|
|
75
|
+
* @returns {Promise<object>} 路径集合对象
|
|
76
|
+
*/
|
|
77
|
+
async function _getAllPaths(self_path) {
|
|
78
|
+
let run_path = await _getRunPath();
|
|
79
|
+
return {
|
|
80
|
+
run_path: run_path,
|
|
81
|
+
module_dir: path.join(run_path, 'common', 'mod'),
|
|
82
|
+
workflow_dir: path.join(run_path, 'common', 'workflow'),
|
|
83
|
+
script_dir: path.join(run_path, 'common', 'script'),
|
|
84
|
+
templates_dir: path.join(run_path, 'templates'),
|
|
85
|
+
data_dir: path.join(run_path, 'data'),
|
|
86
|
+
self_path: self_path,
|
|
87
|
+
self_module_dir: path.join(self_path, 'common', 'mod'),
|
|
88
|
+
self_templates_dir: path.join(self_path, 'templates')
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async function main(params, ctx, options) {
|
|
93
|
+
let action = params.action || 'info';
|
|
94
|
+
let target = params.target || '';
|
|
95
|
+
let base = params.base || 'run';
|
|
96
|
+
let self_path = await _getSelfPath(this);
|
|
97
|
+
|
|
98
|
+
switch (action) {
|
|
99
|
+
case 'info':
|
|
100
|
+
ctx.output = await _getAllPaths(self_path);
|
|
101
|
+
break;
|
|
102
|
+
case 'run':
|
|
103
|
+
ctx.output = { path: await _getRunPath() };
|
|
104
|
+
break;
|
|
105
|
+
case 'module':
|
|
106
|
+
ctx.output = { path: await _getAppSubPath('common/mod') };
|
|
107
|
+
break;
|
|
108
|
+
case 'workflow':
|
|
109
|
+
ctx.output = { path: await _getAppSubPath('common/workflow') };
|
|
110
|
+
break;
|
|
111
|
+
case 'script':
|
|
112
|
+
ctx.output = { path: await _getAppSubPath('common/script') };
|
|
113
|
+
break;
|
|
114
|
+
case 'templates':
|
|
115
|
+
ctx.output = { path: await _getAppSubPath('templates') };
|
|
116
|
+
break;
|
|
117
|
+
case 'data':
|
|
118
|
+
ctx.output = { path: await _getAppSubPath('data') };
|
|
119
|
+
break;
|
|
120
|
+
case 'self':
|
|
121
|
+
ctx.output = { path: self_path };
|
|
122
|
+
break;
|
|
123
|
+
case 'self_module':
|
|
124
|
+
ctx.output = { path: path.join(self_path, 'common', 'mod') };
|
|
125
|
+
break;
|
|
126
|
+
case 'self_templates':
|
|
127
|
+
ctx.output = { path: path.join(self_path, 'templates') };
|
|
128
|
+
break;
|
|
129
|
+
case 'absolute': {
|
|
130
|
+
let abs_path = await _getAbsolute(target, base, self_path);
|
|
131
|
+
ctx.output = { path: abs_path, exists: _exists(abs_path) };
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case 'exists':
|
|
135
|
+
ctx.output = { obj: _exists(await _getAbsolute(target, base, self_path)) };
|
|
136
|
+
break;
|
|
137
|
+
default:
|
|
138
|
+
ctx.output = {
|
|
139
|
+
error: { code: 'QUERY_UNKNOWN_ACTION', message: '不支持的操作: ' + action }
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return ctx.output;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
module.exports = { main };
|