bakit 2.0.0-alpha.13 → 2.0.0-alpha.15

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/bin/bakit.js CHANGED
@@ -2,17 +2,14 @@ import { config } from 'dotenv';
2
2
  import { program } from 'commander';
3
3
  import { fork } from 'child_process';
4
4
  import chokidar from 'chokidar';
5
- import path, { relative, sep } from 'path';
5
+ import path, { resolve, relative, sep } from 'path';
6
6
  import { createJiti } from 'jiti';
7
- import { fileURLToPath } from 'url';
8
7
 
9
8
  // src/bin/bakit.ts
10
9
  var Module = class {
11
10
  static jiti = createJiti(process.cwd());
12
11
  static async import(module, defaultImport = false) {
13
- let path2 = this.resolve(module);
14
- if (!path2)
15
- return null;
12
+ let path2 = resolve(module);
16
13
  try {
17
14
  return await this.jiti.import(path2, { default: defaultImport });
18
15
  } catch (error) {
@@ -20,21 +17,13 @@ var Module = class {
20
17
  }
21
18
  }
22
19
  static isLoaded(module) {
23
- let path2 = this.resolve(module);
20
+ let path2 = resolve(module);
24
21
  return !!path2 && !!this.jiti.cache[path2];
25
22
  }
26
23
  static unload(module) {
27
- let path2 = this.resolve(module);
24
+ let path2 = resolve(module);
28
25
  return !path2 || !this.jiti.cache[path2] ? false : (delete this.jiti.cache[path2], true);
29
26
  }
30
- static resolve(module) {
31
- try {
32
- let url = this.jiti.esmResolve(module);
33
- return fileURLToPath(url);
34
- } catch {
35
- return null;
36
- }
37
- }
38
27
  static getTopLevel(path2, entryDir) {
39
28
  return relative(entryDir, path2).split(sep)[0] ?? null;
40
29
  }
package/dist/index.d.ts CHANGED
@@ -45,7 +45,6 @@ declare class Module {
45
45
  static import<T>(module: string, defaultImport?: boolean): Promise<T | null>;
46
46
  static isLoaded(module: string): boolean;
47
47
  static unload(module: string): boolean;
48
- static resolve(module: string): string | null;
49
48
  static getTopLevel(path: string, entryDir: string): string | null;
50
49
  }
51
50
 
@@ -322,6 +321,7 @@ declare class CommandManager extends BaseClientManager {
322
321
  * @returns The command object if unloaded successfully.
323
322
  */
324
323
  unload(path: string): Promise<Command | undefined>;
324
+ reload(path: string): Promise<Command<any[]> | undefined>;
325
325
  /**
326
326
  * Add a command to the registry.
327
327
  * @param command Command to add.
@@ -374,6 +374,7 @@ declare class ListenerManager extends BaseClientManager {
374
374
  * @returns The listener object if unloaded successfully.
375
375
  */
376
376
  unload(path: string): Promise<Listener | undefined>;
377
+ reload(path: string): Promise<Listener<keyof BakitClientEvents> | undefined>;
377
378
  /**
378
379
  * Add a listener to the registry and create a listener for client.
379
380
  * @param listener Listener to add.
package/dist/index.js CHANGED
@@ -2,8 +2,7 @@ import { GatewayIntentBits, Events, Client, Collection, IntentsBitField, SlashCo
2
2
  import z4, { z } from 'zod';
3
3
  import glob from 'tiny-glob';
4
4
  import { createJiti } from 'jiti';
5
- import { fileURLToPath } from 'url';
6
- import path, { relative, sep, posix, join, dirname } from 'path';
5
+ import path, { resolve, relative, sep, posix, join, dirname } from 'path';
7
6
  import { inspect } from 'util';
8
7
  import { fork } from 'child_process';
9
8
  import chokidar from 'chokidar';
@@ -138,9 +137,7 @@ function extractSnowflakeId(input) {
138
137
  var Module = class {
139
138
  static jiti = createJiti(process.cwd());
140
139
  static async import(module, defaultImport = false) {
141
- let path2 = this.resolve(module);
142
- if (!path2)
143
- return null;
140
+ let path2 = resolve(module);
144
141
  try {
145
142
  return await this.jiti.import(path2, { default: defaultImport });
146
143
  } catch (error) {
@@ -148,21 +145,13 @@ var Module = class {
148
145
  }
149
146
  }
150
147
  static isLoaded(module) {
151
- let path2 = this.resolve(module);
148
+ let path2 = resolve(module);
152
149
  return !!path2 && !!this.jiti.cache[path2];
153
150
  }
154
151
  static unload(module) {
155
- let path2 = this.resolve(module);
152
+ let path2 = resolve(module);
156
153
  return !path2 || !this.jiti.cache[path2] ? false : (delete this.jiti.cache[path2], true);
157
154
  }
158
- static resolve(module) {
159
- try {
160
- let url = this.jiti.esmResolve(module);
161
- return fileURLToPath(url);
162
- } catch {
163
- return null;
164
- }
165
- }
166
155
  static getTopLevel(path2, entryDir) {
167
156
  return relative(entryDir, path2).split(sep)[0] ?? null;
168
157
  }
@@ -194,7 +183,10 @@ var ProjectConfigSchema = z.object({
194
183
  function defineConfig(config) {
195
184
  return config;
196
185
  }
186
+ var _config;
197
187
  async function loadConfig(cwd = process.cwd()) {
188
+ if (_config)
189
+ return console.warn("loadConfig() was called more than once. This shouldn't happen."), _config;
198
190
  let globPattern = `bakit.config.{${["ts", "js"].join(",")}}`, [configPath, other] = await glob(globPattern, {
199
191
  cwd: cwd.replace(/\\/g, "/"),
200
192
  // ensure the path uses `/` instead of `\` on Windows
@@ -204,10 +196,12 @@ async function loadConfig(cwd = process.cwd()) {
204
196
  throw new Error("Missing config file");
205
197
  other && console.warn(`Multiple config files found in ${cwd}. Using ${configPath}.`);
206
198
  let config = await Module.import(configPath, true);
207
- return Object.freeze(await ProjectConfigSchema.parseAsync(config));
199
+ return _config = Object.freeze(await ProjectConfigSchema.parseAsync(config)), _config;
208
200
  }
209
201
  function getConfig() {
210
- throw new Error("Project config is not loaded.");
202
+ if (!_config)
203
+ throw new Error("Project config is not loaded.");
204
+ return _config;
211
205
  }
212
206
 
213
207
  // src/base/lifecycle/Context.ts
@@ -595,11 +589,8 @@ var CommandManager = class extends BaseClientManager {
595
589
  commands = new Collection();
596
590
  entries = new Collection();
597
591
  async loadModules(entryDir) {
598
- let pattern = posix.join(posix.resolve(entryDir), "commands", "**/*.{ts,js}"), files = await glob(pattern, {
599
- cwd: process.cwd(),
600
- absolute: true
601
- }), filtered = (await Promise.all(files.map((file) => this.load(file)))).filter((c) => !!c);
602
- return console.log(`[Loader] Loaded ${filtered.length}/${files.length}`), filtered;
592
+ let pattern = posix.join(posix.resolve(entryDir), "commands", "**/*.{ts,js}"), files = await glob(pattern, { cwd: process.cwd() }), filtered = (await Promise.all(files.map((file) => this.load(file)))).filter((c) => !!c);
593
+ return console.log(`[Loader] Loaded ${filtered.length}/${files.length} command(s)`), filtered;
603
594
  }
