bakit 2.0.0-alpha.17 → 2.0.0-alpha.19
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/cli.js +5 -6
- package/dist/index.d.ts +1 -4
- package/dist/index.js +36 -26
- package/dist/loader/hooks.js +75 -0
- package/dist/loader/register.js +14 -0
- package/package.json +11 -4
package/dist/cli.js
CHANGED
|
@@ -3,15 +3,12 @@ import { program } from 'commander';
|
|
|
3
3
|
import { fork } from 'child_process';
|
|
4
4
|
import chokidar from 'chokidar';
|
|
5
5
|
import path, { relative, sep } from 'path';
|
|
6
|
-
import {
|
|
6
|
+
import { pathToFileURL } from 'url';
|
|
7
7
|
|
|
8
8
|
// src/cli/bin.ts
|
|
9
|
-
createJiti(process.cwd());
|
|
10
9
|
function getTopLevelDirectory(path2, entryDir) {
|
|
11
10
|
return relative(entryDir, path2).split(sep)[0] ?? null;
|
|
12
11
|
}
|
|
13
|
-
|
|
14
|
-
// src/cli/process/DevProcessManager.ts
|
|
15
12
|
var DevProcessManager = class {
|
|
16
13
|
constructor(options) {
|
|
17
14
|
this.options = options;
|
|
@@ -25,7 +22,7 @@ var DevProcessManager = class {
|
|
|
25
22
|
if (this.child) return;
|
|
26
23
|
let entry = path.resolve(this.options.entry);
|
|
27
24
|
this.child = fork(entry, {
|
|
28
|
-
execArgv: ["--import", "
|
|
25
|
+
execArgv: ["--import", "bakit/loader/register"],
|
|
29
26
|
stdio: "inherit",
|
|
30
27
|
env: {
|
|
31
28
|
...process.env,
|
|
@@ -51,7 +48,9 @@ var DevProcessManager = class {
|
|
|
51
48
|
stabilityThreshold: 200,
|
|
52
49
|
pollInterval: 50
|
|
53
50
|
}
|
|
54
|
-
}).on("change", (file) =>
|
|
51
|
+
}).on("change", (file) => {
|
|
52
|
+
this.onFileChanged(pathToFileURL(file).href);
|
|
53
|
+
});
|
|
55
54
|
}
|
|
56
55
|
onFileChanged(file) {
|
|
57
56
|
if (!this.child)
|
package/dist/index.d.ts
CHANGED
|
@@ -447,9 +447,6 @@ declare function tokenize(content: string): string[];
|
|
|
447
447
|
*/
|
|
448
448
|
declare function extractSnowflakeId(input: string): string | null;
|
|
449
449
|
|
|
450
|
-
declare function importModule<T>(modulePath: string, defaultImport?: boolean): Promise<T | null>;
|
|
451
|
-
declare function isModuleLoaded(modulePath: string): boolean;
|
|
452
|
-
declare function unloadModule(modulePath: string): boolean;
|
|
453
450
|
declare function getTopLevelDirectory(path: string, entryDir: string): string | null;
|
|
454
451
|
|
|
455
452
|
declare const messageCommandHandler: Listener<Events.MessageCreate>;
|
|
@@ -482,4 +479,4 @@ declare function loadConfig(cwd?: string): Promise<ProjectConfig>;
|
|
|
482
479
|
*/
|
|
483
480
|
declare function getConfig(): ProjectConfig;
|
|
484
481
|
|
|
485
|
-
export { type AnyParam, ArgumentError, BakitClient, type BakitClientEvents, BakitError, BaseClientManager, BaseCommandContext, BaseParam, type BaseParamOptions, BaseParamSchema, 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, Instance, LifecycleManager, Listener, ListenerManager, type ListenerOptions, ListenerOptionsSchema, type MainHookCallback, MessageContext, type MessageContextSendOptions, type NumberOptions, NumberParam, NumberParamSchema, type ParamResolvedOutputType, ParamUserType, Params, ProjectCacheManager, type ProjectConfig, type ProjectConfigInput, ProjectConfigSchema, type StringOptions, StringParam, StringParamSchema, type UserOptions, UserParam, UserParamSchema, chatInputCommandHandler, defineCommand, defineConfig, defineListener, extractSnowflakeId, getConfig, getTopLevelDirectory,
|
|
482
|
+
export { type AnyParam, ArgumentError, BakitClient, type BakitClientEvents, BakitError, BaseClientManager, BaseCommandContext, BaseParam, type BaseParamOptions, BaseParamSchema, 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, Instance, LifecycleManager, Listener, ListenerManager, type ListenerOptions, ListenerOptionsSchema, type MainHookCallback, MessageContext, type MessageContextSendOptions, type NumberOptions, NumberParam, NumberParamSchema, type ParamResolvedOutputType, ParamUserType, Params, ProjectCacheManager, type ProjectConfig, type ProjectConfigInput, ProjectConfigSchema, type StringOptions, StringParam, StringParamSchema, type UserOptions, UserParam, UserParamSchema, chatInputCommandHandler, defineCommand, defineConfig, defineListener, extractSnowflakeId, getConfig, getTopLevelDirectory, loadConfig, messageCommandHandler, registerCommandsHandler, tokenize, useApp, validateParamsOrder };
|
package/dist/index.js
CHANGED
|
@@ -1,14 +1,41 @@
|
|
|
1
|
+
import { register } from 'module';
|
|
2
|
+
import { MessageChannel } from 'worker_threads';
|
|
1
3
|
import { GatewayIntentBits, Events, Client, Collection, IntentsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandNumberOption, SlashCommandUserOption, ChatInputCommandInteraction, Message } from 'discord.js';
|
|
2
4
|
import { inspect } from 'util';
|
|
3
|
-
import { posix,
|
|
5
|
+
import { posix, relative, sep, join, dirname } from 'path';
|
|
4
6
|
import glob from 'tiny-glob';
|
|
5
7
|
import z4 from 'zod';
|
|
6
|
-
import { createJiti } from 'jiti';
|
|
7
8
|
import { existsSync, mkdirSync, rmSync } from 'fs';
|
|
8
9
|
import { mkdir, writeFile, readFile, rm } from 'fs/promises';
|
|
9
10
|
import { createHash } from 'crypto';
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
var __defProp = Object.defineProperty;
|
|
13
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
14
|
+
var __esm = (fn, res) => function() {
|
|
15
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
16
|
+
};
|
|
17
|
+
var __export = (target, all) => {
|
|
18
|
+
for (var name in all)
|
|
19
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/loader/register.ts
|
|
23
|
+
var register_exports = {};
|
|
24
|
+
__export(register_exports, {
|
|
25
|
+
unload: () => unload
|
|
26
|
+
});
|
|
27
|
+
function unload(module) {
|
|
28
|
+
port2.postMessage({ type: "unload", target: module });
|
|
29
|
+
}
|
|
30
|
+
var port1, port2, hookPath, init_register = __esm({
|
|
31
|
+
"src/loader/register.ts"() {
|
|
32
|
+
(({ port1, port2 } = new MessageChannel())), hookPath = new URL("./hooks.js", import.meta.url).href;
|
|
33
|
+
register(hookPath, import.meta.url, {
|
|
34
|
+
data: { port: port1 },
|
|
35
|
+
transferList: [port1]
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
});
|
|
12
39
|
var ParamUserType = /* @__PURE__ */ ((ParamUserType2) => (ParamUserType2.Bot = "bot", ParamUserType2.Normal = "normal", ParamUserType2.Any = "any", ParamUserType2))(ParamUserType || {}), BaseParamSchema = z4.object({
|
|
13
40
|
name: z4.string(),
|
|
14
41
|
description: z4.string().optional(),
|
|
@@ -159,23 +186,6 @@ function extractSnowflakeId(input) {
|
|
|
159
186
|
let idMatch = /^(\d{17,20})$/.exec(input);
|
|
160
187
|
return idMatch?.[1] ? idMatch[1] : null;
|
|
161
188
|
}
|
|
162
|
-
var jiti = createJiti(process.cwd());
|
|
163
|
-
async function importModule(modulePath, defaultImport = false) {
|
|
164
|
-
let path = resolve(modulePath);
|
|
165
|
-
try {
|
|
166
|
-
return await jiti.import(path, { default: defaultImport });
|
|
167
|
-
} catch (error) {
|
|
168
|
-
return console.error(`[Module] Import failed for ${path}:`, error), null;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
function isModuleLoaded(modulePath) {
|
|
172
|
-
let path = resolve(modulePath);
|
|
173
|
-
return !!path && !!jiti.cache[path];
|
|
174
|
-
}
|
|
175
|
-
function unloadModule(modulePath) {
|
|
176
|
-
let path = resolve(modulePath);
|
|
177
|
-
return !path || !jiti.cache[path] ? false : (delete jiti.cache[path], true);
|
|
178
|
-
}
|
|
179
189
|
function getTopLevelDirectory(path, entryDir) {
|
|
180
190
|
return relative(entryDir, path).split(sep)[0] ?? null;
|
|
181
191
|
}
|
|
@@ -476,7 +486,7 @@ var CommandManager = class extends BaseClientManager {
|
|
|
476
486
|
* @returns The command object if added successfully.
|
|
477
487
|
*/
|
|
478
488
|
async load(path) {
|
|
479
|
-
let command = await
|
|
489
|
+
let command = (await import(path)).default;
|
|
480
490
|
if (!command) {
|
|
481
491
|
console.warn(`[Loader] File has no default export: ${path}`);
|
|
482
492
|
return;
|
|
@@ -494,7 +504,7 @@ var CommandManager = class extends BaseClientManager {
|
|
|
494
504
|
*/
|
|
495
505
|
async unload(path) {
|
|
496
506
|
let command = this.entries.get(path);
|
|
497
|
-
if (
|
|
507
|
+
if (this.entries.delete(path), (await Promise.resolve().then(() => (init_register(), register_exports))).unload(path), !!command)
|
|
498
508
|
return this.remove(command);
|
|
499
509
|
}
|
|
500
510
|
async reload(path) {
|
|
@@ -575,7 +585,7 @@ var ListenerManager = class extends BaseClientManager {
|
|
|
575
585
|
* @returns The listener object if added successfully.
|
|
576
586
|
*/
|
|
577
587
|
async load(path) {
|
|
578
|
-
let listener = await
|
|
588
|
+
let listener = (await import(path)).default;
|
|
579
589
|
if (!listener) {
|
|
580
590
|
console.warn(`[Loader] File has no default export: ${path}`);
|
|
581
591
|
return;
|
|
@@ -593,7 +603,7 @@ var ListenerManager = class extends BaseClientManager {
|
|
|
593
603
|
*/
|
|
594
604
|
async unload(path) {
|
|
595
605
|
let listener = this.entries.get(path);
|
|
596
|
-
if (
|
|
606
|
+
if (this.entries.delete(path), (await Promise.resolve().then(() => (init_register(), register_exports))).unload(path), !!listener)
|
|
597
607
|
return this.remove(listener)?.[0];
|
|
598
608
|
}
|
|
599
609
|
async reload(path) {
|
|
@@ -793,7 +803,7 @@ async function loadConfig(cwd = process.cwd()) {
|
|
|
793
803
|
if (!configPath)
|
|
794
804
|
throw new Error("Missing config file");
|
|
795
805
|
other && console.warn(`Multiple config files found in ${cwd}. Using ${configPath}.`);
|
|
796
|
-
let config = await
|
|
806
|
+
let config = (await import(configPath)).default;
|
|
797
807
|
return _config = Object.freeze(await ProjectConfigSchema.parseAsync(config)), _config;
|
|
798
808
|
}
|
|
799
809
|
function getConfig() {
|
|
@@ -952,4 +962,4 @@ var Params = {
|
|
|
952
962
|
user: createFactory(UserParam)
|
|
953
963
|
};
|
|
954
964
|
|
|
955
|
-
export { 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, StringParam, StringParamSchema, UserParam, UserParamSchema, chatInputCommandHandler, defineCommand, defineConfig, defineListener, extractSnowflakeId, getConfig, getTopLevelDirectory,
|
|
965
|
+
export { 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, StringParam, StringParamSchema, UserParam, UserParamSchema, chatInputCommandHandler, defineCommand, defineConfig, defineListener, extractSnowflakeId, getConfig, getTopLevelDirectory, loadConfig, messageCommandHandler, registerCommandsHandler, tokenize, useApp, validateParamsOrder };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { existsSync } from 'fs';
|
|
2
|
+
import { readFile } from 'fs/promises';
|
|
3
|
+
import { Module } from 'module';
|
|
4
|
+
import { dirname, resolve as resolve$1, basename } from 'path';
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from 'url';
|
|
6
|
+
|
|
7
|
+
// src/loader/hooks.ts
|
|
8
|
+
var EXTENSIONS = [".js", ".ts"], inDev = false, parentPort, versions, esbuild;
|
|
9
|
+
async function initialize({ port }) {
|
|
10
|
+
inDev = process.env.NODE_ENV === "development", inDev && (parentPort = port, parentPort.on("message", onMessage), versions = /* @__PURE__ */ new Map(), esbuild = await import('esbuild'));
|
|
11
|
+
}
|
|
12
|
+
async function resolve(specifier, context, nextResolve) {
|
|
13
|
+
if (shouldSkip(specifier))
|
|
14
|
+
return nextResolve(specifier, context);
|
|
15
|
+
let url = specifier, parentURL = context.parentURL?.split("?")[0], baseDir = parentURL ? dirname(fileURLToPath(parentURL)) : process.cwd();
|
|
16
|
+
if (specifier.startsWith(".")) {
|
|
17
|
+
let absPath = resolve$1(baseDir, specifier);
|
|
18
|
+
if (!existsSync(absPath) && absPath.endsWith(".js")) {
|
|
19
|
+
let tsPath = absPath.slice(0, -3) + ".ts";
|
|
20
|
+
existsSync(tsPath) && (absPath = tsPath);
|
|
21
|
+
}
|
|
22
|
+
url = pathToFileURL(absPath).href;
|
|
23
|
+
}
|
|
24
|
+
let urlObj = new URL(url);
|
|
25
|
+
if (inDev) {
|
|
26
|
+
let filePath = fileURLToPath(urlObj), version = createVersion(filePath);
|
|
27
|
+
urlObj.searchParams.set("hmr", version), parentURL && parentPort && parentPort.postMessage({
|
|
28
|
+
type: "dependency",
|
|
29
|
+
parent: parentURL,
|
|
30
|
+
child: url
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
url: urlObj.href,
|
|
35
|
+
shortCircuit: true,
|
|
36
|
+
format: "module"
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
async function load(url, context, nextLoad) {
|
|
40
|
+
if (shouldSkip(url))
|
|
41
|
+
return nextLoad(url, context);
|
|
42
|
+
let cleanURL = url.split("?")[0], filePath = fileURLToPath(cleanURL ?? "");
|
|
43
|
+
if (filePath.endsWith(".ts") && esbuild) {
|
|
44
|
+
let raw = await readFile(filePath, "utf8");
|
|
45
|
+
return {
|
|
46
|
+
source: (await esbuild.transform(raw, {
|
|
47
|
+
platform: "node",
|
|
48
|
+
sourcefile: filePath,
|
|
49
|
+
sourcemap: "inline",
|
|
50
|
+
loader: "ts"
|
|
51
|
+
})).code,
|
|
52
|
+
format: "module",
|
|
53
|
+
shortCircuit: true
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return nextLoad(url, context);
|
|
57
|
+
}
|
|
58
|
+
function createVersion(filename) {
|
|
59
|
+
let version = versions?.get(filename);
|
|
60
|
+
return version || (version = Date.now().toString(), versions?.set(filename, version)), version;
|
|
61
|
+
}
|
|
62
|
+
function shouldSkip(specifier) {
|
|
63
|
+
if (Module.isBuiltin(specifier))
|
|
64
|
+
return true;
|
|
65
|
+
if (specifier.startsWith(".") || specifier.startsWith("file://")) {
|
|
66
|
+
let filePath = specifier.startsWith("file://") ? fileURLToPath(specifier) : specifier, filename = basename(filePath);
|
|
67
|
+
return !EXTENSIONS.some((ext) => filename.endsWith(ext));
|
|
68
|
+
}
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
function onMessage(message) {
|
|
72
|
+
message.type === "unload" && versions?.delete(fileURLToPath(message.target));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { initialize, load, resolve };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { register } from 'module';
|
|
2
|
+
import { MessageChannel } from 'worker_threads';
|
|
3
|
+
|
|
4
|
+
// src/loader/register.ts
|
|
5
|
+
var { port1, port2 } = new MessageChannel(), hookPath = new URL("./hooks.js", import.meta.url).href;
|
|
6
|
+
register(hookPath, import.meta.url, {
|
|
7
|
+
data: { port: port1 },
|
|
8
|
+
transferList: [port1]
|
|
9
|
+
});
|
|
10
|
+
function unload(module) {
|
|
11
|
+
port2.postMessage({ type: "unload", target: module });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { unload };
|
package/package.json
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bakit",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.19",
|
|
4
4
|
"description": "A framework for discord.js",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
7
|
-
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts"
|
|
10
|
+
},
|
|
11
|
+
"./loader/*": "./dist/loader/*.js"
|
|
12
|
+
},
|
|
8
13
|
"scripts": {
|
|
9
14
|
"build": "tsup",
|
|
10
15
|
"type-check": "tsc --noEmit",
|
|
@@ -36,11 +41,13 @@
|
|
|
36
41
|
"peerDependencies": {
|
|
37
42
|
"discord.js": "^14.0.0"
|
|
38
43
|
},
|
|
44
|
+
"optionalDependencies": {
|
|
45
|
+
"esbuild": "^0.27.0"
|
|
46
|
+
},
|
|
39
47
|
"dependencies": {
|
|
40
48
|
"chokidar": "^5.0.0",
|
|
41
49
|
"commander": "^14.0.2",
|
|
42
50
|
"dotenv": "^17.2.1",
|
|
43
|
-
"jiti": "^2.6.1",
|
|
44
51
|
"tiny-glob": "^0.2.9",
|
|
45
52
|
"type-fest": "^4.41.0",
|
|
46
53
|
"zod": "^4.1.12"
|