node-karin 0.11.11 → 0.11.13

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.
@@ -95,92 +95,98 @@ class PluginLoader {
95
95
  const plugins = common.getPlugins();
96
96
  const list = [];
97
97
  for (const dir of plugins) {
98
- /** 插件包路径 例如: ./plugins/karin-plugin-example */
99
- const root = `${this.dir}/${dir}`;
100
- /** 非插件包 加载该文件夹下全部js 视语言环境加载ts */
101
- if (!common.isPlugin(root)) {
102
- /** 热更新 */
103
- this.watchList.push({ plugin: dir, path: '' });
104
- list.push(...this.loadApps(dir, '', `${"js" /* AppsType.Js */}`));
105
- continue;
106
- }
107
- /** package */
108
- const pkg = common.readJson(`${root}/package.json`);
109
- this.loadMain(root, pkg.main, false);
110
- /**
111
- * 插件加载优先级:
112
- * 1. ts环境: 按照 编译产物 > TS源代码 举例: 优先级高的不存在才会加载下一个。
113
- * 2. js环境: 按照 pkg.apps 进行加载。 当前版本兼容apps,apps处于根目录并且没有在pkg.apps中填写会自动加载,后续废弃。
114
- *
115
- * 注: ts编译产物的路径也是根据pkg.apps进行加载的。
116
- *
117
- * 开发环境说明:
118
- * 1. ts环境默认为开发模式
119
- * 2. 在开发环境下 会对所有 `.js`,`.ts` 文件进行热更新 只需要点下保存即可。
120
- * 3. 监察者模式当前暂未适配 待后续适配。
121
- */
122
- /** ts环境 */
123
- if (process.env.karin_app_lang === 'ts') {
124
- /** 加载ts入口 */
125
- if (pkg?.karin?.main)
126
- this.loadMain(root, pkg.karin.main, false);
127
- /** 获取apps */
128
- const apps = pkg?.karin?.apps && Array.isArray(pkg.karin.apps) ? pkg.karin.apps : [];
129
- /** apps为空则跳过 */
130
- if (!apps.length)
98
+ try {
99
+ /** 插件包路径 例如: ./plugins/karin-plugin-example */
100
+ const root = `${this.dir}/${dir}`;
101
+ /** 非插件包 加载该文件夹下全部js 视语言环境加载ts */
102
+ if (!common.isPlugin(root)) {
103
+ /** 热更新 */
104
+ this.watchList.push({ plugin: dir, path: '' });
105
+ list.push(...this.loadApps(dir, '', `${"js" /* AppsType.Js */}`));
131
106
  continue;
132
- const { outDir, rootDir } = await this.getTsMain(root, pkg);
133
- /** 先瞅瞅编译产物根目录是否存在 */
134
- if (common.exists(path.join(root, outDir))) {
135
- for (const file of apps) {
136
- const rootApps = path.join(root, outDir, file);
137
- if (!common.exists(rootApps)) {
138
- logger.debug(`[插件收集][${rootApps}] 路径不存在,已忽略`);
139
- continue;
107
+ }
108
+ /** package */
109
+ const pkg = common.readJson(`${root}/package.json`, true);
110
+ this.loadMain(root, pkg.main, false);
111
+ /**
112
+ * 插件加载优先级:
113
+ * 1. ts环境: 按照 编译产物 > TS源代码 举例: 优先级高的不存在才会加载下一个。
114
+ * 2. js环境: 按照 pkg.apps 进行加载。 当前版本兼容apps,apps处于根目录并且没有在pkg.apps中填写会自动加载,后续废弃。
115
+ *
116
+ * 注: ts编译产物的路径也是根据pkg.apps进行加载的。
117
+ *
118
+ * 开发环境说明:
119
+ * 1. ts环境默认为开发模式
120
+ * 2. 在开发环境下 会对所有 `.js`,`.ts` 文件进行热更新 只需要点下保存即可。
121
+ * 3. 监察者模式当前暂未适配 待后续适配。
122
+ */
123
+ /** ts环境 */
124
+ if (process.env.karin_app_lang === 'ts') {
125
+ /** 加载ts入口 */
126
+ if (pkg?.karin?.main)
127
+ this.loadMain(root, pkg.karin.main, false);
128
+ /** 获取apps */
129
+ const apps = pkg?.karin?.apps && Array.isArray(pkg.karin.apps) ? pkg.karin.apps : [];
130
+ /** apps为空则跳过 */
131
+ if (!apps.length)
132
+ continue;
133
+ const { outDir, rootDir } = await this.getTsMain(root, pkg);
134
+ /** 先瞅瞅编译产物根目录是否存在 */
135
+ if (common.exists(path.join(root, outDir))) {
136
+ for (const file of apps) {
137
+ const rootApps = path.join(root, outDir, file);
138
+ if (!common.exists(rootApps)) {
139
+ logger.debug(`[插件收集][${rootApps}] 路径不存在,已忽略`);
140
+ continue;
141
+ }
142
+ const _path = path.join(outDir, file);
143
+ /** 热更新 */
144
+ this.watchList.push({ plugin: dir, path: _path });
145
+ list.push(...this.loadApps(dir, _path, `${"git" /* AppsType.Git */}`));
140
146
  }
141
- const _path = path.join(outDir, file);
142
- /** 热更新 */
143
- this.watchList.push({ plugin: dir, path: _path });
144
- list.push(...this.loadApps(dir, _path, `${"git" /* AppsType.Git */}`));
147
+ continue;
148
+ }
149
+ /** 源码根目录 */
150
+ if (common.exists(path.join(root, rootDir))) {
151
+ for (const file of apps) {
152
+ const rootApps = path.join(root, rootDir, file);
153
+ if (!common.exists(rootApps)) {
154
+ logger.debug(`[插件收集][${rootApps}] 路径不存在,已忽略`);
155
+ continue;
156
+ }
157
+ const _path = path.join(rootDir, file);
158
+ /** 热更新 */
159
+ this.watchList.push({ plugin: dir, path: _path });
160
+ list.push(...this.loadApps(dir, _path, `${"git" /* AppsType.Git */}`));
161
+ }
162
+ continue;
145
163
  }
164
+ /** 走到这说明有问题 打印错误信息 */
165
+ logger.error(`[插件收集][${dir}] 加载错误,未检测到任何文件,请检查配置是否正确`);
146
166
  continue;
147
167
  }
148
- /** 源码根目录 */
149
- if (common.exists(path.join(root, rootDir))) {
150
- for (const file of apps) {
151
- const rootApps = path.join(root, rootDir, file);
152
- if (!common.exists(rootApps)) {
153
- logger.debug(`[插件收集][${rootApps}] 路径不存在,已忽略`);
154
- continue;
155
- }
156
- const _path = path.join(rootDir, file);
168
+ /** 全部apps路径 */
169
+ const apps = pkg?.karin?.apps && Array.isArray(pkg.karin.apps) ? pkg.karin.apps : [];
170
+ /** 暂时兼容旧版本 加入apps 计划在正式版本发布之前废弃 */
171
+ if (!apps.includes('apps'))
172
+ apps.push('apps');
173
+ for (const file of apps) {
174
+ const rootApps = path.join(root, file);
175
+ if (!common.exists(rootApps)) {
176
+ logger.debug(`[插件收集][${rootApps}] 路径不存在,已忽略`);
177
+ continue;
178
+ }
179
+ const dev = process.env.karin_app_mode === 'dev';
180
+ if (dev) {
157
181
  /** 热更新 */
158
- this.watchList.push({ plugin: dir, path: _path });
159
- list.push(...this.loadApps(dir, _path, `${"git" /* AppsType.Git */}`));
182
+ this.watchList.push({ plugin: dir, path: file });
160
183
  }
161
- continue;
184
+ list.push(...this.loadApps(dir, file, `${"git" /* AppsType.Git */}`));
162
185
  }
163
- /** 走到这说明有问题 打印错误信息 */
164
- logger.error(`[插件收集][${dir}] 加载错误,未检测到任何文件,请检查配置是否正确`);
165
- continue;
166
186
  }
167
- /** 全部apps路径 */
168
- const apps = pkg?.karin?.apps && Array.isArray(pkg.karin.apps) ? pkg.karin.apps : [];
169
- /** 暂时兼容旧版本 加入apps 计划在正式版本发布之前废弃 */
170
- if (!apps.includes('apps'))
171
- apps.push('apps');
172
- for (const file of apps) {
173
- const rootApps = path.join(root, file);
174
- if (!common.exists(rootApps)) {
175
- logger.debug(`[插件收集][${rootApps}] 路径不存在,已忽略`);
176
- continue;
177
- }
178
- const dev = process.env.karin_app_mode === 'dev';
179
- if (dev) {
180
- /** 热更新 */
181
- this.watchList.push({ plugin: dir, path: file });
182
- }
183
- list.push(...this.loadApps(dir, file, `${"git" /* AppsType.Git */}`));
187
+ catch (error) {
188
+ logger.error(`[插件收集][${dir}] 加载错误`);
189
+ logger.error(error);
184
190
  }
185
191
  }
186
192
  return list;
@@ -34,3 +34,4 @@ export default class LevelDB extends Level {
34
34
  }
35
35
  }
36
36
  export const level = new LevelDB();
37
+ level.open();
@@ -1,5 +1,6 @@
1
1
  export default class RedisLevel {
2
2
  #private;
3
+ /** 唯一标识符 用于区分不同的数据库 */
3
4
  id: string;
4
5
  constructor();
5
6
  /**
@@ -1,23 +1,16 @@
1
1
  import { Level } from 'level';
2
2
  export default class RedisLevel {
3
3
  #level;
4
+ /** 过期时间映射表 */
4
5
  #expireMap;
6
+ /** 唯一标识符 用于区分不同的数据库 */
5
7
  id;
6
8
  constructor() {
7
9
  const path = process.cwd() + '/data/db/RedisLevel';
8
10
  this.#level = new Level(path, { valueEncoding: 'json' });
9
- /**
10
- * @type {'RedisLevel'} 唯一标识符 用于区分不同的数据库
11
- */
11
+ this.#level.open();
12
12
  this.id = 'RedisLevel';
13
- /**
14
- * 过期时间映射表
15
- * @type {Map<string, number>} 键: 值 (过期时间)
16
- */
17
13
  this.#expireMap = new Map();
18
- /**
19
- * 开启过期时间处理
20
- */
21
14
  this.#expireHandle();
22
15
  }
23
16
  /**
@@ -11,7 +11,7 @@ export interface NpmInfo {
11
11
  /**
12
12
  * 常用方法
13
13
  */
14
- declare class Common {
14
+ export declare class Common {
15
15
  streamPipeline: (stream1: Readable, stream2: fs.WriteStream) => Promise<void>;
16
16
  constructor();
17
17
  /**
@@ -71,18 +71,25 @@ declare class Common {
71
71
  exists(path: string): boolean;
72
72
  /**
73
73
  * - 解析json文件
74
+ * @param file - 文件路径
75
+ * @param isThrow - 解析失败是否抛出错误
74
76
  */
75
- readJson(file: string): any;
77
+ readJson(file: string, isThrow?: boolean): any;
76
78
  /**
77
79
  * - 写入json文件
80
+ * @param file - 文件路径
81
+ * @param data - 要写入的数据
78
82
  */
79
83
  writeJson(file: string, data: any): boolean;
80
84
  /**
81
85
  * - 解析yaml文件
86
+ * @param file - 文件路径
82
87
  */
83
88
  readYaml(file: string): any;
84
89
  /**
85
90
  * - 写入yaml文件
91
+ * @param file - 文件路径
92
+ * @param data - 要写入的数据
86
93
  */
87
94
  writeYaml(file: string, data: any): boolean;
88
95
  /**
@@ -187,6 +194,16 @@ declare class Common {
187
194
  * 获取运行时间
188
195
  */
189
196
  uptime(): string;
197
+ /**
198
+ * 传入一个时间戳
199
+ * 返回距今已过去的时间
200
+ * @param time - 时间戳
201
+ *
202
+ * @example
203
+ * common.formatTime(1620000000)
204
+ * // -> '18天'
205
+ */
206
+ formatTime(time: number): string;
190
207
  /**
191
208
  * 构建消息体日志
192
209
  * @param - 消息体
@@ -217,4 +234,3 @@ declare class Common {
217
234
  * 常用方法
218
235
  */
219
236
  export declare const common: Common;
220
- export {};
@@ -11,7 +11,7 @@ import { logger, segment, YamlEditor } from '../../utils/index.js';
11
11
  /**
12
12
  * 常用方法
13
13
  */
14
- class Common {
14
+ export class Common {
15
15
  streamPipeline;
16
16
  constructor() {
17
17
  this.streamPipeline = promisify(pipeline);
@@ -143,18 +143,24 @@ class Common {
143
143
  }
144
144
  /**
145
145
  * - 解析json文件
146
+ * @param file - 文件路径
147
+ * @param isThrow - 解析失败是否抛出错误
146
148
  */
147
- readJson(file) {
149
+ readJson(file, isThrow = false) {
148
150
  try {
149
151
  return JSON.parse(fs.readFileSync(file, 'utf8'));
150
152
  }
151
153
  catch (error) {
152
- logger.error('[common] 读取json文件错误:' + error);
154
+ logger.debug(`[common][error] 读取json文件错误: ${file} ` + error);
155
+ if (isThrow)
156
+ throw error;
153
157
  return null;
154
158
  }
155
159
  }
156
160
  /**
157
161
  * - 写入json文件
162
+ * @param file - 文件路径
163
+ * @param data - 要写入的数据
158
164
  */
159
165
  writeJson(file, data) {
160
166
  try {
@@ -168,6 +174,7 @@ class Common {
168
174
  }
169
175
  /**
170
176
  * - 解析yaml文件
177
+ * @param file - 文件路径
171
178
  */
172
179
  readYaml(file) {
173
180
  try {
@@ -180,6 +187,8 @@ class Common {
180
187
  }
181
188
  /**
182
189
  * - 写入yaml文件
190
+ * @param file - 文件路径
191
+ * @param data - 要写入的数据
183
192
  */
184
193
  writeYaml(file, data) {
185
194
  try {
@@ -489,6 +498,27 @@ class Common {
489
498
  const parts = [day ? `${day}天` : '', hour ? `${hour}小时` : '', min ? `${min}分钟` : '', !day && sec ? `${sec}秒` : ''];
490
499
  return parts.filter(Boolean).join('');
491
500
  }
501
+ /**
502
+ * 传入一个时间戳
503
+ * 返回距今已过去的时间
504
+ * @param time - 时间戳
505
+ *
506
+ * @example
507
+ * common.formatTime(1620000000)
508
+ * // -> '18天'
509
+ */
510
+ formatTime(time) {
511
+ /** 判断是几位时间戳 进行对应处理 */
512
+ time = time.toString().length === 10 ? time * 1000 : time;
513
+ /** 减去当前时间 */
514
+ time = Math.floor((Date.now() - time) / 1000);
515
+ const day = Math.floor(time / 86400);
516
+ const hour = Math.floor((time % 86400) / 3600);
517
+ const min = Math.floor((time % 3600) / 60);
518
+ const sec = Math.floor(time % 60);
519
+ const parts = [day ? `${day}天` : '', hour ? `${hour}小时` : '', min ? `${min}分钟` : '', !day && sec ? `${sec}秒` : ''];
520
+ return parts.filter(Boolean).join('');
521
+ }
492
522
  /**
493
523
  * 构建消息体日志
494
524
  * @param - 消息体
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "0.11.11",
3
+ "version": "0.11.13",
4
4
  "private": false,
5
5
  "description": "基于 Kritor 进行开发的nodejs机器人框架",
6
6
  "homepage": "https://github.com/KarinJS/Karin",