From 63e0daac162a648168de932da512b18d43aedcb8 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Mon, 22 May 2023 22:50:05 -0500 Subject: [PATCH] fix: typings for options- have access to all properties now --- src/core/index.ts | 1 - src/core/module-loading.ts | 31 ++++++++++++++++++++++--------- src/core/types/modules.ts | 29 ++++++++++++----------------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/core/index.ts b/src/core/index.ts index 9ac6593..22ae977 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -22,7 +22,6 @@ export type { ExternalEventCommand, CommandModuleDefs, EventModuleDefs, - BaseOptions, SernAutocompleteData, SernOptionsData, } from './types/modules'; diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index b76b839..895335c 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -54,26 +54,39 @@ export function filename(path: string) { return fmtFileName(basename(path)); } +function createSkipCondition(base: string) { + const validExtensions = ['.js', '.cjs', '.mts', '.mjs', 'cts']; + return ( type: 'file' | 'directory') => { + if(type === 'file') { + return fmtFileName(base)[0] === '!' + || !validExtensions.includes(extname(base)); + } + return base[0] === '!'; + } +} +async function deriveFileInfo(dir: string, file: string) { + const fullPath = join(dir, file); + return { + fullPath, + fileStats: await stat(fullPath), + base: basename(file) + } +} async function* readPaths(dir: string, shouldDebug: boolean): AsyncGenerator { try { const files = await readdir(dir); for (const file of files) { - const fullPath = join(dir, file); - const fileStats = await stat(fullPath); - const base = basename(file); + const { fullPath, fileStats, base } = await deriveFileInfo(dir, file); + const isSkippable = createSkipCondition(base); if (fileStats.isDirectory()) { //Todo: refactor so that i dont repeat myself for files (line 71) - if (base.endsWith('-ignore!')) { + if (isSkippable('directory')) { if (shouldDebug) console.info(`ignored directory: ${fullPath}`); } else { yield* readPaths(fullPath, shouldDebug); } } else { - const isSkippable = - fmtFileName(base).endsWith('-ignore!') || - !['.js', '.cjs', '.mts', '.mjs'].includes(extname(base)); - - if (isSkippable) { + if (isSkippable('file')) { if (shouldDebug) console.info(`ignored: ${fullPath}`); } else { /// #if MODE === 'esm' diff --git a/src/core/types/modules.ts b/src/core/types/modules.ts index efaf7b0..a96a82b 100644 --- a/src/core/types/modules.ts +++ b/src/core/types/modules.ts @@ -1,4 +1,8 @@ import type { + APIApplicationCommandBasicOption, + APIApplicationCommandOptionBase, + APIApplicationCommandOptionChoice, + APIApplicationCommandRoleOption, ApplicationCommandAttachmentOption, ApplicationCommandChannelOptionData, ApplicationCommandChoicesData, @@ -9,6 +13,7 @@ import type { ApplicationCommandSubCommandData, ApplicationCommandSubGroupData, BaseApplicationCommandOptionsData, + CommandOptionNonChoiceResolvableType, } from 'discord.js'; import { AutocompleteInteraction, @@ -202,32 +207,22 @@ export type InputCommand = { [T in CommandType]: CommandModuleNoPlugins[T] & { plugins?: AnyCommandPlugin[] }; }[CommandType]; + /** * Type that replaces autocomplete with {@link SernAutocompleteData} */ -export type BaseOptions = - | ApplicationCommandChoicesData - | ApplicationCommandNonOptionsData - | ApplicationCommandChannelOptionData - | ApplicationCommandNumericOptionData - | ApplicationCommandAttachmentOption +export type SernOptionsData = + | SernSubCommandData + | SernSubCommandGroupData + | APIApplicationCommandBasicOption | SernAutocompleteData; -export interface SernSubCommandData extends BaseApplicationCommandOptionsData { +export interface SernSubCommandData extends APIApplicationCommandOptionBase { type: ApplicationCommandOptionType.Subcommand; - required?: never; - options?: BaseOptions[]; + options?: SernOptionsData[]; } export interface SernSubCommandGroupData extends BaseApplicationCommandOptionsData { type: ApplicationCommandOptionType.SubcommandGroup; - required?: never; options?: SernSubCommandData[]; } - -export type SernOptionsData = - U extends ApplicationCommandSubCommandData - ? SernSubCommandData - : U extends ApplicationCommandSubGroupData - ? SernSubCommandGroupData - : BaseOptions;