Skip to content

Commit 836af8f

Browse files
committed
refactor: reorganize imports + handlers
1 parent 8009a58 commit 836af8f

File tree

10 files changed

+47
-26
lines changed

10 files changed

+47
-26
lines changed

packages/commandkit/bin/build.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// @ts-check
22

3+
import { appendFile } from 'node:fs/promises';
4+
import { join } from 'node:path';
35
import { build } from 'tsup';
46
import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs';
57
import ora from 'ora';
6-
import { appendFile } from 'node:fs/promises';
7-
import { join } from 'node:path';
88

99
export async function bootstrapProductionBuild(config) {
1010
const {

packages/commandkit/bin/common.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// @ts-check
22

3+
import { rimrafSync } from 'rimraf';
34
import { join } from 'node:path';
45
import fs from 'node:fs';
5-
import { rimrafSync } from 'rimraf';
66

77
const resetColor = '\x1b[0m';
88

packages/commandkit/bin/development.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// @ts-check
22
import { config as dotenv } from 'dotenv';
3-
import { build } from 'tsup';
4-
import child_process from 'node:child_process';
5-
import ora from 'ora';
63
import { join } from 'node:path';
4+
import { build } from 'tsup';
75
import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs';
86
import { parseEnv } from './parse-env.mjs';
7+
import child_process from 'node:child_process';
8+
import ora from 'ora';
99

1010
const RESTARTING_MSG_PATTERN = /^Restarting '|".+'|"\n?$/;
1111
const FAILED_RUNNING_PATTERN = /^Failed running '.+'|"\n?$/;

packages/commandkit/bin/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import { Command } from 'commander';
66
import { bootstrapDevelopmentServer } from './development.mjs';
7-
import { bootstrapProductionBuild } from './build.mjs';
87
import { bootstrapProductionServer } from './production.mjs';
8+
import { bootstrapProductionBuild } from './build.mjs';
99

1010
const program = new Command('commandkit');
1111

packages/commandkit/bin/production.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// @ts-check
22
import { config as dotenv } from 'dotenv';
3-
import child_process from 'node:child_process';
3+
import { existsSync } from 'node:fs';
44
import { join } from 'node:path';
55
import { Colors, findCommandKitConfig, panic, write } from './common.mjs';
6-
import { existsSync } from 'node:fs';
76
import { parseEnv } from './parse-env.mjs';
7+
import child_process from 'node:child_process';
88

99
export async function bootstrapProductionServer(config) {
1010
const {

packages/commandkit/src/handlers/command-handler/CommandHandler.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import type { CommandFileObject, ReloadOptions } from '../../typings';
33

44
import { toFileURL } from '../../utils/resolve-file-url';
55
import { getFilePaths } from '../../utils/get-paths';
6+
import { clone } from '../../utils/clone';
67

78
import loadCommandsWithRest from './functions/loadCommandsWithRest';
89
import registerCommands from './functions/registerCommands';
9-
import builtInValidations from './validations';
10+
import builtInValidationsFunctions from './validations';
1011
import colors from '../../utils/colors';
11-
import { clone } from '../../utils/clone';
1212

1313
/**
1414
* A handler for client application commands.
@@ -27,7 +27,7 @@ export class CommandHandler {
2727
async init() {
2828
await this.#buildCommands();
2929

30-
this.#buildValidations();
30+
this.#buildBuiltInValidations();
3131

3232
const devOnlyCommands = this.#data.commands.filter((cmd) => cmd.options?.devOnly);
3333

@@ -150,9 +150,9 @@ export class CommandHandler {
150150
}
151151
}
152152

153-
#buildValidations() {
154-
for (const validationFunction of builtInValidations) {
155-
this.#data.builtInValidations.push(validationFunction);
153+
#buildBuiltInValidations() {
154+
for (const builtInValidationFunction of builtInValidationsFunctions) {
155+
this.#data.builtInValidations.push(builtInValidationFunction);
156156
}
157157
}
158158

@@ -175,7 +175,7 @@ export class CommandHandler {
175175

176176
const { data, options, run, autocompleteRun, ...rest } = targetCommand;
177177

178-
// skip if autocomplete handler is not defined
178+
// Skip if autocomplete handler is not defined
179179
if (isAutocomplete && !autocompleteRun) return;
180180

181181
const commandObj = {
@@ -239,9 +239,15 @@ export class CommandHandler {
239239
}
240240

241241
async reloadCommands(type?: ReloadOptions) {
242+
if (!this.#data.commandsPath) {
243+
throw new Error(
244+
'Cannot reload commands as "commandsPath" was not provided when instantiating CommandKit.',
245+
);
246+
}
247+
242248
this.#data.commands = [];
243249

244-
// Rebuild commands tree
250+
// Re-build commands tree
245251
await this.#buildCommands();
246252

247253
if (this.#data.bulkRegister) {
@@ -262,6 +268,4 @@ export class CommandHandler {
262268
});
263269
}
264270
}
265-
266-
async useUpdatedValidations() {}
267271
}

packages/commandkit/src/handlers/command-handler/validations/devOnly.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { BuiltInValidationParams } from '../typings';
22

33
export default function ({ interaction, targetCommand, handlerData }: BuiltInValidationParams) {
44
if (interaction.isAutocomplete()) return;
5+
56
if (targetCommand.options?.devOnly) {
67
if (interaction.inGuild() && !handlerData.devGuildIds.includes(interaction.guildId)) {
78
interaction.reply({

packages/commandkit/src/handlers/event-handler/EventHandler.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type { EventHandlerOptions, EventHandlerData } from './typings';
22
import type { CommandHandler } from '../command-handler/CommandHandler';
33
import { getFilePaths, getFolderPaths } from '../../utils/get-paths';
44
import { toFileURL } from '../../utils/resolve-file-url';
5-
import colors from '../../utils/colors';
65
import { clone } from '../../utils/clone';
6+
import colors from '../../utils/colors';
77

88
/**
99
* A handler for client events.
@@ -97,6 +97,12 @@ export class EventHandler {
9797
}
9898

9999
async reloadEvents(commandHandler?: CommandHandler) {
100+
if (!this.#data.eventsPath) {
101+
throw new Error(
102+
'Cannot reload events as "eventsPath" was not provided when instantiating CommandKit.',
103+
);
104+
}
105+
100106
this.#data.events = [];
101107

102108
await this.#buildEvents();

packages/commandkit/src/handlers/validation-handler/ValidationHandler.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ValidationHandlerData, ValidationHandlerOptions } from './typings';
22
import { toFileURL } from '../../utils/resolve-file-url';
33
import { getFilePaths } from '../../utils/get-paths';
4-
import colors from '../../utils/colors';
54
import { clone } from '../../utils/clone';
5+
import colors from '../../utils/colors';
66

77
/**
88
* A handler for command validations.
@@ -18,7 +18,7 @@ export class ValidationHandler {
1818
}
1919

2020
async init() {
21-
await this.#buildValidations();
21+
this.#data.validations = await this.#buildValidations();
2222
}
2323

2424
async #buildValidations() {
@@ -27,6 +27,8 @@ export class ValidationHandler {
2727
const validationPaths = await getFilePaths(this.#data.validationsPath, true);
2828
const validationFilePaths = validationPaths.filter((path) => allowedExtensions.test(path));
2929

30+
const validationFunctions: Function[] = [];
31+
3032
for (const validationFilePath of validationFilePaths) {
3133
const modulePath = toFileURL(validationFilePath);
3234

@@ -54,17 +56,25 @@ export class ValidationHandler {
5456
continue;
5557
}
5658

57-
this.#data.validations.push(validationFunction);
59+
validationFunctions.push(validationFunction);
5860
}
61+
62+
return validationFunctions;
5963
}
6064

6165
get validations() {
6266
return this.#data.validations;
6367
}
6468

6569
async reloadValidations() {
66-
this.#data.validations = [];
70+
if (!this.#data.validationsPath) {
71+
throw new Error(
72+
'Cannot reload validations as "validationsPath" was not provided when instantiating CommandKit.',
73+
);
74+
}
75+
76+
const newValidations = await this.#buildValidations();
6777

68-
await this.#buildValidations();
78+
this.#data.validations = newValidations;
6979
}
7080
}

packages/commandkit/tests/src/commands/misc/ping.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function run({ interaction, client }: SlashCommandProps) {
4646
const row = new ActionRowBuilder<ButtonKit>().addComponents(button);
4747

4848
const message = await interaction.reply({
49-
content: 'Click the button',
49+
content: 'Click one of the buttons',
5050
components: [row],
5151
fetchReply: true,
5252
});

0 commit comments

Comments
 (0)