bakit 2.0.0-alpha.16 → 2.0.0-alpha.18
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 +28 -5
- package/dist/index.js +40 -26
- package/dist/loader/hooks.js +1636 -0
- package/dist/loader/register.js +16 -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
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as discord_js from 'discord.js';
|
|
2
|
-
import { ChatInputCommandInteraction, CacheType, Message, User, MessageCreateOptions, InteractionReplyOptions, Awaitable, Collection, Events, IntentsBitField, ClientEvents, Client, ClientOptions } from 'discord.js';
|
|
2
|
+
import { ChatInputCommandInteraction, CacheType, Message, User, MessageCreateOptions, InteractionReplyOptions, Awaitable, Collection, Events, IntentsBitField, ClientEvents, Client, ClientOptions, GatewayIntentBits } from 'discord.js';
|
|
3
3
|
import { inspect } from 'node:util';
|
|
4
4
|
import z from 'zod';
|
|
5
5
|
|
|
@@ -447,13 +447,36 @@ 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>;
|
|
456
453
|
declare const chatInputCommandHandler: Listener<Events.InteractionCreate>;
|
|
457
454
|
declare const registerCommandsHandler: Listener<Events.ClientReady>;
|
|
458
455
|
|
|
459
|
-
|
|
456
|
+
declare const ProjectConfigSchema: z.ZodObject<{
|
|
457
|
+
intents: z.ZodDefault<z.ZodUnion<readonly [z.ZodLiteral<"auto">, z.ZodBigInt, z.ZodArray<z.ZodEnum<typeof GatewayIntentBits>>]>>;
|
|
458
|
+
clientOptions: z.ZodOptional<z.ZodCustom<Omit<ClientOptions, "intents">, Omit<ClientOptions, "intents">>>;
|
|
459
|
+
prefixes: z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
460
|
+
token: z.ZodString;
|
|
461
|
+
}, z.z.core.$strip>;
|
|
462
|
+
type ProjectConfigInput = z.input<typeof ProjectConfigSchema>;
|
|
463
|
+
type ProjectConfig = z.output<typeof ProjectConfigSchema>;
|
|
464
|
+
/**
|
|
465
|
+
* Define config object for your project. This is just a cleaner way to define config.
|
|
466
|
+
* @param config The partial version of project config.
|
|
467
|
+
* @returns The same config you provided earlier.
|
|
468
|
+
*/
|
|
469
|
+
declare function defineConfig(config: ProjectConfigInput): ProjectConfigInput;
|
|
470
|
+
/**
|
|
471
|
+
* Load the config file and save them for later usage.
|
|
472
|
+
* @param cwd The location of the config file, uses root by default.
|
|
473
|
+
* @returns The complete config with default values from the validation.
|
|
474
|
+
*/
|
|
475
|
+
declare function loadConfig(cwd?: string): Promise<ProjectConfig>;
|
|
476
|
+
/**
|
|
477
|
+
* Get the loaded config of the project.
|
|
478
|
+
* @returns The project config.
|
|
479
|
+
*/
|
|
480
|
+
declare function getConfig(): ProjectConfig;
|
|
481
|
+
|
|
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,42 @@
|
|
|
1
|
+
import { register } from 'module';
|
|
2
|
+
import { posix, resolve, relative, sep, join, dirname } from 'path';
|
|
3
|
+
import { pathToFileURL } from 'url';
|
|
4
|
+
import { MessageChannel } from 'worker_threads';
|
|
1
5
|
import { GatewayIntentBits, Events, Client, Collection, IntentsBitField, SlashCommandBuilder, SlashCommandStringOption, SlashCommandNumberOption, SlashCommandUserOption, ChatInputCommandInteraction, Message } from 'discord.js';
|
|
2
6
|
import { inspect } from 'util';
|
|
3
|
-
import { posix, resolve, relative, sep, join, dirname } from 'path';
|
|
4
7
|
import glob from 'tiny-glob';
|
|
5
8
|
import z4 from 'zod';
|
|
6
|
-
import { createJiti } from 'jiti';
|
|
7
9
|
import { existsSync, mkdirSync, rmSync } from 'fs';
|
|
8
10
|
import { mkdir, writeFile, readFile, rm } from 'fs/promises';
|
|
9
11
|
import { createHash } from 'crypto';
|
|
10
12
|
|
|
11
|
-
|
|
13
|
+
var __defProp = Object.defineProperty;
|
|
14
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
15
|
+
var __esm = (fn, res) => function() {
|
|
16
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
17
|
+
};
|
|
18
|
+
var __export = (target, all) => {
|
|
19
|
+
for (var name in all)
|
|
20
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/loader/register.ts
|
|
24
|
+
var register_exports = {};
|
|
25
|
+
__export(register_exports, {
|
|
26
|
+
unload: () => unload
|
|
27
|
+
});
|
|
28
|
+
function unload(module) {
|
|
29
|
+
port2.postMessage({ type: "unload", target: module });
|
|
30
|
+
}
|
|
31
|
+
var port1, port2, hookPath, init_register = __esm({
|
|
32
|
+
"src/loader/register.ts"() {
|
|
33
|
+
(({ port1, port2 } = new MessageChannel())), hookPath = pathToFileURL(resolve("./loader.js")).href;
|
|
34
|
+
register(hookPath, import.meta.url, {
|
|
35
|
+
data: { port: port1 },
|
|
36
|
+
transferList: [port1]
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
12
40
|
var ParamUserType = /* @__PURE__ */ ((ParamUserType2) => (ParamUserType2.Bot = "bot", ParamUserType2.Normal = "normal", ParamUserType2.Any = "any", ParamUserType2))(ParamUserType || {}), BaseParamSchema = z4.object({
|
|
13
41
|
name: z4.string(),
|
|
14
42
|
description: z4.string().optional(),
|
|
@@ -159,23 +187,6 @@ function extractSnowflakeId(input) {
|
|
|
159
187
|
let idMatch = /^(\d{17,20})$/.exec(input);
|
|
160
188
|
return idMatch?.[1] ? idMatch[1] : null;
|
|
161
189
|
}
|
|
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
190
|
function getTopLevelDirectory(path, entryDir) {
|
|
180
191
|
return relative(entryDir, path).split(sep)[0] ?? null;
|
|
181
192
|
}
|
|
@@ -476,7 +487,7 @@ var CommandManager = class extends BaseClientManager {
|
|
|
476
487
|
* @returns The command object if added successfully.
|
|
477
488
|
*/
|
|
478
489
|
async load(path) {
|
|
479
|
-
let command = await
|
|
490
|
+
let command = (await import(path)).default;
|
|
480
491
|
if (!command) {
|
|
481
492
|
console.warn(`[Loader] File has no default export: ${path}`);
|
|
482
493
|
return;
|
|
@@ -494,7 +505,7 @@ var CommandManager = class extends BaseClientManager {
|
|
|
494
505
|
*/
|
|
495
506
|
async unload(path) {
|
|
496
507
|
let command = this.entries.get(path);
|
|
497
|
-
if (
|
|
508
|
+
if (this.entries.delete(path), (await Promise.resolve().then(() => (init_register(), register_exports))).unload(path), !!command)
|
|
498
509
|
return this.remove(command);
|
|
499
510
|
}
|
|
500
511
|
async reload(path) {
|
|
@@ -575,7 +586,7 @@ var ListenerManager = class extends BaseClientManager {
|
|
|
575
586
|
* @returns The listener object if added successfully.
|
|
576
587
|
*/
|
|
577
588
|
async load(path) {
|
|
578
|
-
let listener = await
|
|
589
|
+
let listener = (await import(path)).default;
|
|
579
590
|
if (!listener) {
|
|
580
591
|
console.warn(`[Loader] File has no default export: ${path}`);
|
|
581
592
|
return;
|
|
@@ -593,7 +604,7 @@ var ListenerManager = class extends BaseClientManager {
|
|
|
593
604
|
*/
|
|
594
605
|
async unload(path) {
|
|
595
606
|
let listener = this.entries.get(path);
|
|
596
|
-
if (
|
|
607
|
+
if (this.entries.delete(path), (await Promise.resolve().then(() => (init_register(), register_exports))).unload(path), !!listener)
|
|
597
608
|
return this.remove(listener)?.[0];
|
|
598
609
|
}
|
|
599
610
|
async reload(path) {
|
|
@@ -778,6 +789,9 @@ var ProjectConfigSchema = z4.object({
|
|
|
778
789
|
prefixes: z4.array(z4.string()).default([]),
|
|
779
790
|
token: z4.string()
|
|
780
791
|
});
|
|
792
|
+
function defineConfig(config) {
|
|
793
|
+
return config;
|
|
794
|
+
}
|
|
781
795
|
var _config;
|
|
782
796
|
async function loadConfig(cwd = process.cwd()) {
|
|
783
797
|
if (_config)
|
|
@@ -790,7 +804,7 @@ async function loadConfig(cwd = process.cwd()) {
|
|
|
790
804
|
if (!configPath)
|
|
791
805
|
throw new Error("Missing config file");
|
|
792
806
|
other && console.warn(`Multiple config files found in ${cwd}. Using ${configPath}.`);
|
|
793
|
-
let config = await
|
|
807
|
+
let config = (await import(configPath)).default;
|
|
794
808
|
return _config = Object.freeze(await ProjectConfigSchema.parseAsync(config)), _config;
|
|
795
809
|
}
|
|
796
810
|
function getConfig() {
|
|
@@ -949,4 +963,4 @@ var Params = {
|
|
|
949
963
|
user: createFactory(UserParam)
|
|
950
964
|
};
|
|
951
965
|
|
|
952
|
-
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, StringParam, StringParamSchema, UserParam, UserParamSchema, chatInputCommandHandler, defineCommand, defineListener, extractSnowflakeId,
|
|
966
|
+
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 };
|