Skip to content

Commit

Permalink
refactor: reorganize imports + handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
notunderctrl committed Dec 7, 2023
1 parent 8009a58 commit 836af8f
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/commandkit/bin/build.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-check

import { appendFile } from 'node:fs/promises';
import { join } from 'node:path';
import { build } from 'tsup';
import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs';
import ora from 'ora';
import { appendFile } from 'node:fs/promises';
import { join } from 'node:path';

export async function bootstrapProductionBuild(config) {
const {
Expand Down
2 changes: 1 addition & 1 deletion packages/commandkit/bin/common.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @ts-check

import { rimrafSync } from 'rimraf';
import { join } from 'node:path';
import fs from 'node:fs';
import { rimrafSync } from 'rimraf';

const resetColor = '\x1b[0m';

Expand Down
6 changes: 3 additions & 3 deletions packages/commandkit/bin/development.mjs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// @ts-check
import { config as dotenv } from 'dotenv';
import { build } from 'tsup';
import child_process from 'node:child_process';
import ora from 'ora';
import { join } from 'node:path';
import { build } from 'tsup';
import { Colors, erase, findCommandKitConfig, panic, write } from './common.mjs';
import { parseEnv } from './parse-env.mjs';
import child_process from 'node:child_process';
import ora from 'ora';

const RESTARTING_MSG_PATTERN = /^Restarting '|".+'|"\n?$/;
const FAILED_RUNNING_PATTERN = /^Failed running '.+'|"\n?$/;
Expand Down
2 changes: 1 addition & 1 deletion packages/commandkit/bin/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

import { Command } from 'commander';
import { bootstrapDevelopmentServer } from './development.mjs';
import { bootstrapProductionBuild } from './build.mjs';
import { bootstrapProductionServer } from './production.mjs';
import { bootstrapProductionBuild } from './build.mjs';

const program = new Command('commandkit');

Expand Down
4 changes: 2 additions & 2 deletions packages/commandkit/bin/production.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// @ts-check
import { config as dotenv } from 'dotenv';
import child_process from 'node:child_process';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { Colors, findCommandKitConfig, panic, write } from './common.mjs';
import { existsSync } from 'node:fs';
import { parseEnv } from './parse-env.mjs';
import child_process from 'node:child_process';

export async function bootstrapProductionServer(config) {
const {
Expand Down
24 changes: 14 additions & 10 deletions packages/commandkit/src/handlers/command-handler/CommandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import type { CommandFileObject, ReloadOptions } from '../../typings';

import { toFileURL } from '../../utils/resolve-file-url';
import { getFilePaths } from '../../utils/get-paths';
import { clone } from '../../utils/clone';

import loadCommandsWithRest from './functions/loadCommandsWithRest';
import registerCommands from './functions/registerCommands';
import builtInValidations from './validations';
import builtInValidationsFunctions from './validations';
import colors from '../../utils/colors';
import { clone } from '../../utils/clone';

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

this.#buildValidations();
this.#buildBuiltInValidations();

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

Expand Down Expand Up @@ -150,9 +150,9 @@ export class CommandHandler {
}
}

#buildValidations() {
for (const validationFunction of builtInValidations) {
this.#data.builtInValidations.push(validationFunction);
#buildBuiltInValidations() {
for (const builtInValidationFunction of builtInValidationsFunctions) {
this.#data.builtInValidations.push(builtInValidationFunction);
}
}

Expand All @@ -175,7 +175,7 @@ export class CommandHandler {

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

// skip if autocomplete handler is not defined
// Skip if autocomplete handler is not defined
if (isAutocomplete && !autocompleteRun) return;

const commandObj = {
Expand Down Expand Up @@ -239,9 +239,15 @@ export class CommandHandler {
}

async reloadCommands(type?: ReloadOptions) {
if (!this.#data.commandsPath) {
throw new Error(
'Cannot reload commands as "commandsPath" was not provided when instantiating CommandKit.',
);
}

this.#data.commands = [];

// Rebuild commands tree
// Re-build commands tree
await this.#buildCommands();

if (this.#data.bulkRegister) {
Expand All @@ -262,6 +268,4 @@ export class CommandHandler {
});
}
}

async useUpdatedValidations() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { BuiltInValidationParams } from '../typings';

export default function ({ interaction, targetCommand, handlerData }: BuiltInValidationParams) {
if (interaction.isAutocomplete()) return;

if (targetCommand.options?.devOnly) {
if (interaction.inGuild() && !handlerData.devGuildIds.includes(interaction.guildId)) {
interaction.reply({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type { EventHandlerOptions, EventHandlerData } from './typings';
import type { CommandHandler } from '../command-handler/CommandHandler';
import { getFilePaths, getFolderPaths } from '../../utils/get-paths';
import { toFileURL } from '../../utils/resolve-file-url';
import colors from '../../utils/colors';
import { clone } from '../../utils/clone';
import colors from '../../utils/colors';

/**
* A handler for client events.
Expand Down Expand Up @@ -97,6 +97,12 @@ export class EventHandler {
}

async reloadEvents(commandHandler?: CommandHandler) {
if (!this.#data.eventsPath) {
throw new Error(
'Cannot reload events as "eventsPath" was not provided when instantiating CommandKit.',
);
}

this.#data.events = [];

await this.#buildEvents();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { ValidationHandlerData, ValidationHandlerOptions } from './typings';
import { toFileURL } from '../../utils/resolve-file-url';
import { getFilePaths } from '../../utils/get-paths';
import colors from '../../utils/colors';
import { clone } from '../../utils/clone';
import colors from '../../utils/colors';

/**
* A handler for command validations.
Expand All @@ -18,7 +18,7 @@ export class ValidationHandler {
}

async init() {
await this.#buildValidations();
this.#data.validations = await this.#buildValidations();
}

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

const validationFunctions: Function[] = [];

for (const validationFilePath of validationFilePaths) {
const modulePath = toFileURL(validationFilePath);

Expand Down Expand Up @@ -54,17 +56,25 @@ export class ValidationHandler {
continue;
}

this.#data.validations.push(validationFunction);
validationFunctions.push(validationFunction);
}

return validationFunctions;
}

get validations() {
return this.#data.validations;
}

async reloadValidations() {
this.#data.validations = [];
if (!this.#data.validationsPath) {
throw new Error(
'Cannot reload validations as "validationsPath" was not provided when instantiating CommandKit.',
);
}

const newValidations = await this.#buildValidations();

await this.#buildValidations();
this.#data.validations = newValidations;
}
}
2 changes: 1 addition & 1 deletion packages/commandkit/tests/src/commands/misc/ping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function run({ interaction, client }: SlashCommandProps) {
const row = new ActionRowBuilder<ButtonKit>().addComponents(button);

const message = await interaction.reply({
content: 'Click the button',
content: 'Click one of the buttons',
components: [row],
fetchReply: true,
});
Expand Down

0 comments on commit 836af8f

Please sign in to comment.