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