fix: typings for options- have access to all properties now

This commit is contained in:
Jacob Nguyen
2023-05-22 22:50:05 -05:00
parent 49fb1453a5
commit 63e0daac16
3 changed files with 34 additions and 27 deletions

View File

@@ -22,7 +22,6 @@ export type {
ExternalEventCommand,
CommandModuleDefs,
EventModuleDefs,
BaseOptions,
SernAutocompleteData,
SernOptionsData,
} from './types/modules';

View File

@@ -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<string> {
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'

View File

@@ -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<ApplicationCommandOptionType.Subcommand> {
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 ApplicationCommandOptionData = ApplicationCommandOptionData> =
U extends ApplicationCommandSubCommandData
? SernSubCommandData
: U extends ApplicationCommandSubGroupData
? SernSubCommandGroupData
: BaseOptions;