node-karin 0.10.4 → 0.10.5
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/lib/cli/karin.d.ts +13 -1
- package/lib/cli/karin.js +2 -1
- package/lib/utils/config/yamlEditor.d.ts +10 -4
- package/lib/utils/config/yamlEditor.js +40 -2
- package/lib/utils/tools/exec.d.ts +10 -0
- package/lib/utils/tools/exec.js +17 -0
- package/package.json +1 -1
package/lib/cli/karin.d.ts
CHANGED
|
@@ -1,2 +1,14 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
export {
|
|
2
|
+
export declare const enum Runner {
|
|
3
|
+
Node = "node",
|
|
4
|
+
Tsx = "tsx",
|
|
5
|
+
Pm2 = "pm2"
|
|
6
|
+
}
|
|
7
|
+
export declare const enum Mode {
|
|
8
|
+
Dev = "dev",
|
|
9
|
+
Prod = "prod"
|
|
10
|
+
}
|
|
11
|
+
export declare const enum Lang {
|
|
12
|
+
Js = "js",
|
|
13
|
+
Ts = "ts"
|
|
14
|
+
}
|
package/lib/cli/karin.js
CHANGED
|
@@ -14,6 +14,7 @@ class KarinCli {
|
|
|
14
14
|
file
|
|
15
15
|
constructor () {
|
|
16
16
|
process.env.karin_app_start_count = '0'
|
|
17
|
+
process.env.karin_app_watch = 'no'
|
|
17
18
|
/** 当前文件绝对路径 */
|
|
18
19
|
this.filename = fileURLToPath(import.meta.url)
|
|
19
20
|
/** karin目录 */
|
|
@@ -77,7 +78,7 @@ class KarinCli {
|
|
|
77
78
|
}
|
|
78
79
|
}
|
|
79
80
|
/** 启动 */
|
|
80
|
-
this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env })
|
|
81
|
+
this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env, shell: runner === 'tsx' /* Runner.Tsx */ })
|
|
81
82
|
/** 监听退出 */
|
|
82
83
|
this.child.once('exit', (code) => process.exit(code))
|
|
83
84
|
/** 监听子进程消息 */
|
|
@@ -15,13 +15,13 @@ export declare class YamlEditor {
|
|
|
15
15
|
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
16
16
|
* @param value - 要设置的值 允许的类型:`string`, `boolean`, `number`, `object`, `array`
|
|
17
17
|
*/
|
|
18
|
-
set(path: string, value: YamlValue):
|
|
18
|
+
set(path: string, value: YamlValue): boolean;
|
|
19
19
|
/**
|
|
20
20
|
* 向指定路径添加新值
|
|
21
21
|
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
22
22
|
* @param value - 要添加的值
|
|
23
23
|
*/
|
|
24
|
-
add(path: string, value: YamlValue):
|
|
24
|
+
add(path: string, value: YamlValue): boolean;
|
|
25
25
|
/**
|
|
26
26
|
* 删除指定路径
|
|
27
27
|
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
@@ -34,7 +34,13 @@ export declare class YamlEditor {
|
|
|
34
34
|
* @param value - 要添加的值
|
|
35
35
|
* @param prepend - 如果为 true,则添加到数组的开头,否则添加到末尾
|
|
36
36
|
*/
|
|
37
|
-
append(path: string, value: string, prepend?: boolean):
|
|
37
|
+
append(path: string, value: string, prepend?: boolean): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* 向指定路径的数组删除值
|
|
40
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
41
|
+
* @param value - 要删除的值
|
|
42
|
+
*/
|
|
43
|
+
remove(path: string, value: YamlValue): boolean;
|
|
38
44
|
/**
|
|
39
45
|
* 检查指定路径的键是否存在
|
|
40
46
|
* @param path - 路径,用点号分隔
|
|
@@ -57,7 +63,7 @@ export declare class YamlEditor {
|
|
|
57
63
|
* 向根节点新增元素,如果根节点不是数组,则将其转换为数组再新增元素
|
|
58
64
|
* @param value - 要新增的元素
|
|
59
65
|
*/
|
|
60
|
-
pusharr(value: YamlValue):
|
|
66
|
+
pusharr(value: YamlValue): boolean;
|
|
61
67
|
/**
|
|
62
68
|
* 根据索引从根节点数组删除元素
|
|
63
69
|
* @param index - 要删除元素的索引
|
|
@@ -41,10 +41,11 @@ export class YamlEditor {
|
|
|
41
41
|
try {
|
|
42
42
|
const _path = typeof path === 'string' ? path.split('.') : path;
|
|
43
43
|
this.document.setIn(_path, value);
|
|
44
|
+
return true;
|
|
44
45
|
}
|
|
45
46
|
catch (error) {
|
|
46
47
|
logger.error(`[YamlEditor] 设置数据时出错:${error}`);
|
|
47
|
-
return
|
|
48
|
+
return false;
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
/**
|
|
@@ -57,9 +58,11 @@ export class YamlEditor {
|
|
|
57
58
|
const _path = typeof path === 'string' ? path.split('.') : path;
|
|
58
59
|
this.document.addIn(_path, value);
|
|
59
60
|
logger.debug(`[YamlEditor] 已在 ${path} 添加新的值`);
|
|
61
|
+
return true;
|
|
60
62
|
}
|
|
61
63
|
catch (error) {
|
|
62
64
|
logger.error(`[YamlEditor] 添加数据时出错:${error}`);
|
|
65
|
+
return false;
|
|
63
66
|
}
|
|
64
67
|
}
|
|
65
68
|
/**
|
|
@@ -93,15 +96,49 @@ export class YamlEditor {
|
|
|
93
96
|
this.document.setIn(_path, current);
|
|
94
97
|
}
|
|
95
98
|
else if (!(current instanceof Yaml.YAMLSeq)) {
|
|
96
|
-
|
|
99
|
+
logger.error('[YamlEditor] 指定的路径不是数组');
|
|
100
|
+
return false;
|
|
97
101
|
}
|
|
98
102
|
else {
|
|
99
103
|
prepend ? current.items.unshift(value) : current.add(value);
|
|
100
104
|
}
|
|
101
105
|
logger.debug(`[YamlEditor] 已向 ${path} 数组${prepend ? '开头' : '末尾'}添加新元素:${value}`);
|
|
106
|
+
return true;
|
|
102
107
|
}
|
|
103
108
|
catch (error) {
|
|
104
109
|
logger.error(`[YamlEditor] 向数组添加元素时出错:${error}`);
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* 向指定路径的数组删除值
|
|
115
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
116
|
+
* @param value - 要删除的值
|
|
117
|
+
*/
|
|
118
|
+
remove(path, value) {
|
|
119
|
+
try {
|
|
120
|
+
const _path = typeof path === 'string' ? path.split('.') : path;
|
|
121
|
+
const current = this.document.getIn(_path);
|
|
122
|
+
if (!current) {
|
|
123
|
+
logger.error('[YamlEditor] 指定的路径不存在');
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
if (!(current instanceof Yaml.YAMLSeq)) {
|
|
127
|
+
logger.error('[YamlEditor] 指定的路径不是数组');
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
const index = current.items.findIndex(item => lodash.isEqual(item.toJSON(), value));
|
|
131
|
+
if (index < 0) {
|
|
132
|
+
logger.error('[YamlEditor] 未找到要删除的值');
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
current.items.splice(index, 1);
|
|
136
|
+
logger.debug(`[YamlEditor] 已从 ${path} 数组删除元素:${value}`);
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
logger.error(`[YamlEditor] 从数组删除元素时出错:${error}`);
|
|
141
|
+
return false;
|
|
105
142
|
}
|
|
106
143
|
}
|
|
107
144
|
/**
|
|
@@ -170,6 +207,7 @@ export class YamlEditor {
|
|
|
170
207
|
}
|
|
171
208
|
this.document.contents.add(value);
|
|
172
209
|
logger.debug('[YamlEditor] 已向根节点数组新增元素:', value);
|
|
210
|
+
return true;
|
|
173
211
|
}
|
|
174
212
|
catch (error) {
|
|
175
213
|
logger.error(`[YamlEditor] 向根节点数组新增元素时出错:${error}`);
|
|
@@ -19,4 +19,14 @@ export declare const exec: (cmd: string, log?: boolean, options?: {
|
|
|
19
19
|
stdout: string | "";
|
|
20
20
|
stderr: string | "";
|
|
21
21
|
}>;
|
|
22
|
+
/**
|
|
23
|
+
* 执行 shell 命令
|
|
24
|
+
* @param cmd 命令
|
|
25
|
+
* @param log 是否打印日志
|
|
26
|
+
* @param options 选项
|
|
27
|
+
*/
|
|
28
|
+
export declare const execs: (cmd: string, options?: {
|
|
29
|
+
cwd: string;
|
|
30
|
+
encoding: string;
|
|
31
|
+
}) => Promise<string>;
|
|
22
32
|
export default exec;
|
package/lib/utils/tools/exec.js
CHANGED
|
@@ -34,4 +34,21 @@ export const exec = (cmd, log = true, options = { cwd: process.cwd(), encoding:
|
|
|
34
34
|
});
|
|
35
35
|
});
|
|
36
36
|
};
|
|
37
|
+
/**
|
|
38
|
+
* 执行 shell 命令
|
|
39
|
+
* @param cmd 命令
|
|
40
|
+
* @param log 是否打印日志
|
|
41
|
+
* @param options 选项
|
|
42
|
+
*/
|
|
43
|
+
export const execs = (cmd, options = { cwd: process.cwd(), encoding: 'utf-8' }) => {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
execCmd(cmd, options, (error, stdout, stderr) => {
|
|
46
|
+
if (error)
|
|
47
|
+
return reject(error);
|
|
48
|
+
if (stderr)
|
|
49
|
+
console.error(stderr);
|
|
50
|
+
resolve(stdout.trim());
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
};
|
|
37
54
|
export default exec;
|