bakit 2.0.0-alpha.25 → 2.0.0-alpha.26
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/dist/index.d.ts +9 -4
- package/dist/index.js +49 -33
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -377,11 +377,13 @@ declare class Instance {
|
|
|
377
377
|
private loadModules;
|
|
378
378
|
private initIntents;
|
|
379
379
|
private isInHotDirectory;
|
|
380
|
-
private isFileHotReloadable;
|
|
381
380
|
private restart;
|
|
382
381
|
shutdown(): Promise<void>;
|
|
382
|
+
private containsEntryPoint;
|
|
383
|
+
private containsHotModule;
|
|
383
384
|
private onFileRemove;
|
|
384
385
|
private onFileChange;
|
|
386
|
+
private unloadModule;
|
|
385
387
|
}
|
|
386
388
|
declare function useApp(): Instance;
|
|
387
389
|
|
|
@@ -460,8 +462,11 @@ declare function getTopLevelDirectory(path: string, entryDir: string): string |
|
|
|
460
462
|
|
|
461
463
|
declare function $initLoader(): void;
|
|
462
464
|
declare function $unloadFile(path: string): Promise<boolean>;
|
|
463
|
-
declare function
|
|
464
|
-
declare function
|
|
465
|
+
declare function getImporters(path: string, createNew: true): Set<string>;
|
|
466
|
+
declare function getImporters(path: string, createNew?: false): Set<string> | undefined;
|
|
467
|
+
declare function getDependencyChain(path: string): string[];
|
|
468
|
+
declare function isImported(path: string): boolean;
|
|
469
|
+
declare function isImportedBy(path: string, matcher: string | RegExp | ((path: string) => boolean)): boolean;
|
|
465
470
|
|
|
466
471
|
declare const RPC_RESPONSE_MARK = "$DONE:";
|
|
467
472
|
declare const RPC_RESPONSE_TIMEOUT = 5000;
|
|
@@ -564,4 +569,4 @@ interface LoadResult {
|
|
|
564
569
|
type NextResolve = (specifier: string, context: ResolveContext) => Promise<ResolveResult>;
|
|
565
570
|
type NextLoad = (url: string, context: LoadContext) => Promise<LoadResult>;
|
|
566
571
|
|
|
567
|
-
export { $initLoader, $unloadFile, type AnyParam, ArgumentError, BakitClient, type BakitClientEvents, BakitError, BaseClientManager, BaseCommandContext, BaseParam, type BaseParamOptions, BaseParamSchema, type BaseRPCMessage, type BaseRPCResponse, ChatInputContext, type ChatInputContextSendOptions, Command, type CommandContext, CommandManager, type CommandOptions, type CommandOptionsInput, CommandOptionsSchema, Context, type ContextSendOptions, EVENT_INTENT_MAPPING, type ErrorHookCallback, type GetPrefixFunction, HookOrder, HookState, type InferParamTuple, type InferParamValue, type InitializeData, Instance, LifecycleManager, Listener, ListenerManager, type ListenerOptions, ListenerOptionsSchema, type LoadContext, type LoadResult, type MainHookCallback, MessageContext, type MessageContextSendOptions, type NextLoad, type NextResolve, type NumberOptions, NumberParam, NumberParamSchema, type ParamResolvedOutputType, ParamUserType, Params, ProjectCacheManager, type ProjectConfig, type ProjectConfigInput, ProjectConfigSchema, RPC, type RPCErrorResponse, type RPCMessage, type RPCPendingPromise, type RPCRequest, type RPCResponse, type RPCSuccessResponse, RPC_RESPONSE_MARK, RPC_RESPONSE_TIMEOUT, type ResolveContext, type ResolveResult, type StringOptions, StringParam, StringParamSchema, type UserOptions, UserParam, UserParamSchema, chatInputCommandHandler, defineCommand, defineConfig, defineListener, extractSnowflakeId, getConfig, getTopLevelDirectory, isImported, isImportedBy, loadConfig, messageCommandHandler, registerCommandsHandler, tokenize, useApp, validateParamsOrder };
|
|
572
|
+
export { $initLoader, $unloadFile, type AnyParam, ArgumentError, BakitClient, type BakitClientEvents, BakitError, BaseClientManager, BaseCommandContext, BaseParam, type BaseParamOptions, BaseParamSchema, type BaseRPCMessage, type BaseRPCResponse, ChatInputContext, type ChatInputContextSendOptions, Command, type CommandContext, CommandManager, type CommandOptions, type CommandOptionsInput, CommandOptionsSchema, Context, type ContextSendOptions, EVENT_INTENT_MAPPING, type ErrorHookCallback, type GetPrefixFunction, HookOrder, HookState, type InferParamTuple, type InferParamValue, type InitializeData, Instance, LifecycleManager, Listener, ListenerManager, type ListenerOptions, ListenerOptionsSchema, type LoadContext, type LoadResult, type MainHookCallback, MessageContext, type MessageContextSendOptions, type NextLoad, type NextResolve, type NumberOptions, NumberParam, NumberParamSchema, type ParamResolvedOutputType, ParamUserType, Params, ProjectCacheManager, type ProjectConfig, type ProjectConfigInput, ProjectConfigSchema, RPC, type RPCErrorResponse, type RPCMessage, type RPCPendingPromise, type RPCRequest, type RPCResponse, type RPCSuccessResponse, RPC_RESPONSE_MARK, RPC_RESPONSE_TIMEOUT, type ResolveContext, type ResolveResult, type StringOptions, StringParam, StringParamSchema, type UserOptions, UserParam, UserParamSchema, chatInputCommandHandler, defineCommand, defineConfig, defineListener, extractSnowflakeId, getConfig, getDependencyChain, getImporters, getTopLevelDirectory, isImported, isImportedBy, loadConfig, messageCommandHandler, registerCommandsHandler, tokenize, useApp, validateParamsOrder };
|
package/dist/index.js
CHANGED
|
@@ -231,6 +231,8 @@ var RPC_RESPONSE_MARK = "$DONE:", RPC_RESPONSE_TIMEOUT = 5e3, RPC = class extend
|
|
|
231
231
|
});
|
|
232
232
|
}
|
|
233
233
|
};
|
|
234
|
+
|
|
235
|
+
// src/lib/loader/loader.ts
|
|
234
236
|
var rpc, reverseDependencyGraph = /* @__PURE__ */ new Map();
|
|
235
237
|
function $initLoader() {
|
|
236
238
|
let { port1, port2 } = new MessageChannel(), hookPath = new URL("./hooks.js", import.meta.url).href;
|
|
@@ -244,26 +246,33 @@ function $unloadFile(path) {
|
|
|
244
246
|
throw new Error("Loader isn't initialized");
|
|
245
247
|
return rpc.request("unload", resolve(path));
|
|
246
248
|
}
|
|
247
|
-
function
|
|
248
|
-
|
|
249
|
+
function getImporters(path, createNew = false) {
|
|
250
|
+
path = resolve(path);
|
|
251
|
+
let entry = reverseDependencyGraph.get(path);
|
|
252
|
+
return createNew && !entry && (entry = /* @__PURE__ */ new Set(), reverseDependencyGraph.set(path, entry)), entry;
|
|
249
253
|
}
|
|
250
|
-
function
|
|
251
|
-
|
|
254
|
+
function getDependencyChain(path) {
|
|
255
|
+
path = resolve(path);
|
|
256
|
+
let queue = [path], visited = /* @__PURE__ */ new Set();
|
|
252
257
|
for (; queue.length > 0; ) {
|
|
253
258
|
let current = queue.shift();
|
|
254
|
-
if (visited.has(current))
|
|
259
|
+
if (!current || visited.has(current) || (visited.add(current), current.includes("/node_modules/")))
|
|
255
260
|
continue;
|
|
256
|
-
|
|
257
|
-
let parents = reverseDependencyGraph.get(current);
|
|
261
|
+
let parents = getImporters(current);
|
|
258
262
|
if (parents)
|
|
259
|
-
for (let parent of parents)
|
|
260
|
-
let isMatch = false;
|
|
261
|
-
if (typeof matcher == "string" ? isMatch = parent === matcher : matcher instanceof RegExp ? isMatch = matcher.test(parent) : typeof matcher == "function" && (isMatch = matcher(parent)), isMatch)
|
|
262
|
-
return true;
|
|
263
|
+
for (let parent of parents)
|
|
263
264
|
visited.has(parent) || queue.push(parent);
|
|
264
|
-
}
|
|
265
265
|
}
|
|
266
|
-
return
|
|
266
|
+
return Array.from(visited);
|
|
267
|
+
}
|
|
268
|
+
function isImported(path) {
|
|
269
|
+
return !!getImporters(path)?.size;
|
|
270
|
+
}
|
|
271
|
+
function isImportedBy(path, matcher) {
|
|
272
|
+
return getDependencyChain(path).slice(1).some((p) => {
|
|
273
|
+
let isMatch = false;
|
|
274
|
+
return typeof matcher == "string" && (isMatch = resolve(matcher) === p), typeof matcher == "function" && (isMatch = matcher(p)), matcher instanceof RegExp && (isMatch = matcher.test(p)), isMatch;
|
|
275
|
+
});
|
|
267
276
|
}
|
|
268
277
|
function onDependencyAdd(data) {
|
|
269
278
|
let { url, parentURL } = data, path = fileURLToPath(url), parentPath = fileURLToPath(parentURL), entry = reverseDependencyGraph.get(path);
|
|
@@ -994,7 +1003,7 @@ chatInputCommandHandler.main(async (_, interaction) => {
|
|
|
994
1003
|
});
|
|
995
1004
|
|
|
996
1005
|
// src/core/internal/Instance.ts
|
|
997
|
-
var HOT_DIRECTORIES = ["listeners", "commands"], SOURCE_ROOT = resolve(process.cwd(), "src"), Instance = class {
|
|
1006
|
+
var HOT_DIRECTORIES = ["listeners", "commands"], SOURCE_ROOT = resolve(process.cwd(), "src"), APP_ENTRY_POINTS = /* @__PURE__ */ new Set([resolve(SOURCE_ROOT, "index.ts"), resolve(SOURCE_ROOT, "index.js")]), Instance = class {
|
|
998
1007
|
client;
|
|
999
1008
|
cache;
|
|
1000
1009
|
rpc;
|
|
@@ -1029,46 +1038,53 @@ var HOT_DIRECTORIES = ["listeners", "commands"], SOURCE_ROOT = resolve(process.c
|
|
|
1029
1038
|
let topLevelDir = getTopLevelDirectory(path, SOURCE_ROOT);
|
|
1030
1039
|
return !!topLevelDir && HOT_DIRECTORIES.includes(topLevelDir);
|
|
1031
1040
|
}
|
|
1032
|
-
isFileHotReloadable(path) {
|
|
1033
|
-
return path = resolve(path), !(!this.isInHotDirectory(path) || isImportedBy(path, (parentPath) => !this.isInHotDirectory(parentPath)));
|
|
1034
|
-
}
|
|
1035
1041
|
restart() {
|
|
1036
1042
|
this.rpc.send("restart", {});
|
|
1037
1043
|
}
|
|
1038
1044
|
async shutdown() {
|
|
1039
1045
|
this.client && await this.client.destroy().catch(() => null), process.exit(0);
|
|
1040
1046
|
}
|
|
1047
|
+
containsEntryPoint(chain) {
|
|
1048
|
+
return chain.some((x) => APP_ENTRY_POINTS.has(x));
|
|
1049
|
+
}
|
|
1050
|
+
containsHotModule(chain) {
|
|
1051
|
+
return chain.some((x) => this.isInHotDirectory(x));
|
|
1052
|
+
}
|
|
1041
1053
|
async onFileRemove(path) {
|
|
1042
1054
|
if (!isImported(path))
|
|
1043
1055
|
return;
|
|
1044
|
-
|
|
1056
|
+
let chain = getDependencyChain(path);
|
|
1057
|
+
if (this.containsEntryPoint(chain)) {
|
|
1045
1058
|
this.restart();
|
|
1046
1059
|
return;
|
|
1047
1060
|
}
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
await listeners.unload(path);
|
|
1052
|
-
break;
|
|
1053
|
-
case "commands":
|
|
1054
|
-
await commands.unload(path);
|
|
1055
|
-
break;
|
|
1056
|
-
}
|
|
1061
|
+
if (this.containsHotModule(chain))
|
|
1062
|
+
for (let path2 of chain.reverse())
|
|
1063
|
+
await this.unloadModule(path2);
|
|
1057
1064
|
}
|
|
1058
1065
|
async onFileChange(path) {
|
|
1059
1066
|
if (!isImported(path))
|
|
1060
1067
|
return;
|
|
1061
|
-
|
|
1068
|
+
let chain = getDependencyChain(path);
|
|
1069
|
+
if (this.containsEntryPoint(chain)) {
|
|
1062
1070
|
this.restart();
|
|
1063
1071
|
return;
|
|
1064
1072
|
}
|
|
1065
|
-
|
|
1066
|
-
|
|
1073
|
+
if (this.containsHotModule(chain))
|
|
1074
|
+
for (let path2 of chain.reverse())
|
|
1075
|
+
await this.unloadModule(path2, true);
|
|
1076
|
+
}
|
|
1077
|
+
async unloadModule(path, reload = false) {
|
|
1078
|
+
let { listeners, commands } = this.client.managers;
|
|
1079
|
+
switch (getTopLevelDirectory(path, SOURCE_ROOT)) {
|
|
1067
1080
|
case "listeners":
|
|
1068
|
-
await listeners
|
|
1081
|
+
await listeners[reload ? "reload" : "unload"](path);
|
|
1069
1082
|
break;
|
|
1070
1083
|
case "commands":
|
|
1071
|
-
await commands
|
|
1084
|
+
await commands[reload ? "reload" : "unload"](path);
|
|
1085
|
+
break;
|
|
1086
|
+
default:
|
|
1087
|
+
await $unloadFile(path);
|
|
1072
1088
|
break;
|
|
1073
1089
|
}
|
|
1074
1090
|
}
|
|
@@ -1087,4 +1103,4 @@ var Params = {
|
|
|
1087
1103
|
user: createFactory(UserParam)
|
|
1088
1104
|
};
|
|
1089
1105
|
|
|
1090
|
-
export { $initLoader, $unloadFile, ArgumentError, BakitClient, BakitError, BaseClientManager, BaseCommandContext, BaseParam, BaseParamSchema, ChatInputContext, Command, CommandManager, CommandOptionsSchema, Context, EVENT_INTENT_MAPPING, HookOrder, HookState, Instance, LifecycleManager, Listener, ListenerManager, ListenerOptionsSchema, MessageContext, NumberParam, NumberParamSchema, ParamUserType, Params, ProjectCacheManager, ProjectConfigSchema, RPC, RPC_RESPONSE_MARK, RPC_RESPONSE_TIMEOUT, StringParam, StringParamSchema, UserParam, UserParamSchema, chatInputCommandHandler, defineCommand, defineConfig, defineListener, extractSnowflakeId, getConfig, getTopLevelDirectory, isImported, isImportedBy, loadConfig, messageCommandHandler, registerCommandsHandler, tokenize, useApp, validateParamsOrder };
|
|
1106
|
+
export { $initLoader, $unloadFile, ArgumentError, BakitClient, BakitError, BaseClientManager, BaseCommandContext, BaseParam, BaseParamSchema, ChatInputContext, Command, CommandManager, CommandOptionsSchema, Context, EVENT_INTENT_MAPPING, HookOrder, HookState, Instance, LifecycleManager, Listener, ListenerManager, ListenerOptionsSchema, MessageContext, NumberParam, NumberParamSchema, ParamUserType, Params, ProjectCacheManager, ProjectConfigSchema, RPC, RPC_RESPONSE_MARK, RPC_RESPONSE_TIMEOUT, StringParam, StringParamSchema, UserParam, UserParamSchema, chatInputCommandHandler, defineCommand, defineConfig, defineListener, extractSnowflakeId, getConfig, getDependencyChain, getImporters, getTopLevelDirectory, isImported, isImportedBy, loadConfig, messageCommandHandler, registerCommandsHandler, tokenize, useApp, validateParamsOrder };
|