bakit 2.0.0-alpha.25 → 2.0.0-alpha.27

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/hooks.js CHANGED
@@ -11,7 +11,7 @@ function isDevelopment() {
11
11
  return process.env.NODE_ENV === "development";
12
12
  }
13
13
  async function initialize({ port }) {
14
- rpc = new RPC(port), versions = /* @__PURE__ */ new Map(), rpc.on("message", onUnload), isDevelopment() && (esbuild = await import('esbuild'));
14
+ rpc = new RPC(port), versions = /* @__PURE__ */ new Map(), rpc.on("unload", onUnload), isDevelopment() && (esbuild = await import('esbuild'));
15
15
  }
16
16
  async function resolve(specifier, context, nextResolve) {
17
17
  if (shouldSkip(specifier))
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 isImported(filePath: string): boolean;
464
- declare function isImportedBy(filePath: string, matcher: string | RegExp | ((path: string) => boolean)): boolean;
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 isImported(filePath) {
248
- return !!reverseDependencyGraph.get(filePath)?.size;
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 isImportedBy(filePath, matcher) {
251
- let queue = [filePath], visited = /* @__PURE__ */ new Set();
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
- visited.add(current);
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 false;
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,61 @@ 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) {
1054
+ if (APP_ENTRY_POINTS.has(path)) {
1055
+ this.restart();
1056
+ return;
1057
+ }
1042
1058
  if (!isImported(path))
1043
1059
  return;
1044
- if (!this.isFileHotReloadable(path)) {
1060
+ let chain = getDependencyChain(path);
1061
+ if (this.containsEntryPoint(chain)) {
1045
1062
  this.restart();
1046
1063
  return;
1047
1064
  }
1048
- let topLevelDir = getTopLevelDirectory(path, SOURCE_ROOT), { listeners, commands } = this.client.managers;
1049
- switch (topLevelDir) {
1050
- case "listeners":
1051
- await listeners.unload(path);
1052
- break;
1053
- case "commands":
1054
- await commands.unload(path);
1055
- break;
1056
- }
1065
+ if (this.containsHotModule(chain))
1066
+ for (let path2 of chain.reverse())
1067
+ await this.unloadModule(path2);
1057
1068
  }
1058
1069
  async onFileChange(path) {
1070
+ if (APP_ENTRY_POINTS.has(path)) {
1071
+ this.restart();
1072
+ return;
1073
+ }
1059
1074
  if (!isImported(path))
1060
1075
  return;
1061
- if (!this.isFileHotReloadable(path)) {
1076
+ let chain = getDependencyChain(path);
1077
+ if (this.containsEntryPoint(chain)) {
1062
1078
  this.restart();
1063
1079
  return;
1064
1080
  }
1065
- let topLevelDir = getTopLevelDirectory(path, SOURCE_ROOT), { listeners, commands } = this.client.managers;
1066
- switch (topLevelDir) {
1081
+ if (this.containsHotModule(chain))
1082
+ for (let path2 of chain.reverse())
1083
+ await this.unloadModule(path2, true);
1084
+ }
1085
+ async unloadModule(path, reload = false) {
1086
+ let { listeners, commands } = this.client.managers;
1087
+ switch (getTopLevelDirectory(path, SOURCE_ROOT)) {
1067
1088
  case "listeners":
1068
- await listeners.reload(path);
1089
+ await listeners[reload ? "reload" : "unload"](path);
1069
1090
  break;
1070
1091
  case "commands":
1071
- await commands.reload(path);
1092
+ await commands[reload ? "reload" : "unload"](path);
1093
+ break;
1094
+ default:
1095
+ await $unloadFile(path);
1072
1096
  break;
1073
1097
  }
1074
1098
  }
@@ -1087,4 +1111,4 @@ var Params = {
1087
1111
  user: createFactory(UserParam)
1088
1112
  };
1089
1113
 
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 };
1114
+ 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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bakit",
3
- "version": "2.0.0-alpha.25",
3
+ "version": "2.0.0-alpha.27",
4
4
  "description": "A framework for discord.js",
5
5
  "type": "module",
6
6
  "exports": {