Skip to content

Commit 5803ad9

Browse files
committed
feat: improvements to the cli
1 parent 2d0e9a4 commit 5803ad9

33 files changed

+1055
-97
lines changed

apps/test-bot/src/app/commands/(actions)/dm-only.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { SlashCommandProps, CommandData, dmOnly } from 'commandkit';
22

3-
export const data: CommandData = {
3+
export const command: CommandData = {
44
name: 'dm-only',
55
description: 'This is a dm only command',
66
};
77

8-
export async function run({ interaction }: SlashCommandProps) {
8+
export async function chatInput({ interaction }: SlashCommandProps) {
99
dmOnly();
1010

1111
await interaction.reply('This command can only be used in a dm.');

apps/test-bot/src/app/commands/(actions)/giveaway.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
AutocompleteProps,
1212
} from 'commandkit';
1313

14-
export const data: CommandData = {
14+
export const command: CommandData = {
1515
name: 'ping',
1616
description: 'Pong!',
1717
options: [
@@ -42,7 +42,7 @@ export async function autocomplete({ interaction }: AutocompleteProps) {
4242
interaction.respond(filtered);
4343
}
4444

45-
export async function run({ interaction, client }: SlashCommandProps) {
45+
export async function chatInput({ interaction, client }: SlashCommandProps) {
4646
if (!interaction.channel) return;
4747

4848
const button = new ButtonKit()

apps/test-bot/src/app/commands/(actions)/guild-only.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { SlashCommandProps, CommandData, guildOnly } from 'commandkit';
22

3-
export const data: CommandData = {
3+
export const command: CommandData = {
44
name: 'guild-only',
55
description: 'This is a guild only command.',
66
};
77

8-
export async function run({ interaction }: SlashCommandProps) {
8+
export async function chatInput({ interaction }: SlashCommandProps) {
99
guildOnly();
1010

1111
await interaction.reply('This command can only be used in a guild.');

apps/test-bot/src/app/commands/(developer)/reload.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { SlashCommandProps, CommandOptions, CommandData } from 'commandkit';
22

3-
export const data: CommandData = {
3+
export const command: CommandData = {
44
name: 'reload',
55
description: 'Reload commands, events, and validations.',
66
};
77

8-
export async function run({ interaction, handler }: SlashCommandProps) {
8+
export async function chatInput({ interaction, handler }: SlashCommandProps) {
99
await interaction.deferReply({ ephemeral: true });
1010

1111
// await handler.reloadCommands();

apps/test-bot/src/app/commands/(developer)/run-after.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { SlashCommandProps, CommandData, afterCommand } from 'commandkit';
22

3-
export const data: CommandData = {
3+
export const command: CommandData = {
44
name: 'run-after',
55
description: 'This is a run-after command',
66
};
77

8-
export async function run({ interaction }: SlashCommandProps) {
8+
export async function chatInput({ interaction }: SlashCommandProps) {
99
afterCommand((env) => {
1010
console.log(
1111
`The command ${interaction.commandName} was executed successfully in ${env

apps/test-bot/src/app/commands/(general)/help.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SlashCommandProps, CommandData } from 'commandkit';
22

3-
export const data: CommandData = {
3+
export const command: CommandData = {
44
name: 'help',
55
description: 'This is a help command.',
66
};
@@ -12,7 +12,7 @@ function $botVersion(): string {
1212
return require(path).version;
1313
}
1414

15-
export async function run({ interaction, handler }: SlashCommandProps) {
15+
export async function chatInput({ interaction, handler }: SlashCommandProps) {
1616
await interaction.deferReply();
1717

1818
const botVersion = $botVersion();

apps/test-bot/src/app/commands/(general)/ping.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
AutocompleteProps,
1212
} from 'commandkit';
1313

14-
export const data: CommandData = {
14+
export const command: CommandData = {
1515
name: 'ping',
1616
description: 'Pong!',
1717
options: [
@@ -42,7 +42,7 @@ export async function autocomplete({ interaction }: AutocompleteProps) {
4242
interaction.respond(filtered);
4343
}
4444

45-
export async function run({ interaction, client }: SlashCommandProps) {
45+
export async function chatInput({ interaction, client }: SlashCommandProps) {
4646
if (!interaction.channel) return;
4747

4848
const button = new ButtonKit()

apps/test-bot/src/app/commands/(interactions)/commandkit.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import CommandKit, {
77
} from 'commandkit';
88
import { MessageFlags } from 'discord.js';
99

10-
export const data: CommandData = {
10+
export const command: CommandData = {
1111
name: 'commandkit',
1212
description: 'This is a commandkit command.',
1313
};
@@ -40,7 +40,7 @@ function ButtonGrid() {
4040
);
4141
}
4242

43-
export async function run({ interaction }: SlashCommandProps) {
43+
export async function chatInput({ interaction }: SlashCommandProps) {
4444
await interaction.deferReply();
4545

4646
await interaction.editReply({

apps/test-bot/src/app/commands/(interactions)/confirmation.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import CommandKit, {
77
} from 'commandkit';
88
import { ButtonStyle, MessageFlags } from 'discord.js';
99

10-
export const data: CommandData = {
10+
export const command: CommandData = {
1111
name: 'confirm',
1212
description: 'This is a confirm command.',
1313
};
@@ -30,7 +30,7 @@ const handleCancel: OnButtonKitClick = async (interaction, context) => {
3030
context.dispose();
3131
};
3232

33-
export async function run({ interaction }: SlashCommandProps) {
33+
export async function chatInput({ interaction }: SlashCommandProps) {
3434
const buttons = (
3535
<ActionRow>
3636
<Button onClick={handleCancel} style={ButtonStyle.Primary}>

apps/test-bot/src/app/commands/(leveling)/xp.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { SlashCommandProps, CommandData, cacheTag } from 'commandkit';
22
import { database } from '../../../database/store.ts';
33

4-
export const data: CommandData = {
4+
export const command: CommandData = {
55
name: 'xp',
66
description: 'This is an xp command.',
77
};
@@ -17,7 +17,7 @@ async function getUserXP(guildId: string, userId: string) {
1717
return xp;
1818
}
1919

20-
export async function run({ interaction }: SlashCommandProps) {
20+
export async function chatInput({ interaction }: SlashCommandProps) {
2121
await interaction.deferReply();
2222

2323
const dataRetrievalStart = Date.now();

apps/test-bot/src/app/commands/random/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SlashCommandProps, CommandData, cache } from 'commandkit';
22

3-
export const data: CommandData = {
3+
export const command: CommandData = {
44
name: 'random',
55
description: 'This is a random command.',
66
};
@@ -12,7 +12,7 @@ const random = cache(
1212
{ name: 'random', ttl: 60_000 },
1313
);
1414

15-
export async function run({ interaction }: SlashCommandProps) {
15+
export async function chatInput({ interaction }: SlashCommandProps) {
1616
await interaction.deferReply();
1717

1818
const xp = await random();

apps/test-bot/src/app/commands/random/invalidate.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { SlashCommandProps, CommandData, invalidate } from 'commandkit';
22

3-
export const data: CommandData = {
3+
export const command: CommandData = {
44
name: 'invalidate-random',
55
description: 'This is a random command invalidation.',
66
};
77

8-
export async function run({ interaction }: SlashCommandProps) {
8+
export async function chatInput({ interaction }: SlashCommandProps) {
99
await interaction.deferReply();
1010

1111
await invalidate('random');

apps/test-bot/src/app/commands/random/revalidate.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { SlashCommandProps, CommandData, revalidate } from 'commandkit';
22

3-
export const data: CommandData = {
3+
export const command: CommandData = {
44
name: 'revalidate-random',
55
description: 'This is a random command invalidation.',
66
};
77

8-
export async function run({ interaction }: SlashCommandProps) {
8+
export async function chatInput({ interaction }: SlashCommandProps) {
99
await interaction.deferReply();
1010

1111
const revalidated = await revalidate<number>('random');

packages/commandkit/package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"scripts": {
1515
"lint": "tsc --noEmit",
16-
"dev": "tsup --watch",
16+
"dev": "pnpm run --filter=test-bot dev",
1717
"build": "tsup",
1818
"publish-package": "npm publish",
1919
"test": "vitest"
@@ -34,14 +34,15 @@
3434
"@babel/parser": "^7.26.5",
3535
"@babel/traverse": "^7.26.5",
3636
"@babel/types": "^7.26.5",
37+
"chokidar": "^4.0.3",
3738
"commander": "^12.1.0",
3839
"dotenv": "^16.4.7",
3940
"ms": "^2.1.3",
4041
"ora": "^8.0.1",
4142
"picocolors": "^1.1.1",
4243
"rfdc": "^1.3.1",
4344
"rimraf": "^5.0.5",
44-
"tsup": "^8.3.5",
45+
"tsup": "^8.4.0",
4546
"use-macro": "^1.1.0"
4647
},
4748
"devDependencies": {

packages/commandkit/src/CommandKit.ts

+8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ import { LocalizationStrategy } from './app/i18n/LocalizationStrategy';
1414
import { CommandsRouter, EventsRouter } from './app/router';
1515
import { AppEventsHandler } from './app/handlers/AppEventsHandler';
1616
import { CommandKitPluginRuntime } from './plugins/runtime/CommandKitPluginRuntime';
17+
import { loadConfigFile } from './config/loader';
18+
import { COMMANDKIT_IS_DEV } from './utils/constants';
19+
import { registerDevHooks } from './utils/dev-hooks';
1720

1821
export interface CommandKitConfiguration {
1922
defaultLocale: Locale;
@@ -95,6 +98,7 @@ export class CommandKit extends EventEmitter {
9598
* @param token The application token to connect to the discord gateway. If not provided, it will use the `TOKEN` or `DISCORD_TOKEN` environment variable. If set to `false`, it will not login.
9699
*/
97100
async start(token?: string | false) {
101+
await loadConfigFile();
98102
if (this.#started) return;
99103

100104
await this.#init();
@@ -109,6 +113,10 @@ export class CommandKit extends EventEmitter {
109113

110114
this.#started = true;
111115

116+
if (COMMANDKIT_IS_DEV) {
117+
registerDevHooks(this);
118+
}
119+
112120
await this.commandHandler.registrar.register();
113121
}
114122

packages/commandkit/src/app/handlers/AppEventsHandler.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { CommandKit } from '../../CommandKit';
22
import { Logger } from '../../logger/Logger';
3+
import { toFileURL } from '../../utils/resolve-file-url';
34
import { ParsedEvent } from '../router';
45

56
export type EventListener = (...args: any[]) => any;
@@ -30,7 +31,7 @@ export class AppEventsHandler {
3031
const listeners: EventListener[] = [];
3132

3233
for (const listener of event.listeners) {
33-
const handler = await import(`file://${listener}?t=${Date.now()}`);
34+
const handler = await import(toFileURL(listener, true));
3435

3536
if (!handler.default || typeof handler.default !== 'function') {
3637
Logger.error(
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { spawn } from 'node:child_process';
2+
import { DevEnv, devEnvFileArgs, prodEnvFileArgs } from './env';
3+
import { join } from 'node:path';
4+
import { existsSync } from 'node:fs';
5+
import { panic } from './common';
6+
7+
export function createAppProcess(
8+
fileName: string,
9+
cwd: string,
10+
isDev: boolean,
11+
) {
12+
const envFiles = isDev ? devEnvFileArgs(cwd) : prodEnvFileArgs(cwd);
13+
14+
if (!existsSync(join(cwd, fileName))) {
15+
panic(`Could not locate the entrypoint file: ${fileName}`);
16+
}
17+
18+
const ps = spawn(
19+
'node',
20+
[
21+
...envFiles,
22+
`--title="CommandKit ${isDev ? 'Development' : 'Production'}"`,
23+
'--enable-source-maps',
24+
fileName,
25+
],
26+
{
27+
cwd: cwd,
28+
windowsHide: true,
29+
env: DevEnv(),
30+
stdio: 'pipe',
31+
},
32+
);
33+
34+
ps.stdout?.pipe(process.stdout);
35+
ps.stderr?.pipe(process.stderr);
36+
37+
return ps;
38+
}

0 commit comments

Comments
 (0)