@kotori-bot/loader 1.1.0 → 1.2.0

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.
@@ -0,0 +1,4 @@
1
+ export declare const DEV_FILE = ".ts";
2
+ export declare const BUILD_FILE = ".js";
3
+ export declare const DEV_IMPORT = "./src/index.ts";
4
+ export declare const DEV_CODE_DIRS = "./src/";
@@ -0,0 +1,25 @@
1
+ import { BaseDir } from '@kotori-bot/core';
2
+ export declare const isDev: () => boolean;
3
+ export declare const CONFIG_FILE: () => "kotori.dev.yml" | "kotori.yml";
4
+ export declare const getBaseDir: () => {
5
+ root: any;
6
+ modules: string;
7
+ };
8
+ export declare const getGlobalConfig: (baseDir: BaseDir) => import("@kotori-bot/core").ObjectParserInfer<{
9
+ global: import("@kotori-bot/core").IntersectionParser<[import("@kotori-bot/core").ObjectParser<{
10
+ dirs: import("@kotori-bot/core").ArrayParser<import("@kotori-bot/core").StringParser>;
11
+ }>, import("@kotori-bot/core").ObjectParser<{
12
+ lang: import("@kotori-bot/core").UnionParser<[import("@kotori-bot/core").UnionParser<[import("@kotori-bot/core").LiteralParser<"en_US">, import("@kotori-bot/core").LiteralParser<"ja_JP">]>, import("@kotori-bot/core").UnionParser<[import("@kotori-bot/core").LiteralParser<"zh_CN">, import("@kotori-bot/core").LiteralParser<"zh_TW">]>]>;
13
+ 'command-prefix': import("@kotori-bot/core").StringParser;
14
+ }>]>;
15
+ adapter: import("@kotori-bot/core").Parser<import("@kotori-bot/core").IndexObject<import("@kotori-bot/core").ObjectParserInfer<{
16
+ extends: import("@kotori-bot/core").StringParser;
17
+ master: import("@kotori-bot/core").UnionParser<[import("@kotori-bot/core").NumberParser, import("@kotori-bot/core").StringParser]>;
18
+ }> & import("@kotori-bot/core").ObjectParserInfer<{
19
+ lang: import("@kotori-bot/core").UnionParser<[import("@kotori-bot/core").UnionParser<[import("@kotori-bot/core").LiteralParser<"en_US">, import("@kotori-bot/core").LiteralParser<"ja_JP">]>, import("@kotori-bot/core").UnionParser<[import("@kotori-bot/core").LiteralParser<"zh_CN">, import("@kotori-bot/core").LiteralParser<"zh_TW">]>]>;
20
+ 'command-prefix': import("@kotori-bot/core").StringParser;
21
+ }>>>;
22
+ plugin: import("@kotori-bot/core").Parser<import("@kotori-bot/core").IndexObject<import("@kotori-bot/core").ObjectParserInfer<{
23
+ priority: import("@kotori-bot/core").NumberParser;
24
+ }>>>;
25
+ }>;
package/lib/global.js CHANGED
@@ -3,17 +3,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.globalConfig = exports.baseDir = void 0;
6
+ exports.getGlobalConfig = exports.getBaseDir = exports.CONFIG_FILE = exports.isDev = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
- const kotori_bot_1 = require("kotori-bot");
10
- const CONFIG_FILE = 'kotori.yml';
11
- exports.baseDir = (() => {
9
+ const core_1 = require("@kotori-bot/core");
10
+ const isDev = () => globalThis.env_mode === 'dev';
11
+ exports.isDev = isDev;
12
+ const CONFIG_FILE = () => ((0, exports.isDev)() ? 'kotori.dev.yml' : 'kotori.yml');
13
+ exports.CONFIG_FILE = CONFIG_FILE;
14
+ const getBaseDir = () => {
15
+ const { env_dir: envDir } = globalThis;
16
+ if (envDir) {
17
+ return {
18
+ root: envDir,
19
+ modules: path_1.default.join(envDir, 'modules'),
20
+ };
21
+ }
12
22
  let root = path_1.default.resolve(__dirname, '..').replace('loader', 'kotori');
13
23
  let count = 0;
14
- while (!fs_1.default.existsSync(path_1.default.join(root, CONFIG_FILE))) {
24
+ while (!fs_1.default.existsSync(path_1.default.join(root, (0, exports.CONFIG_FILE)()))) {
15
25
  if (count > 5)
16
- throw new kotori_bot_1.CoreError(`cannot find kotori-bot global ${CONFIG_FILE}`);
26
+ throw new core_1.CoreError(`cannot find kotori-bot global ${exports.CONFIG_FILE}`);
17
27
  root = path_1.default.join(root, '..');
18
28
  count += 1;
19
29
  }
@@ -21,20 +31,23 @@ exports.baseDir = (() => {
21
31
  root,
22
32
  modules: path_1.default.join(root, 'modules'),
23
33
  };
24
- })();
25
- exports.globalConfig = (() => {
26
- const data = (0, kotori_bot_1.loadConfig)(path_1.default.join(exports.baseDir.root, CONFIG_FILE), 'yaml');
34
+ };
35
+ exports.getBaseDir = getBaseDir;
36
+ const getGlobalConfig = (baseDir) => {
37
+ const data = (0, core_1.loadConfig)(path_1.default.join(baseDir.root, (0, exports.CONFIG_FILE)()), 'yaml');
27
38
  const isExistsGlobal = data && typeof data === 'object' && data.global && typeof data.global === 'object';
28
39
  try {
29
40
  if (!isExistsGlobal)
30
- throw new kotori_bot_1.TsuError('en_US', 'array_error');
31
- const lang = data.global.lang ? kotori_bot_1.localeTypeSchema.parse(data.global.lang) : undefined;
41
+ throw new core_1.TsuError('en_US', 'array_error');
42
+ const lang = data.global.lang ? core_1.localeTypeSchema.parse(data.global.lang) : undefined;
32
43
  const commandPrefix = data.global['command-prefix'] ? data.global['command-prefix'] : undefined;
33
- return (0, kotori_bot_1.globalConfigSchemaController)(lang, commandPrefix).parse(data);
44
+ return (0, core_1.globalConfigSchemaController)(lang, commandPrefix).parse(data);
34
45
  }
35
46
  catch (err) {
36
- if (!(err instanceof kotori_bot_1.TsuError))
47
+ if (!(err instanceof core_1.TsuError))
37
48
  throw err;
38
- throw new kotori_bot_1.CoreError(`kotori-bot global ${CONFIG_FILE} format error`);
49
+ // process.exit(1);
50
+ throw new core_1.CoreError(`kotori-bot global ${exports.CONFIG_FILE} format error: ${err.message}`);
39
51
  }
40
- })();
52
+ };
53
+ exports.getGlobalConfig = getGlobalConfig;
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import loader from './loader';
2
+ export default loader;
package/lib/index.js CHANGED
@@ -8,8 +8,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
8
8
  * @Blog: https://hotaru.icu
9
9
  * @Date: 2023-10-29 16:20:51
10
10
  * @LastEditors: Hotaru biyuehuya@gmail.com
11
- * @LastEditTime: 2023-11-05 16:29:15
11
+ * @LastEditTime: 2023-12-30 17:32:51
12
12
  */
13
- const main_1 = __importDefault(require("./main"));
14
- const loader = new main_1.default();
15
- loader.run();
13
+ const loader_1 = __importDefault(require("./loader"));
14
+ exports.default = loader_1.default;
@@ -0,0 +1,13 @@
1
+ import { ContextInstance } from '@kotori-bot/core';
2
+ declare class Main extends ContextInstance {
3
+ private ctx;
4
+ constructor();
5
+ run(): void;
6
+ private handleError;
7
+ private catchError;
8
+ private listenMessage;
9
+ private loadAllModule;
10
+ private loadAllAdapter;
11
+ private checkUpdate;
12
+ }
13
+ export default Main;
@@ -8,26 +8,30 @@ Object.defineProperty(exports, "__esModule", { value: true });
8
8
  * @Blog: https://hotaru.icu
9
9
  * @Date: 2023-06-24 15:12:55
10
10
  * @LastEditors: Hotaru biyuehuya@gmail.com
11
- * @LastEditTime: 2023-12-17 15:54:48
11
+ * @LastEditTime: 2023-12-30 19:12:43
12
12
  */
13
- const kotori_bot_1 = require("kotori-bot");
13
+ const core_1 = require("@kotori-bot/core");
14
14
  const modules_1 = __importDefault(require("./modules"));
15
15
  const global_1 = require("./global");
16
16
  const log_1 = __importDefault(require("./log"));
17
- const isDev = process.env.NODE_ENV === 'dev';
18
- const kotoriConfig = {
19
- baseDir: global_1.baseDir,
20
- config: global_1.globalConfig,
21
- options: {
22
- env: isDev ? 'dev' : 'build',
23
- },
17
+ const kotoriConfig = () => {
18
+ let result = {};
19
+ const baseDir = (0, global_1.getBaseDir)();
20
+ result = {
21
+ baseDir,
22
+ config: (0, global_1.getGlobalConfig)(baseDir),
23
+ options: {
24
+ env: (0, global_1.isDev)() ? 'dev' : 'build',
25
+ },
26
+ };
27
+ return result;
24
28
  };
25
- class Main extends kotori_bot_1.ContextInstance {
29
+ class Main extends core_1.ContextInstance {
26
30
  ctx;
27
31
  constructor() {
28
- kotori_bot_1.ContextInstance.setInstance(new modules_1.default(kotoriConfig));
29
32
  super();
30
- this.ctx = kotori_bot_1.ContextInstance.getInstance();
33
+ core_1.ContextInstance.setInstance(new modules_1.default(kotoriConfig()));
34
+ this.ctx = core_1.ContextInstance.getInstance();
31
35
  // 静态类型继续居然她妈是隔离的
32
36
  }
33
37
  run() {
@@ -38,15 +42,17 @@ class Main extends kotori_bot_1.ContextInstance {
38
42
  this.checkUpdate();
39
43
  }
40
44
  handleError(err, prefix) {
41
- const isKotoriError = err instanceof kotori_bot_1.KotoriError;
45
+ const isKotoriError = err instanceof core_1.KotoriError;
42
46
  if (!isKotoriError) {
43
47
  this.ctx.logger.tag(prefix, 'default', prefix === 'UCE' ? 'cyanBG' : 'greenBG').error(err);
44
48
  return;
45
49
  }
46
50
  this.ctx.logger
47
51
  .tag(err.name.split('Error')[0].toUpperCase(), 'yellow', 'default')[err.level === 'normal' ? 'error' : err.level](err.message, err.stack);
48
- if (err.name === 'CoreError')
49
- process.emit('SIGINT');
52
+ if (err.name !== 'CoreError')
53
+ return;
54
+ this.ctx.logger.tag('CORE', 'black', 'red').error(err.message);
55
+ process.emit('SIGINT');
50
56
  }
51
57
  catchError() {
52
58
  process.on('uncaughtExceptionMonitor', err => this.handleError(err, 'UCE'));
@@ -78,7 +84,7 @@ class Main extends kotori_bot_1.ContextInstance {
78
84
  }
79
85
  loadAllModule() {
80
86
  this.ctx.moduleAll();
81
- if (isDev)
87
+ if ((0, global_1.isDev)())
82
88
  this.ctx.watchFile();
83
89
  }
84
90
  loadAllAdapter() {
@@ -110,7 +116,7 @@ class Main extends kotori_bot_1.ContextInstance {
110
116
  const res = await this.ctx.http
111
117
  .get('https://hotaru.icu/api/agent/?url=https://raw.githubusercontent.com/kotorijs/kotori/master/packages/kotori/package.json')
112
118
  .catch(() => this.ctx.logger.error('Get update failed, please check your network'));
113
- if (!res || !kotori_bot_1.Tsu.Object({ version: kotori_bot_1.Tsu.String() }).check(res)) {
119
+ if (!res || !core_1.Tsu.Object({ version: core_1.Tsu.String() }).check(res)) {
114
120
  this.ctx.logger.error(`Detection update failed`);
115
121
  }
116
122
  else if (version === res.version) {
package/lib/log.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ import type { Context, PackageInfo } from '@kotori-bot/core';
2
+ export declare function loadInfo(info: PackageInfo, ctx: Context): void;
3
+ export default loadInfo;
@@ -0,0 +1,18 @@
1
+ import { Context } from '@kotori-bot/core';
2
+ declare module '@kotori-bot/core' {
3
+ interface Context {
4
+ readonly moduleAll?: () => void;
5
+ readonly watchFile?: () => void;
6
+ }
7
+ }
8
+ export declare class Modules extends Context {
9
+ private isDev;
10
+ private readonly moduleRootDir;
11
+ private getDirFiles;
12
+ private getModuleRootDir;
13
+ private getModuleList;
14
+ private moduleQuick;
15
+ readonly moduleAll: () => Promise<void>;
16
+ readonly watchFile: () => Promise<void>;
17
+ }
18
+ export default Modules;
package/lib/modules.js CHANGED
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Modules = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const path_1 = __importDefault(require("path"));
9
- const kotori_bot_1 = require("kotori-bot");
9
+ const core_1 = require("@kotori-bot/core");
10
10
  const consts_1 = require("./consts");
11
- class Modules extends kotori_bot_1.Context {
11
+ class Modules extends core_1.Context {
12
12
  isDev = this.options.env === 'dev';
13
13
  moduleRootDir = [];
14
14
  getDirFiles(rootDir) {
@@ -26,7 +26,7 @@ class Modules extends kotori_bot_1.Context {
26
26
  return list;
27
27
  }
28
28
  getModuleRootDir() {
29
- Object.assign(this.config.global.dirs, this.baseDir.modules).forEach(dir => {
29
+ Object.assign(this.config.global.dirs, [this.baseDir.modules]).forEach(dir => {
30
30
  if (fs_1.default.existsSync(dir) && fs_1.default.statSync(dir).isDirectory())
31
31
  this.moduleRootDir.push(dir);
32
32
  });
@@ -37,6 +37,8 @@ class Modules extends kotori_bot_1.Context {
37
37
  const dir = path_1.default.join(rootDir, fileName);
38
38
  if (!fs_1.default.statSync(dir).isDirectory())
39
39
  return;
40
+ if (rootDir !== this.baseDir.modules && fileName.startsWith(core_1.PLUGIN_PREFIX))
41
+ return;
40
42
  const packagePath = path_1.default.join(dir, 'package.json');
41
43
  let packageJson;
42
44
  if (!fs_1.default.existsSync(packagePath))
@@ -45,16 +47,15 @@ class Modules extends kotori_bot_1.Context {
45
47
  packageJson = JSON.parse(fs_1.default.readFileSync(packagePath).toString());
46
48
  }
47
49
  catch {
48
- throw new kotori_bot_1.DevError(`illegal package.json ${packagePath}`);
50
+ throw new core_1.DevError(`illegal package.json ${packagePath}`);
49
51
  }
50
- if (!kotori_bot_1.ModulePackageSchema.check(packageJson)) {
51
- if (rootDir !== this.baseDir.modules)
52
- return;
53
- throw new kotori_bot_1.DevError(`package.json format error ${packagePath}`);
52
+ const result = core_1.ModulePackageSchema.parseSafe(packageJson);
53
+ if (!result.value && rootDir === this.baseDir.modules) {
54
+ throw new core_1.DevError(`package.json format error ${packagePath}: ${result.error.message}`);
54
55
  }
55
56
  const mainPath = path_1.default.join(dir, this.isDev ? consts_1.DEV_IMPORT : packageJson.main);
56
57
  if (!fs_1.default.existsSync(mainPath))
57
- throw new kotori_bot_1.DevError(`cannot find ${mainPath}`);
58
+ throw new core_1.DevError(`cannot find ${mainPath}`);
58
59
  const codeDirs = path_1.default.join(dir, this.isDev ? consts_1.DEV_CODE_DIRS : path_1.default.dirname(packageJson.main));
59
60
  this.moduleStack.push({
60
61
  package: packageJson,
@@ -64,14 +65,14 @@ class Modules extends kotori_bot_1.Context {
64
65
  });
65
66
  }
66
67
  moduleQuick(moduleData) {
67
- return this.use(moduleData, this, this.config.plugin[(0, kotori_bot_1.stringRightSplit)(moduleData.package.name, kotori_bot_1.PLUGIN_PREFIX)] ?? {});
68
+ return this.use(moduleData, this, this.config.plugin[(0, core_1.stringRightSplit)(moduleData.package.name, core_1.PLUGIN_PREFIX)] ?? {});
68
69
  }
69
70
  moduleAll = async () => {
70
71
  this.getModuleRootDir();
71
72
  this.moduleRootDir.forEach(dir => {
72
73
  this.getModuleList(dir);
73
74
  });
74
- const array = this.moduleStack.filter(data => data.package.name.startsWith(kotori_bot_1.OFFICIAL_MODULES_SCOPE));
75
+ const array = this.moduleStack.filter(data => data.package.name.startsWith(core_1.OFFICIAL_MODULES_SCOPE));
75
76
  array.push(...this.moduleStack.filter(data => !array.includes(data)));
76
77
  array.forEach(moduleData => this.moduleQuick(moduleData));
77
78
  };
package/package.json CHANGED
@@ -1,27 +1,18 @@
1
1
  {
2
2
  "name": "@kotori-bot/loader",
3
- "version": "1.1.0",
3
+ "version": "v1.2.0",
4
4
  "description": "Loader For KotoriBot",
5
5
  "license": "GPL-3.0",
6
+ "main": "lib/index.js",
6
7
  "author": "Hotaru <biyuehuya@gmail.com>",
7
8
  "dependencies": {
8
- "cross-env": "^7.0.3",
9
- "kotori-bot": "^1.0.0"
9
+ "@kotori-bot/core": "^v1.1.0"
10
10
  },
11
11
  "files": [
12
12
  "lib"
13
13
  ],
14
14
  "devDependencies": {
15
15
  "ts-node": "^10.9.2",
16
- "tsconfig-paths": "^4.2.0",
17
16
  "typescript": "^5.1.3"
18
- },
19
- "publishConfig": {
20
- "access": "public"
21
- },
22
- "scripts": {
23
- "dev": "cross-env NODE_ENV=dev ts-node --esm -r tsconfig-paths/register ./src/index.ts --no-signale",
24
- "start": "cross-env NODE_ENV=build node ./dist/index.js",
25
- "build": "tsc --build"
26
17
  }
27
18
  }