node-karin 0.11.12 → 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;
@@ -71,18 +71,25 @@ export 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
  /**
@@ -143,18 +143,24 @@ export 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 @@ export 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 @@ export 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 {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "0.11.12",
3
+ "version": "0.11.13",
4
4
  "private": false,
5
5
  "description": "基于 Kritor 进行开发的nodejs机器人框架",
6
6
  "homepage": "https://github.com/KarinJS/Karin",