604
595
  /**
605
596
  * Load the file and add the command to the registry.
@@ -628,6 +619,12 @@ var CommandManager = class extends BaseClientManager {
628
619
  if (Module.isLoaded(path2) && (command ??= await Module.import(path2, true), Module.unload(path2)), this.entries.delete(path2), !!command)
629
620
  return this.remove(command);
630
621
  }
622
+ async reload(path2) {
623
+ await this.unload(path2);
624
+ let command = await this.load(path2);
625
+ if (command)
626
+ return console.log(`[Loader] Reloaded command'${command.options.name}' at '${path2}'`), command;
627
+ }
631
628
  /**
632
629
  * Add a command to the registry.
633
630
  * @param command Command to add.
@@ -681,11 +678,8 @@ var ListenerManager = class extends BaseClientManager {
681
678
  entries = new Collection();
682
679
  executors = /* @__PURE__ */ new WeakMap();
683
680
  async loadModules(entryDir) {
684
- let pattern = posix.join(posix.resolve(entryDir), "listeners", "**/*.{ts,js}"), files = await glob(pattern, {
685
- cwd: process.cwd(),
686
- absolute: true
687
- }), filtered = (await Promise.all(files.map((file) => this.load(file)))).filter((l) => !!l);
688
- return console.log(`[Loader] Loaded ${filtered.length}/${files.length}`), filtered;
681
+ let pattern = posix.join(posix.resolve(entryDir), "listeners", "**/*.{ts,js}"), files = await glob(pattern, { cwd: process.cwd() }), filtered = (await Promise.all(files.map((file) => this.load(file)))).filter((l) => !!l);
682
+ return console.log(`[Loader] Loaded ${filtered.length}/${files.length} listener(s)`), filtered;
689
683
  }
690
684
  /**
691
685
  * Load the file and add the listener to the registry.
@@ -714,6 +708,12 @@ var ListenerManager = class extends BaseClientManager {
714
708
  if (Module.isLoaded(path2) && (listener ??= await Module.import(path2, true), Module.unload(path2)), this.entries.delete(path2), !!listener)
715
709
  return this.remove(listener)?.[0];
716
710
  }
711
+ async reload(path2) {
712
+ await this.unload(path2);
713
+ let listener = await this.load(path2);
714
+ if (listener)
715
+ return console.log(`[Loader] Reloaded listener '${listener.options.name}' at '${path2}'`), listener;
716
+ }
717
717
  /**
718
718
  * Add a listener to the registry and create a listener for client.
719
719
  * @param listener Listener to add.
@@ -1001,10 +1001,10 @@ var Instance = class {
1001
1001
  let target = type.split(":")[1], { listeners, commands } = this.client.managers;
1002
1002
  switch (target) {
1003
1003
  case "listeners":
1004
- listeners.unload(file), await listeners.load(file);
1004
+ await listeners.reload(file);
1005
1005
  break;
1006
1006
  case "commands":
1007
- commands.unload(file), await listeners.load(file);
1007
+ await commands.reload(file);
1008
1008
  break;
1009
1009
  }
1010
1010
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bakit",
3
- "version": "2.0.0-alpha.13",
3
+ "version": "2.0.0-alpha.15",
4
4
  "description": "A framework for discord.js",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",