node-karin 0.8.1 → 0.8.2
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.js +2 -2
- package/lib/core/plugin.loader.js +3 -2
- package/lib/utils/common.d.ts +22 -3
- package/lib/utils/common.js +33 -3
- package/lib/utils/config.d.ts +16 -16
- package/lib/utils/config.js +21 -21
- package/lib/utils/yamlEditor.d.ts +8 -1
- package/lib/utils/yamlEditor.js +27 -2
- package/package.json +1 -1
package/lib/cli/karin.js
CHANGED
|
@@ -267,8 +267,8 @@ program.command('start').description('启动karin').action(() => cli.start('prod
|
|
|
267
267
|
program.command('pm2').description('后台运行karin').action(() => cli.start('prod' /* Mode.Prod */, 'js' /* Lang.Js */, 'pm2' /* Runner.Pm2 */))
|
|
268
268
|
program.command('stop').description('停止后台运行').action(() => cli.stop())
|
|
269
269
|
program.command('rs').description('重启pm2服务').action(() => cli.restart())
|
|
270
|
-
program.command('dev').description('
|
|
271
|
-
program.command('
|
|
270
|
+
program.command('dev').description('JavaScript开发模式').action(() => cli.start('dev' /* Mode.Dev */, 'js' /* Lang.Js */, 'node' /* Runner.Node */))
|
|
271
|
+
program.command('ts').description('TypeScript开发模式').action(() => cli.start('dev' /* Mode.Dev */, 'ts' /* Lang.Ts */, 'tsx' /* Runner.Tsx */))
|
|
272
272
|
program.command('log').description('查看日志').action(() => cli.log())
|
|
273
273
|
program.command('up').description('更新依赖').action(() => cli.update())
|
|
274
274
|
program.parse(process.argv)
|
|
@@ -185,8 +185,9 @@ class PluginLoader {
|
|
|
185
185
|
} else {
|
|
186
186
|
/** 入口文件 */
|
|
187
187
|
if (common.exists(`${PluginPath}/${rootDir}/index.ts`)) {
|
|
188
|
-
|
|
189
|
-
this.
|
|
188
|
+
const dirPath = `${dir}/${rootDir}`
|
|
189
|
+
this.FileList.push({ dir: dirPath, name: 'index.ts' })
|
|
190
|
+
this.isDev && this.watchList.push({ dir: dirPath, name: 'index.ts' })
|
|
190
191
|
continue
|
|
191
192
|
}
|
|
192
193
|
/** ts源码 */
|
package/lib/utils/common.d.ts
CHANGED
|
@@ -5,8 +5,9 @@ import { dirName, fileName, KarinElement, NodeElement } from '../types/index.js'
|
|
|
5
5
|
/**
|
|
6
6
|
* 常用方法
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
declare class Common {
|
|
9
9
|
streamPipeline: (stream1: Readable, stream2: fs.WriteStream) => Promise<void>;
|
|
10
|
+
constructor();
|
|
10
11
|
/**
|
|
11
12
|
* 休眠函数
|
|
12
13
|
* @param ms 毫秒
|
|
@@ -25,7 +26,7 @@ export declare const common: {
|
|
|
25
26
|
* @param type 请求类型
|
|
26
27
|
* @param param axios参数
|
|
27
28
|
*/
|
|
28
|
-
axios(url: string, type?:
|
|
29
|
+
axios(url: string, type?: 'get' | 'post', param?: AxiosRequestConfig): Promise<any | null>;
|
|
29
30
|
/**
|
|
30
31
|
* 递归创建目录
|
|
31
32
|
* @param dirname - 要创建的文件夹路径
|
|
@@ -159,4 +160,22 @@ export declare const common: {
|
|
|
159
160
|
* @param - 消息体
|
|
160
161
|
*/
|
|
161
162
|
makeMessageLog(message: Array<KarinElement>): string;
|
|
162
|
-
|
|
163
|
+
/**
|
|
164
|
+
* 更新yaml文件
|
|
165
|
+
* @param filePath - 文件路径
|
|
166
|
+
* @param settings - 设置项
|
|
167
|
+
*/
|
|
168
|
+
updateYaml(filePath: string, settings: {
|
|
169
|
+
/** 键路径 */
|
|
170
|
+
key: string;
|
|
171
|
+
/** 要写入的 */
|
|
172
|
+
val: any;
|
|
173
|
+
/** 需要写入的注释 */
|
|
174
|
+
comment: string;
|
|
175
|
+
}[]): void;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* 常用方法
|
|
179
|
+
*/
|
|
180
|
+
export declare const common: Common;
|
|
181
|
+
export {};
|
package/lib/utils/common.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { promisify } from 'util'
|
|
2
2
|
import { fileURLToPath } from 'url'
|
|
3
3
|
import { pipeline, Readable } from 'stream'
|
|
4
|
-
import { logger, segment } from '../utils/index.js'
|
|
4
|
+
import { logger, segment, YamlEditor } from '../utils/index.js'
|
|
5
5
|
import { fs, path, axios, lodash, yaml as Yaml } from '../modules.js'
|
|
6
6
|
/**
|
|
7
7
|
* 常用方法
|
|
8
8
|
*/
|
|
9
|
-
|
|
9
|
+
class Common {
|
|
10
10
|
streamPipeline
|
|
11
11
|
constructor () {
|
|
12
12
|
this.streamPipeline = promisify(pipeline)
|
|
@@ -398,6 +398,7 @@ export const common = new (class Common {
|
|
|
398
398
|
'lodash',
|
|
399
399
|
'log4js',
|
|
400
400
|
'moment',
|
|
401
|
+
'node-karin',
|
|
401
402
|
'node-schedule',
|
|
402
403
|
'redis',
|
|
403
404
|
'ws',
|
|
@@ -555,4 +556,33 @@ export const common = new (class Common {
|
|
|
555
556
|
}
|
|
556
557
|
return logs.join('')
|
|
557
558
|
}
|
|
558
|
-
|
|
559
|
+
|
|
560
|
+
/**
|
|
561
|
+
* 更新yaml文件
|
|
562
|
+
* @param filePath - 文件路径
|
|
563
|
+
* @param settings - 设置项
|
|
564
|
+
*/
|
|
565
|
+
updateYaml (filePath, settings) {
|
|
566
|
+
let yaml = new YamlEditor(filePath)
|
|
567
|
+
/** 先添加内容 */
|
|
568
|
+
settings.forEach(({ key, val }) => {
|
|
569
|
+
try {
|
|
570
|
+
if (!yaml.has(key)) { yaml.set(key, val) }
|
|
571
|
+
} catch { }
|
|
572
|
+
})
|
|
573
|
+
/** 先保存 */
|
|
574
|
+
yaml.save()
|
|
575
|
+
/** 重新解析 再次写入注释 直接写入注释会报错 写入的不是node节点模式 */
|
|
576
|
+
yaml = new YamlEditor(filePath)
|
|
577
|
+
settings.forEach(({ key, comment }) => {
|
|
578
|
+
try {
|
|
579
|
+
yaml.comment(key, comment, true)
|
|
580
|
+
} catch { }
|
|
581
|
+
})
|
|
582
|
+
yaml.save()
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* 常用方法
|
|
587
|
+
*/
|
|
588
|
+
export const common = new Common()
|
package/lib/utils/config.d.ts
CHANGED
|
@@ -5,21 +5,21 @@ import { Redis, App, Config, Server, Package, GroupCfg } from '../types/index.js
|
|
|
5
5
|
*/
|
|
6
6
|
export declare const config: {
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* 运行目录绝对路径
|
|
9
9
|
*/
|
|
10
|
-
|
|
10
|
+
dir: string;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* 运行目录配置文件夹路径
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
cfgDir: string;
|
|
15
15
|
/**
|
|
16
|
-
* npm包路径
|
|
16
|
+
* node-karin npm包路径
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
pkgDir: string;
|
|
19
19
|
/**
|
|
20
|
-
* karin包配置文件夹路径
|
|
20
|
+
* node-karin 包配置文件夹路径
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
pkgCfgDir: string;
|
|
23
23
|
change: Map<string, any>;
|
|
24
24
|
watcher: Map<string, any>;
|
|
25
25
|
review: boolean;
|
|
@@ -90,21 +90,21 @@ export declare const config: {
|
|
|
90
90
|
};
|
|
91
91
|
export declare const Cfg: {
|
|
92
92
|
/**
|
|
93
|
-
*
|
|
93
|
+
* 运行目录绝对路径
|
|
94
94
|
*/
|
|
95
|
-
|
|
95
|
+
dir: string;
|
|
96
96
|
/**
|
|
97
|
-
*
|
|
97
|
+
* 运行目录配置文件夹路径
|
|
98
98
|
*/
|
|
99
|
-
|
|
99
|
+
cfgDir: string;
|
|
100
100
|
/**
|
|
101
|
-
* npm包路径
|
|
101
|
+
* node-karin npm包路径
|
|
102
102
|
*/
|
|
103
|
-
|
|
103
|
+
pkgDir: string;
|
|
104
104
|
/**
|
|
105
|
-
* karin包配置文件夹路径
|
|
105
|
+
* node-karin 包配置文件夹路径
|
|
106
106
|
*/
|
|
107
|
-
|
|
107
|
+
pkgCfgDir: string;
|
|
108
108
|
change: Map<string, any>;
|
|
109
109
|
watcher: Map<string, any>;
|
|
110
110
|
review: boolean;
|
package/lib/utils/config.js
CHANGED
|
@@ -7,30 +7,30 @@ import { common } from './common.js'
|
|
|
7
7
|
*/
|
|
8
8
|
export const config = new (class Cfg {
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* 运行目录绝对路径
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
dir
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
14
|
+
* 运行目录配置文件夹路径
|
|
15
15
|
*/
|
|
16
|
-
|
|
16
|
+
cfgDir
|
|
17
17
|
/**
|
|
18
|
-
* npm包路径
|
|
18
|
+
* node-karin npm包路径
|
|
19
19
|
*/
|
|
20
|
-
|
|
20
|
+
pkgDir
|
|
21
21
|
/**
|
|
22
|
-
* karin包配置文件夹路径
|
|
22
|
+
* node-karin 包配置文件夹路径
|
|
23
23
|
*/
|
|
24
|
-
|
|
24
|
+
pkgCfgDir
|
|
25
25
|
change
|
|
26
26
|
watcher
|
|
27
27
|
review
|
|
28
28
|
logger
|
|
29
29
|
constructor () {
|
|
30
|
-
this.
|
|
31
|
-
this.
|
|
32
|
-
this.
|
|
33
|
-
this.
|
|
30
|
+
this.dir = process.cwd()
|
|
31
|
+
this.pkgDir = karinDir
|
|
32
|
+
this.cfgDir = this.dir + '/config'
|
|
33
|
+
this.pkgCfgDir = this.pkgDir + '/config/defSet'
|
|
34
34
|
/** 缓存 */
|
|
35
35
|
this.change = new Map()
|
|
36
36
|
/** 监听文件 */
|
|
@@ -43,17 +43,17 @@ export const config = new (class Cfg {
|
|
|
43
43
|
/** 初始化配置 */
|
|
44
44
|
async initCfg () {
|
|
45
45
|
const list = [
|
|
46
|
-
this.
|
|
47
|
-
this.
|
|
48
|
-
this.
|
|
49
|
-
this.
|
|
46
|
+
this.dir + '/temp/input',
|
|
47
|
+
this.dir + '/plugins/karin-plugin-example',
|
|
48
|
+
this.cfgDir + '/config',
|
|
49
|
+
this.cfgDir + '/plugin',
|
|
50
50
|
]
|
|
51
51
|
list.forEach(path => this.mkdir(path))
|
|
52
|
-
if (this.
|
|
53
|
-
const files = fs.readdirSync(this.
|
|
52
|
+
if (this.pkgCfgDir !== (this.cfgDir + '/defSet').replace(/\\/g, '/')) {
|
|
53
|
+
const files = fs.readdirSync(this.pkgCfgDir).filter(file => file.endsWith('.yaml'))
|
|
54
54
|
files.forEach(file => {
|
|
55
|
-
const path = `${this.
|
|
56
|
-
const pathDef = `${this.
|
|
55
|
+
const path = `${this.cfgDir}/config/${file}`
|
|
56
|
+
const pathDef = `${this.pkgCfgDir}/${file}`
|
|
57
57
|
if (!fs.existsSync(path)) { fs.copyFileSync(pathDef, path) }
|
|
58
58
|
})
|
|
59
59
|
}
|
|
@@ -64,7 +64,7 @@ export const config = new (class Cfg {
|
|
|
64
64
|
'temp',
|
|
65
65
|
'resources',
|
|
66
66
|
'temp/html',
|
|
67
|
-
this.
|
|
67
|
+
this.cfgDir + '/plugin',
|
|
68
68
|
]
|
|
69
69
|
DataList.forEach(path => this.dirPath(path, plugins))
|
|
70
70
|
this.logger = (await import('./logger.js')).default
|
|
@@ -2,6 +2,7 @@ import Yaml from 'yaml';
|
|
|
2
2
|
export type YamlValue = string | boolean | number | object | any[];
|
|
3
3
|
export declare class YamlEditor {
|
|
4
4
|
filePath: string;
|
|
5
|
+
doc: Yaml.Document;
|
|
5
6
|
document: Yaml.Document;
|
|
6
7
|
constructor(file: string);
|
|
7
8
|
/**
|
|
@@ -73,13 +74,19 @@ export declare class YamlEditor {
|
|
|
73
74
|
* @param comment - 要设置的注释
|
|
74
75
|
* @param prepend - 如果为 true,则添加到注释的开头,否则添加到同一行的末尾
|
|
75
76
|
*/
|
|
76
|
-
comment(path: string, comment: string, prepend
|
|
77
|
+
comment(path: string, comment: string, prepend: boolean): void;
|
|
77
78
|
/**
|
|
78
79
|
* 删除指定键的注释
|
|
79
80
|
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
80
81
|
* @param type - 要删除的注释类型,`before` 为注释前,`after` 为注释后,`all` 为全部
|
|
81
82
|
*/
|
|
82
83
|
uncomment(path: string, type?: 'before' | 'after' | 'all'): void;
|
|
84
|
+
/**
|
|
85
|
+
* 检查注释是否存在
|
|
86
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
87
|
+
* @param type - 要检查的注释类型,`before` 为注释前,`after` 为注释后
|
|
88
|
+
*/
|
|
89
|
+
hascomment(path: string, type: 'before' | 'after'): boolean;
|
|
83
90
|
/**
|
|
84
91
|
* 保存文件
|
|
85
92
|
*/
|
package/lib/utils/yamlEditor.js
CHANGED
|
@@ -9,10 +9,13 @@ import logger from './logger.js'
|
|
|
9
9
|
import Yaml, { isMap, isSeq, isPair } from 'yaml'
|
|
10
10
|
export class YamlEditor {
|
|
11
11
|
filePath
|
|
12
|
+
doc
|
|
12
13
|
document
|
|
13
14
|
constructor (file) {
|
|
14
15
|
this.filePath = file
|
|
15
|
-
|
|
16
|
+
const data = Yaml.parseDocument(fs.readFileSync(file, 'utf8'))
|
|
17
|
+
this.doc = data
|
|
18
|
+
this.document = data
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
/**
|
|
@@ -217,10 +220,11 @@ export class YamlEditor {
|
|
|
217
220
|
* @param comment - 要设置的注释
|
|
218
221
|
* @param prepend - 如果为 true,则添加到注释的开头,否则添加到同一行的末尾
|
|
219
222
|
*/
|
|
220
|
-
comment (path, comment, prepend
|
|
223
|
+
comment (path, comment, prepend) {
|
|
221
224
|
if (!path) { throw new Error('[YamlEditor] path 不能为空') }
|
|
222
225
|
const pair = this.getpair(path)
|
|
223
226
|
if (!pair) { throw new Error(`[YamlEditor] 未找到节点 ${path}`) }
|
|
227
|
+
comment = ` ${comment}`
|
|
224
228
|
if (prepend) {
|
|
225
229
|
pair.key.commentBefore = comment
|
|
226
230
|
} else {
|
|
@@ -247,11 +251,32 @@ export class YamlEditor {
|
|
|
247
251
|
}
|
|
248
252
|
}
|
|
249
253
|
|
|
254
|
+
/**
|
|
255
|
+
* 检查注释是否存在
|
|
256
|
+
* @param path - 路径,多个路径使用`.`连接,例如:`a.b.c`
|
|
257
|
+
* @param type - 要检查的注释类型,`before` 为注释前,`after` 为注释后
|
|
258
|
+
*/
|
|
259
|
+
hascomment (path, type) {
|
|
260
|
+
if (!path) { throw new Error('[YamlEditor] path 不能为空') }
|
|
261
|
+
const pair = this.getpair(path)
|
|
262
|
+
if (!pair) { throw new Error(`[YamlEditor] 未找到节点 ${path}`) }
|
|
263
|
+
if (type === 'before') {
|
|
264
|
+
return !!pair.key.commentBefore
|
|
265
|
+
} else if (type === 'after') {
|
|
266
|
+
return !!pair.key.comment
|
|
267
|
+
}
|
|
268
|
+
return false
|
|
269
|
+
}
|
|
270
|
+
|
|
250
271
|
/**
|
|
251
272
|
* 保存文件
|
|
252
273
|
*/
|
|
253
274
|
save () {
|
|
254
275
|
try {
|
|
276
|
+
if (this.doc.toString() === this.document.toString()) {
|
|
277
|
+
logger.info('[YamlEditor] 文件未更改,无需保存')
|
|
278
|
+
return
|
|
279
|
+
}
|
|
255
280
|
fs.writeFileSync(this.filePath, this.document.toString())
|
|
256
281
|
logger.info('[YamlEditor] 文件已保存')
|
|
257
282
|
} catch (error) {
|