@kotori-bot/loader 1.6.0 → 1.6.1
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/class/loader.js +68 -467
- package/lib/class/runner.js +72 -157
- package/lib/constants.js +15 -17
- package/lib/decorators/index.js +22 -118
- package/lib/decorators/plugin.js +4 -6
- package/lib/decorators/utils.js +6 -7
- package/lib/index.js +10 -807
- package/lib/service/adapter.js +2 -4
- package/lib/service/database.js +2 -4
- package/lib/service/file.js +5 -7
- package/lib/service/server.js +5 -7
- package/lib/types/index.js +8 -4
- package/lib/types/internal.js +2 -2
- package/lib/types/server.js +2 -4
- package/lib/utils/log.js +2 -4
- package/lib/utils/logger.js +4 -6
- package/package.json +3 -3
package/lib/class/runner.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
/**
|
|
3
3
|
* @Package @kotori-bot/loader
|
|
4
|
-
* @Version 1.6.0
|
|
4
|
+
* @Version 1.6.0
|
|
5
5
|
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
6
|
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
7
|
* @License GPL-3.0
|
|
8
8
|
* @Link https://github.com/kotorijs/kotori
|
|
9
|
-
* @Date 2024/6/
|
|
9
|
+
* @Date 2024/6/7 11:22:23
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
"use strict";
|
|
@@ -37,8 +37,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
37
37
|
mod
|
|
38
38
|
));
|
|
39
39
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
40
|
-
|
|
41
|
-
// src/class/runner.ts
|
|
42
40
|
var runner_exports = {};
|
|
43
41
|
__export(runner_exports, {
|
|
44
42
|
Runner: () => Runner,
|
|
@@ -46,118 +44,35 @@ __export(runner_exports, {
|
|
|
46
44
|
localeTypeSchema: () => localeTypeSchema
|
|
47
45
|
});
|
|
48
46
|
module.exports = __toCommonJS(runner_exports);
|
|
49
|
-
var
|
|
50
|
-
var
|
|
51
|
-
var import_core4 = require("@kotori-bot/core");
|
|
52
|
-
var import_logger3 = require("@kotori-bot/logger");
|
|
53
|
-
|
|
54
|
-
// src/constants.ts
|
|
55
|
-
var DEV_FILE = ".ts";
|
|
56
|
-
var BUILD_FILE = ".js";
|
|
57
|
-
var DEV_CODE_DIRS = "./src/";
|
|
58
|
-
var DEV_IMPORT = `${DEV_CODE_DIRS}index.ts`;
|
|
59
|
-
var DEV_MODE = "dev";
|
|
60
|
-
var DEV_SOURCE_MODE = "dev-source";
|
|
61
|
-
var CORE_MODULES = [
|
|
62
|
-
"@kotori-bot/kotori-plugin-core",
|
|
63
|
-
"@kotori-bot/kotori-plugin-i18n-command",
|
|
64
|
-
"@kotori-bot/kotori-plugin-filter"
|
|
65
|
-
// '@kotori-bot/kotori-plugin-webui'
|
|
66
|
-
];
|
|
67
|
-
|
|
68
|
-
// src/utils/logger.ts
|
|
69
|
-
var import_logger = require("@kotori-bot/logger");
|
|
70
|
-
var KotoriLogger = class extends import_logger.Logger {
|
|
71
|
-
constructor(optionsSelf, ctx) {
|
|
72
|
-
super(optionsSelf);
|
|
73
|
-
this.optionsSelf = optionsSelf;
|
|
74
|
-
this.ctx = ctx;
|
|
75
|
-
}
|
|
76
|
-
setLabel() {
|
|
77
|
-
const origin = Object.create(this.optionsSelf.label);
|
|
78
|
-
const label = this.ctx.identity ? [this.ctx.identity, ...this.optionsSelf.label] : this.optionsSelf.label;
|
|
79
|
-
this[/* @__PURE__ */ (() => "options")()].label = label;
|
|
80
|
-
return () => {
|
|
81
|
-
this[/* @__PURE__ */ (() => "options")()].label = origin;
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
fatal(...args) {
|
|
85
|
-
const dispose = this.setLabel();
|
|
86
|
-
super.fatal(...args);
|
|
87
|
-
dispose();
|
|
88
|
-
}
|
|
89
|
-
error(...args) {
|
|
90
|
-
const dispose = this.setLabel();
|
|
91
|
-
super.error(...args);
|
|
92
|
-
dispose();
|
|
93
|
-
}
|
|
94
|
-
warn(...args) {
|
|
95
|
-
const dispose = this.setLabel();
|
|
96
|
-
super.warn(...args);
|
|
97
|
-
dispose();
|
|
98
|
-
}
|
|
99
|
-
info(...args) {
|
|
100
|
-
const dispose = this.setLabel();
|
|
101
|
-
super.info(...args);
|
|
102
|
-
dispose();
|
|
103
|
-
}
|
|
104
|
-
record(...args) {
|
|
105
|
-
const dispose = this.setLabel();
|
|
106
|
-
super.record(...args);
|
|
107
|
-
dispose();
|
|
108
|
-
}
|
|
109
|
-
debug(...args) {
|
|
110
|
-
const dispose = this.setLabel();
|
|
111
|
-
super.debug(...args);
|
|
112
|
-
dispose();
|
|
113
|
-
}
|
|
114
|
-
trace(...args) {
|
|
115
|
-
const dispose = this.setLabel();
|
|
116
|
-
super.trace(...args);
|
|
117
|
-
dispose();
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
var logger_default = KotoriLogger;
|
|
121
|
-
|
|
122
|
-
// src/class/loader.ts
|
|
123
|
-
var import_core3 = require("@kotori-bot/core");
|
|
124
|
-
var import_node_path2 = __toESM(require("path"));
|
|
125
|
-
var import_node_fs = __toESM(require("fs"));
|
|
126
|
-
var import_logger2 = __toESM(require("@kotori-bot/logger"));
|
|
127
|
-
|
|
128
|
-
// src/service/server.ts
|
|
47
|
+
var import_node_fs = __toESM(require("node:fs"));
|
|
48
|
+
var import_node_path = __toESM(require("node:path"));
|
|
129
49
|
var import_core = require("@kotori-bot/core");
|
|
130
|
-
var
|
|
131
|
-
var
|
|
132
|
-
var
|
|
133
|
-
var
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
// src/class/runner.ts
|
|
140
|
-
var localeTypeSchema = import_core4.Tsu.Union([
|
|
141
|
-
import_core4.Tsu.Union([import_core4.Tsu.Literal("en_US"), import_core4.Tsu.Literal("ja_JP")]),
|
|
142
|
-
import_core4.Tsu.Union([import_core4.Tsu.Literal("zh_TW"), import_core4.Tsu.Any()])
|
|
50
|
+
var import_logger = require("@kotori-bot/logger");
|
|
51
|
+
var import_constants = require("../constants");
|
|
52
|
+
var import_internal = require("../types/internal");
|
|
53
|
+
var import_logger2 = __toESM(require("../utils/logger"));
|
|
54
|
+
var import_loader = require("./loader");
|
|
55
|
+
const localeTypeSchema = import_core.Tsu.Union([
|
|
56
|
+
import_core.Tsu.Union([import_core.Tsu.Literal("en_US"), import_core.Tsu.Literal("ja_JP")]),
|
|
57
|
+
import_core.Tsu.Union([import_core.Tsu.Literal("zh_TW"), import_core.Tsu.Any()])
|
|
143
58
|
]);
|
|
144
|
-
|
|
145
|
-
name:
|
|
146
|
-
version:
|
|
147
|
-
description:
|
|
148
|
-
main:
|
|
149
|
-
license:
|
|
150
|
-
keywords:
|
|
59
|
+
const modulePackageSchema = import_core.Tsu.Object({
|
|
60
|
+
name: import_core.Tsu.String().regexp(/kotori-plugin-[a-z]([a-z,0-9]{2,13})\b/),
|
|
61
|
+
version: import_core.Tsu.String(),
|
|
62
|
+
description: import_core.Tsu.String(),
|
|
63
|
+
main: import_core.Tsu.String(),
|
|
64
|
+
license: import_core.Tsu.Literal("GPL-3.0"),
|
|
65
|
+
keywords: import_core.Tsu.Custom(
|
|
151
66
|
(val) => Array.isArray(val) && val.includes("kotori") && val.includes("chatbot") && val.includes("kotori-plugin")
|
|
152
67
|
),
|
|
153
|
-
author:
|
|
154
|
-
peerDependencies:
|
|
155
|
-
"kotori-bot":
|
|
68
|
+
author: import_core.Tsu.Union([import_core.Tsu.String(), import_core.Tsu.Array(import_core.Tsu.String())]),
|
|
69
|
+
peerDependencies: import_core.Tsu.Object({
|
|
70
|
+
"kotori-bot": import_core.Tsu.String()
|
|
156
71
|
}),
|
|
157
|
-
kotori:
|
|
158
|
-
enforce:
|
|
159
|
-
meta:
|
|
160
|
-
language:
|
|
72
|
+
kotori: import_core.Tsu.Object({
|
|
73
|
+
enforce: import_core.Tsu.Union([import_core.Tsu.Literal("pre"), import_core.Tsu.Literal("post")]).optional(),
|
|
74
|
+
meta: import_core.Tsu.Object({
|
|
75
|
+
language: import_core.Tsu.Array(localeTypeSchema).default([])
|
|
161
76
|
}).default({ language: [] })
|
|
162
77
|
}).default({
|
|
163
78
|
enforce: void 0,
|
|
@@ -165,99 +80,99 @@ var modulePackageSchema = import_core4.Tsu.Object({
|
|
|
165
80
|
})
|
|
166
81
|
});
|
|
167
82
|
function moduleLoadOrder(pkg) {
|
|
168
|
-
if (CORE_MODULES.includes(pkg.name)) return 1;
|
|
169
|
-
if (pkg.name.includes(
|
|
170
|
-
if (pkg.name.includes(
|
|
83
|
+
if (import_constants.CORE_MODULES.includes(pkg.name)) return 1;
|
|
84
|
+
if (pkg.name.includes(import_core.DATABASE_PREFIX)) return 2;
|
|
85
|
+
if (pkg.name.includes(import_core.ADAPTER_PREFIX)) return 3;
|
|
171
86
|
if (pkg.kotori.enforce === "pre") return 4;
|
|
172
87
|
if (!pkg.kotori.enforce) return 5;
|
|
173
88
|
return 6;
|
|
174
89
|
}
|
|
175
|
-
|
|
90
|
+
class Runner {
|
|
176
91
|
baseDir;
|
|
177
92
|
options;
|
|
178
93
|
ctx;
|
|
179
94
|
isDev;
|
|
180
95
|
isSourceDev;
|
|
181
|
-
[
|
|
96
|
+
[import_core.Symbols.modules] = /* @__PURE__ */ new Map();
|
|
182
97
|
constructor(ctx, config) {
|
|
183
98
|
this.ctx = ctx;
|
|
184
99
|
this.baseDir = config.baseDir;
|
|
185
100
|
this.options = config.options;
|
|
186
|
-
this.isDev = this.options.mode.startsWith(DEV_MODE);
|
|
187
|
-
this.isSourceDev = this.options.mode === DEV_SOURCE_MODE;
|
|
101
|
+
this.isDev = this.options.mode.startsWith(import_constants.DEV_MODE);
|
|
102
|
+
this.isSourceDev = this.options.mode === import_constants.DEV_SOURCE_MODE;
|
|
188
103
|
const loggerOptions = {
|
|
189
104
|
level: this.ctx.config.global.level ?? config.level,
|
|
190
105
|
label: [],
|
|
191
106
|
transports: [
|
|
192
|
-
new
|
|
107
|
+
new import_logger.ConsoleTransport({
|
|
193
108
|
template: "<blue>%time%</blue> %level% (<bold>%pid%</bold>) %labels%: %msg%",
|
|
194
109
|
time: "M/D H:m:s"
|
|
195
110
|
}),
|
|
196
|
-
new
|
|
111
|
+
new import_logger.FileTransport({ dir: this.baseDir.logs, filter: (data) => data.level >= import_logger.LoggerLevel.WARN })
|
|
197
112
|
]
|
|
198
113
|
};
|
|
199
|
-
ctx.provide("logger", new
|
|
114
|
+
ctx.provide("logger", new import_logger2.default(loggerOptions, this.ctx));
|
|
200
115
|
ctx.inject("logger");
|
|
201
116
|
}
|
|
202
117
|
getDirFiles(rootDir) {
|
|
203
|
-
const files =
|
|
118
|
+
const files = import_node_fs.default.readdirSync(rootDir);
|
|
204
119
|
const list = [];
|
|
205
120
|
files.forEach((fileName) => {
|
|
206
|
-
const file =
|
|
207
|
-
if (
|
|
121
|
+
const file = import_node_path.default.join(rootDir, fileName);
|
|
122
|
+
if (import_node_fs.default.statSync(file).isDirectory()) {
|
|
208
123
|
list.push(...this.getDirFiles(file));
|
|
209
124
|
}
|
|
210
|
-
if (
|
|
211
|
-
list.push(
|
|
125
|
+
if (import_node_path.default.parse(file).ext !== (this.isSourceDev ? import_constants.DEV_FILE : import_constants.BUILD_FILE)) return;
|
|
126
|
+
list.push(import_node_path.default.resolve(file));
|
|
212
127
|
});
|
|
213
128
|
return list;
|
|
214
129
|
}
|
|
215
130
|
getModuleRootDir() {
|
|
216
131
|
const moduleRootDir = [];
|
|
217
132
|
[
|
|
218
|
-
...this.ctx.config.global.dirs.map((dir) =>
|
|
133
|
+
...this.ctx.config.global.dirs.map((dir) => import_node_path.default.resolve(this.ctx.baseDir.root, dir)),
|
|
219
134
|
this.ctx.baseDir.modules
|
|
220
135
|
].forEach((dir) => {
|
|
221
|
-
if (
|
|
136
|
+
if (import_node_fs.default.existsSync(dir) && import_node_fs.default.statSync(dir).isDirectory()) moduleRootDir.push(dir);
|
|
222
137
|
});
|
|
223
138
|
return moduleRootDir;
|
|
224
139
|
}
|
|
225
140
|
async checkModuleFiles(rootDir, filename) {
|
|
226
|
-
const dir =
|
|
227
|
-
if (!
|
|
228
|
-
if (rootDir !== this.ctx.baseDir.modules && !filename.startsWith(
|
|
229
|
-
const packagePath =
|
|
141
|
+
const dir = import_node_path.default.join(rootDir, filename);
|
|
142
|
+
if (!import_node_fs.default.statSync(dir).isDirectory()) return;
|
|
143
|
+
if (rootDir !== this.ctx.baseDir.modules && !filename.startsWith(import_core.PLUGIN_PREFIX)) return;
|
|
144
|
+
const packagePath = import_node_path.default.join(dir, "package.json");
|
|
230
145
|
let pkg;
|
|
231
|
-
if (!
|
|
146
|
+
if (!import_node_fs.default.existsSync(packagePath)) return;
|
|
232
147
|
try {
|
|
233
|
-
pkg = JSON.parse(
|
|
148
|
+
pkg = JSON.parse(import_node_fs.default.readFileSync(packagePath).toString());
|
|
234
149
|
} catch {
|
|
235
|
-
throw new
|
|
150
|
+
throw new import_core.DevError(this.ctx.format("error.dev.package.illegal", [packagePath]));
|
|
236
151
|
}
|
|
237
152
|
const result = modulePackageSchema.parseSafe(pkg);
|
|
238
153
|
if (!result.value) {
|
|
239
154
|
if (rootDir !== this.ctx.baseDir.modules) return;
|
|
240
|
-
throw new
|
|
155
|
+
throw new import_core.DevError(this.ctx.format("error.dev.package.missing", [packagePath, result.error.message]));
|
|
241
156
|
}
|
|
242
157
|
pkg = result.data;
|
|
243
|
-
const devMode = this.isSourceDev && (0,
|
|
244
|
-
const main =
|
|
245
|
-
if (!
|
|
246
|
-
const dirs =
|
|
247
|
-
const files =
|
|
248
|
-
this[
|
|
158
|
+
const devMode = this.isSourceDev && (0, import_node_fs.existsSync)(import_node_path.default.resolve(dir, import_constants.DEV_IMPORT));
|
|
159
|
+
const main = import_node_path.default.resolve(dir, devMode ? import_constants.DEV_IMPORT : pkg.main);
|
|
160
|
+
if (!import_node_fs.default.existsSync(main)) throw new import_core.DevError(this.ctx.format("error.dev.main_file", [main]));
|
|
161
|
+
const dirs = import_node_path.default.join(dir, devMode ? import_constants.DEV_CODE_DIRS : import_node_path.default.dirname(pkg.main));
|
|
162
|
+
const files = import_node_fs.default.statSync(dirs).isDirectory() ? this.getDirFiles(dirs) : [];
|
|
163
|
+
this[import_core.Symbols.modules].set(pkg.name, [
|
|
249
164
|
{ pkg, files, main },
|
|
250
|
-
this.ctx.config.plugin[(0,
|
|
165
|
+
this.ctx.config.plugin[(0, import_core.stringRightSplit)(pkg.name, import_core.PLUGIN_PREFIX)] || {}
|
|
251
166
|
]);
|
|
252
167
|
}
|
|
253
168
|
getModuleList(rootDir) {
|
|
254
169
|
this.ctx.logger.trace("load dirs:", rootDir);
|
|
255
|
-
|
|
170
|
+
import_node_fs.default.readdirSync(rootDir).forEach(async (filename) => {
|
|
256
171
|
await this.checkModuleFiles(rootDir, filename);
|
|
257
172
|
});
|
|
258
173
|
}
|
|
259
174
|
loadLang(lang) {
|
|
260
|
-
if (lang) this.ctx.i18n.use((0,
|
|
175
|
+
if (lang) this.ctx.i18n.use((0, import_node_path.resolve)(...Array.isArray(lang) ? lang : [lang]));
|
|
261
176
|
}
|
|
262
177
|
loadEx(instance, origin) {
|
|
263
178
|
this.ctx.logger.trace("module:", instance, origin);
|
|
@@ -265,32 +180,32 @@ var Runner = class {
|
|
|
265
180
|
const parsed = (schema) => {
|
|
266
181
|
const result = schema.parseSafe(config);
|
|
267
182
|
if (!result.value)
|
|
268
|
-
throw new
|
|
183
|
+
throw new import_core.ModuleError(this.ctx.format("error.module.config", [pkg.name, result.error.message]));
|
|
269
184
|
return result.data;
|
|
270
185
|
};
|
|
271
186
|
const { main, pkg } = instance;
|
|
272
187
|
let obj = require(main);
|
|
273
188
|
let config = origin;
|
|
274
|
-
const adapterName = pkg.name.split(
|
|
189
|
+
const adapterName = pkg.name.split(import_core.ADAPTER_PREFIX)[1];
|
|
275
190
|
if (this.ctx.get("decorators")?.registers.includes(pkg.name)) {
|
|
276
191
|
this.ctx.emit("ready_module_decorators", pkg.name);
|
|
277
192
|
return;
|
|
278
193
|
}
|
|
279
|
-
if (
|
|
280
|
-
this.ctx[
|
|
194
|
+
if (import_core.Adapter.isPrototypeOf.call(import_core.Adapter, obj.default) && adapterName && (!obj.config || obj.config instanceof import_core.Parser)) {
|
|
195
|
+
this.ctx[import_core.Symbols.adapter].set(adapterName, [obj.default, obj.config]);
|
|
281
196
|
obj = {};
|
|
282
|
-
} else if (
|
|
197
|
+
} else if (import_core.Service.isPrototypeOf.call(import_core.Service, obj.default)) {
|
|
283
198
|
obj = {};
|
|
284
|
-
} else if (obj.config instanceof
|
|
199
|
+
} else if (obj.config instanceof import_core.Parser) {
|
|
285
200
|
config = parsed(obj.config);
|
|
286
201
|
}
|
|
287
202
|
if (obj.lang) this.loadLang(obj.lang);
|
|
288
203
|
if (obj.default) {
|
|
289
204
|
if (obj.default.lang) this.loadLang(obj.default.lang);
|
|
290
|
-
if (obj.default.config instanceof
|
|
205
|
+
if (obj.default.config instanceof import_core.Parser) config = parsed(obj.default.config);
|
|
291
206
|
} else if (obj.Main) {
|
|
292
207
|
if (obj.Main.lang) this.loadLang(obj.Main.lang);
|
|
293
|
-
if (obj.Main.config instanceof
|
|
208
|
+
if (obj.Main.config instanceof import_core.Parser) config = parsed(obj.Main.config);
|
|
294
209
|
}
|
|
295
210
|
this.ctx.load({ name: pkg.name, ...obj, config });
|
|
296
211
|
}
|
|
@@ -301,14 +216,14 @@ var Runner = class {
|
|
|
301
216
|
loadAll() {
|
|
302
217
|
this.getModuleRootDir().forEach((dir) => this.getModuleList(dir));
|
|
303
218
|
const modules = [];
|
|
304
|
-
this[
|
|
219
|
+
this[import_core.Symbols.modules].forEach((val) => modules.push(val));
|
|
305
220
|
modules.sort(([{ pkg: pkg1 }], [{ pkg: pkg2 }]) => moduleLoadOrder(pkg1) - moduleLoadOrder(pkg2)).forEach((el) => this.loadEx(...el));
|
|
306
221
|
if (this.isDev) this.watcher();
|
|
307
222
|
}
|
|
308
223
|
watcher() {
|
|
309
|
-
this[
|
|
224
|
+
this[import_core.Symbols.modules].forEach(
|
|
310
225
|
(data) => data[0].files.forEach(
|
|
311
|
-
(file) =>
|
|
226
|
+
(file) => import_node_fs.default.watchFile(file, async () => {
|
|
312
227
|
this.ctx.logger.debug(this.ctx.format("loader.debug.reload", [data[0].pkg.name]));
|
|
313
228
|
this.unloadEx(data[0]);
|
|
314
229
|
this.loadEx(...data);
|
|
@@ -316,7 +231,7 @@ var Runner = class {
|
|
|
316
231
|
)
|
|
317
232
|
);
|
|
318
233
|
}
|
|
319
|
-
}
|
|
234
|
+
}
|
|
320
235
|
var runner_default = Runner;
|
|
321
236
|
// Annotate the CommonJS export names for ESM import in node:
|
|
322
237
|
0 && (module.exports = {
|
package/lib/constants.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
/**
|
|
3
3
|
* @Package @kotori-bot/loader
|
|
4
|
-
* @Version 1.6.0
|
|
4
|
+
* @Version 1.6.0
|
|
5
5
|
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
6
|
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
7
|
* @License GPL-3.0
|
|
8
8
|
* @Link https://github.com/kotorijs/kotori
|
|
9
|
-
* @Date 2024/6/
|
|
9
|
+
* @Date 2024/6/7 11:22:23
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
"use strict";
|
|
@@ -27,8 +27,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
27
27
|
return to;
|
|
28
28
|
};
|
|
29
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
-
|
|
31
|
-
// src/constants.ts
|
|
32
30
|
var constants_exports = {};
|
|
33
31
|
__export(constants_exports, {
|
|
34
32
|
BUILD_CONFIG_NAME: () => BUILD_CONFIG_NAME,
|
|
@@ -46,19 +44,19 @@ __export(constants_exports, {
|
|
|
46
44
|
SUPPORTS_VERSION: () => SUPPORTS_VERSION
|
|
47
45
|
});
|
|
48
46
|
module.exports = __toCommonJS(constants_exports);
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
47
|
+
const DEV_FILE = ".ts";
|
|
48
|
+
const BUILD_FILE = ".js";
|
|
49
|
+
const DEV_CODE_DIRS = "./src/";
|
|
50
|
+
const DEV_IMPORT = `${DEV_CODE_DIRS}index.ts`;
|
|
51
|
+
const CONFIG_EXT = [".toml", ".yml", ".yaml", ".json"];
|
|
52
|
+
const BUILD_CONFIG_NAME = "kotori";
|
|
53
|
+
const DEV_CONFIG_NAME = "kotori.dev";
|
|
54
|
+
const SUPPORTS_VERSION = /(1\.1\.0)|(1\.2\.0)|(1\.3\.(.*))|(1\.4\.(.*))/;
|
|
55
|
+
const SUPPORTS_HALF_VERSION = /(x\.x\.(.*?))/;
|
|
56
|
+
const BUILD_MODE = "build";
|
|
57
|
+
const DEV_MODE = "dev";
|
|
58
|
+
const DEV_SOURCE_MODE = "dev-source";
|
|
59
|
+
const CORE_MODULES = [
|
|
62
60
|
"@kotori-bot/kotori-plugin-core",
|
|
63
61
|
"@kotori-bot/kotori-plugin-i18n-command",
|
|
64
62
|
"@kotori-bot/kotori-plugin-filter"
|
package/lib/decorators/index.js
CHANGED
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
|
|
2
2
|
/**
|
|
3
3
|
* @Package @kotori-bot/loader
|
|
4
|
-
* @Version 1.6.0
|
|
4
|
+
* @Version 1.6.0
|
|
5
5
|
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
6
|
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
7
|
* @License GPL-3.0
|
|
8
8
|
* @Link https://github.com/kotorijs/kotori
|
|
9
|
-
* @Date 2024/6/
|
|
9
|
+
* @Date 2024/6/7 11:22:23
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
"use strict";
|
|
13
|
+
var __create = Object.create;
|
|
13
14
|
var __defProp = Object.defineProperty;
|
|
14
15
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
15
16
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
17
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
16
18
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
17
19
|
var __export = (target, all) => {
|
|
18
20
|
for (var name in all)
|
|
@@ -26,140 +28,42 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
26
28
|
}
|
|
27
29
|
return to;
|
|
28
30
|
};
|
|
31
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
32
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
33
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
34
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
35
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
36
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
37
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
38
|
+
mod
|
|
39
|
+
));
|
|
29
40
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
-
|
|
31
|
-
// src/decorators/index.ts
|
|
32
41
|
var decorators_exports = {};
|
|
33
42
|
__export(decorators_exports, {
|
|
34
|
-
KotoriPlugin: () => KotoriPlugin,
|
|
35
43
|
default: () => decorators_default,
|
|
36
44
|
plugins: () => plugins
|
|
37
45
|
});
|
|
38
46
|
module.exports = __toCommonJS(decorators_exports);
|
|
39
|
-
var import_node_fs = require("fs");
|
|
40
|
-
var
|
|
41
|
-
var import_core2 = require("@kotori-bot/core");
|
|
42
|
-
|
|
43
|
-
// src/decorators/utils.ts
|
|
44
|
-
var import_node_path = require("path");
|
|
47
|
+
var import_node_fs = require("node:fs");
|
|
48
|
+
var import_node_path = require("node:path");
|
|
45
49
|
var import_core = require("@kotori-bot/core");
|
|
46
|
-
var
|
|
47
|
-
|
|
48
|
-
isCreated = false;
|
|
49
|
-
object;
|
|
50
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
51
|
-
register(callback) {
|
|
52
|
-
return (...args) => this.ctx.root.once("ready_module_decorators", (pkgName) => {
|
|
53
|
-
if (pkgName === this.ctx.identity) {
|
|
54
|
-
callback(...args);
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
this.register(callback);
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
/* eslint-enable @typescript-eslint/no-explicit-any */
|
|
61
|
-
constructor(ctx) {
|
|
62
|
-
this.ctx = ctx;
|
|
63
|
-
if (!this.ctx.root.get("decorators")) this.ctx.root.provide("decorators", { registers: [] });
|
|
64
|
-
if (this.ctx.identity) this.ctx.root.get("decorators").registers.push(this.ctx.identity);
|
|
65
|
-
}
|
|
66
|
-
import = (Target) => {
|
|
67
|
-
this.register(() => {
|
|
68
|
-
if (!this.isCreated) this.schema(Target, void 0);
|
|
69
|
-
})();
|
|
70
|
-
};
|
|
71
|
-
lang = (target, property) => {
|
|
72
|
-
this.register(() => {
|
|
73
|
-
const lang = target[property];
|
|
74
|
-
this.ctx.parent.i18n.use((0, import_node_path.resolve)(...Array.isArray(lang) ? lang : [lang]));
|
|
75
|
-
})();
|
|
76
|
-
};
|
|
77
|
-
inject = (target, property) => {
|
|
78
|
-
this.register(() => {
|
|
79
|
-
const inject = target[property];
|
|
80
|
-
inject.forEach(
|
|
81
|
-
(identity) => this.ctx[import_core.Tokens.container].forEach((service, name) => {
|
|
82
|
-
if (!(service instanceof import_core.Service) || service.identity !== identity) return;
|
|
83
|
-
this.ctx.inject(name);
|
|
84
|
-
})
|
|
85
|
-
);
|
|
86
|
-
})();
|
|
87
|
-
};
|
|
88
|
-
schema = (Target, property) => {
|
|
89
|
-
this.register(() => {
|
|
90
|
-
let config = (this.ctx[import_core.Symbols.modules].get(this.ctx.identity) ?? [])[1];
|
|
91
|
-
if (Target[property]) {
|
|
92
|
-
const result = Target[property].parseSafe(config);
|
|
93
|
-
if (!result.value)
|
|
94
|
-
throw new import_core.ModuleError(`Config format of module ${this.ctx.identity} is error: ${result.error.message}`);
|
|
95
|
-
config = result.data;
|
|
96
|
-
}
|
|
97
|
-
if (this.isCreated) return;
|
|
98
|
-
this.isCreated = true;
|
|
99
|
-
this.object = new Target(this.ctx, config);
|
|
100
|
-
this.ctx.root.emit("ready_module", { instance: { name: this.ctx.identity, config } });
|
|
101
|
-
})();
|
|
102
|
-
};
|
|
103
|
-
on(meta) {
|
|
104
|
-
return this.register(
|
|
105
|
-
(target, property) => this.ctx.on(meta.type, (...args) => target[property].bind(this.object)(...args))
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
once(meta) {
|
|
109
|
-
return this.register(
|
|
110
|
-
(target, property) => this.ctx.once(meta.type, (...args) => target[property].bind(this.object)(...args))
|
|
111
|
-
);
|
|
112
|
-
}
|
|
113
|
-
midware(meta) {
|
|
114
|
-
return this.register(
|
|
115
|
-
(target, property) => this.ctx.midware((next, session) => target[property].bind(this.object)(next, session), meta?.priority)
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
command(meta) {
|
|
119
|
-
return this.register((target, property) => {
|
|
120
|
-
const command = this.ctx.command(meta.template, meta).action((data, session) => target[property].bind(this.object)(data, session));
|
|
121
|
-
meta.options?.forEach(([name, template]) => command.option(name, template));
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
regexp(meta) {
|
|
125
|
-
return this.register(
|
|
126
|
-
(target, property) => this.ctx.regexp(meta.match, (match, session) => target[property].bind(this.object)(match, session))
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
task(meta) {
|
|
130
|
-
return this.register(
|
|
131
|
-
(target, property) => this.ctx.task(meta, (ctx) => target[property].bind(this.object)(ctx))
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
var utils_default = Decorators;
|
|
136
|
-
|
|
137
|
-
// src/decorators/plugin.ts
|
|
138
|
-
var KotoriPlugin = class {
|
|
139
|
-
ctx;
|
|
140
|
-
config;
|
|
141
|
-
constructor(ctx, config) {
|
|
142
|
-
this.ctx = ctx;
|
|
143
|
-
this.config = config;
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
// src/decorators/index.ts
|
|
50
|
+
var import_utils = __toESM(require("./utils"));
|
|
51
|
+
__reExport(decorators_exports, require("./plugin"), module.exports);
|
|
148
52
|
function plugins(plugin) {
|
|
149
53
|
let pkgName;
|
|
150
54
|
if (!Array.isArray(plugin) && typeof plugin === "object") {
|
|
151
55
|
pkgName = plugin.name;
|
|
152
56
|
} else {
|
|
153
57
|
pkgName = JSON.parse(
|
|
154
|
-
(0, import_node_fs.readFileSync)((0,
|
|
58
|
+
(0, import_node_fs.readFileSync)((0, import_node_path.resolve)(...Array.isArray(plugin) ? plugin : [plugin], "package.json")).toString()
|
|
155
59
|
).name;
|
|
156
60
|
}
|
|
157
|
-
const ctx =
|
|
158
|
-
return new
|
|
61
|
+
const ctx = import_core.Container.getInstance().extends(void 0, pkgName);
|
|
62
|
+
return new import_utils.default(ctx);
|
|
159
63
|
}
|
|
160
64
|
var decorators_default = plugins;
|
|
161
65
|
// Annotate the CommonJS export names for ESM import in node:
|
|
162
66
|
0 && (module.exports = {
|
|
163
|
-
|
|
164
|
-
|
|
67
|
+
plugins,
|
|
68
|
+
...require("./plugin")
|
|
165
69
|
});
|
package/lib/decorators/plugin.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
|
|
2
2
|
/**
|
|
3
3
|
* @Package @kotori-bot/loader
|
|
4
|
-
* @Version 1.6.0
|
|
4
|
+
* @Version 1.6.0
|
|
5
5
|
* @Author Hotaru <biyuehuya@gmail.com>
|
|
6
6
|
* @Copyright 2024 Hotaru. All rights reserved.
|
|
7
7
|
* @License GPL-3.0
|
|
8
8
|
* @Link https://github.com/kotorijs/kotori
|
|
9
|
-
* @Date 2024/6/
|
|
9
|
+
* @Date 2024/6/7 11:22:23
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
"use strict";
|
|
@@ -27,22 +27,20 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
27
27
|
return to;
|
|
28
28
|
};
|
|
29
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
-
|
|
31
|
-
// src/decorators/plugin.ts
|
|
32
30
|
var plugin_exports = {};
|
|
33
31
|
__export(plugin_exports, {
|
|
34
32
|
KotoriPlugin: () => KotoriPlugin,
|
|
35
33
|
default: () => plugin_default
|
|
36
34
|
});
|
|
37
35
|
module.exports = __toCommonJS(plugin_exports);
|
|
38
|
-
|
|
36
|
+
class KotoriPlugin {
|
|
39
37
|
ctx;
|
|
40
38
|
config;
|
|
41
39
|
constructor(ctx, config) {
|
|
42
40
|
this.ctx = ctx;
|
|
43
41
|
this.config = config;
|
|
44
42
|
}
|
|
45
|
-
}
|
|
43
|
+
}
|
|
46
44
|
var plugin_default = KotoriPlugin;
|
|
47
45
|
// Annotate the CommonJS export names for ESM import in node:
|
|
48
46
|
0 && (module.exports = {
|