bakit 2.0.0-alpha.12 → 2.0.0-alpha.14

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.
@@ -0,0 +1,100 @@
1
+ import { config } from 'dotenv';
2
+ import { program } from 'commander';
3
+ import { fork } from 'child_process';
4
+ import chokidar from 'chokidar';
5
+ import path, { resolve, relative, sep } from 'path';
6
+ import { createJiti } from 'jiti';
7
+
8
+ // src/bin/bakit.ts
9
+ var Module = class {
10
+ static jiti = createJiti(process.cwd());
11
+ static async import(module, defaultImport = false) {
12
+ let path2 = resolve(module);
13
+ try {
14
+ return await this.jiti.import(path2, { default: defaultImport });
15
+ } catch (error) {
16
+ return console.error(`[Module] Import failed for ${path2}:`, error), null;
17
+ }
18
+ }
19
+ static isLoaded(module) {
20
+ let path2 = resolve(module);
21
+ return !!path2 && !!this.jiti.cache[path2];
22
+ }
23
+ static unload(module) {
24
+ let path2 = resolve(module);
25
+ return !path2 || !this.jiti.cache[path2] ? false : (delete this.jiti.cache[path2], true);
26
+ }
27
+ static getTopLevel(path2, entryDir) {
28
+ return relative(entryDir, path2).split(sep)[0] ?? null;
29
+ }
30
+ };
31
+
32
+ // src/base/process/DevProcessManager.ts
33
+ var DevProcessManager = class {
34
+ constructor(options) {
35
+ this.options = options;
36
+ }
37
+ child = null;
38
+ restartTimer = null;
39
+ start() {
40
+ console.log("Starting bakit in dev mode..."), this.startChild(), this.startWatcher();
41
+ }
42
+ startChild() {
43
+ if (this.child) return;
44
+ let entry = path.resolve(this.options.entry);
45
+ this.child = fork(entry, {
46
+ execArgv: ["--import", "tsx"],
47
+ stdio: "inherit",
48
+ env: {
49
+ ...process.env,
50
+ NODE_ENV: "development"
51
+ }
52
+ }), this.child.on("exit", () => {
53
+ this.child = null;
54
+ });
55
+ }
56
+ restartChild() {
57
+ if (!this.child)
58
+ return this.startChild();
59
+ let old = this.child;
60
+ old.once("exit", () => {
61
+ this.child = null, this.startChild();
62
+ }), old.kill("SIGTERM");
63
+ }
64
+ startWatcher() {
65
+ let { rootDir } = this.options;
66
+ chokidar.watch(rootDir, {
67
+ ignoreInitial: true,
68
+ awaitWriteFinish: {
69
+ stabilityThreshold: 200,
70
+ pollInterval: 50
71
+ }
72
+ }).on("change", (file) => this.onFileChanged(file));
73
+ }
74
+ onFileChanged(file) {
75
+ if (!this.child)
76
+ return;
77
+ let top = Module.getTopLevel(file, this.options.rootDir);
78
+ if (top && this.options.hotDirs.includes(top)) {
79
+ this.child.connected && this.child.send({ type: `hmr:${top}`, file });
80
+ return;
81
+ }
82
+ this.scheduleRestart();
83
+ }
84
+ scheduleRestart() {
85
+ this.restartTimer && clearTimeout(this.restartTimer), this.restartTimer = setTimeout(() => {
86
+ console.log("Detected changes, restarting..."), this.restartChild(), this.restartTimer = null;
87
+ }, 150);
88
+ }
89
+ };
90
+
91
+ // src/bin/bakit.ts
92
+ program.name("bakit");
93
+ program.command("dev").action(() => {
94
+ config({ path: [".env.local", ".env"], quiet: true }), new DevProcessManager({
95
+ rootDir: "src",
96
+ entry: "src/index.ts",
97
+ hotDirs: ["commands", "listeners"]
98
+ }).start();
99
+ });
100
+ program.parse();
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
 
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,10 +589,7 @@ 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);
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);
602
593
  return console.log(`[Loader] Loaded ${filtered.length}/${files.length}`), filtered;
603
594
  }
604
595
  /**
@@ -681,10 +672,7 @@ var ListenerManager = class extends BaseClientManager {
681
672
  entries = new Collection();
682
673
  executors = /* @__PURE__ */ new WeakMap();
683
674
  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);
675
+ 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);
688
676
  return console.log(`[Loader] Loaded ${filtered.length}/${files.length}`), filtered;
689
677
  }
690
678
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bakit",
3
- "version": "2.0.0-alpha.12",
3
+ "version": "2.0.0-alpha.14",
4
4
  "description": "A framework for discord.js",
5
5
  "type": "module",
6
6
  "types": "./dist/index.d.ts",