feat: add getPublishableCommands to ModuleManager

This commit is contained in:
Jacob Nguyen
2023-05-06 01:49:42 -05:00
parent 1e1398fade
commit 098a440669
6 changed files with 25 additions and 19 deletions

View File

@@ -1,3 +1,5 @@
import { CommandModule } from '../../types/module';
import { importModule } from '../module-loading';
import type { ModuleStore } from '../structures';
/**
* @since 2.0.0
@@ -5,6 +7,7 @@ import type { ModuleStore } from '../structures';
export interface ModuleManager {
get(id: string): string | undefined;
set(id: string, path: string): void;
getPublishableCommands() : Promise<CommandModule[]>
}
/**
* @since 2.0.0
@@ -12,10 +15,19 @@ export interface ModuleManager {
export class DefaultModuleManager implements ModuleManager {
constructor(private moduleStore: ModuleStore) {}
get(id: string) {
return this.moduleStore.Commands.get(id);
return this.moduleStore.get(id);
}
set(id: string, path: string): void {
this.moduleStore.Commands.set(id, path)
this.moduleStore.set(id, path)
}
//not tested
getPublishableCommands(): Promise<CommandModule[]> {
const entries = this.moduleStore.entries();
const publishable = 0b000000110;
return Promise.all(
Array.from(entries)
.filter(([id,]) => (Number.parseInt(id.at(-1)!) & publishable) !== 0)
.map(([, path]) => importModule<CommandModule>(path)))
}
}

View File

@@ -10,23 +10,21 @@ import { basename, join, resolve } from 'path';
export type ModuleResult<T> = Promise<Result<Processed<T>, SernError>>
export type Loader<T> = (absPath: string) => ModuleResult<T>
export async function importModule<T>(absPath: string) {
/// #if MODE === 'esm'
return (await import(absPath)).default as T
/// #elif MODE === 'cjs'
return require(absPath).default as T; // eslint-disable-line
/// #endif
}
export async function defaultModuleLoader<T extends Module>(
absPath: string,
): ModuleResult<T> {
// prettier-ignore
let module: T | undefined
/// #if MODE === 'esm'
= (await import(absPath)).default
/// #elif MODE === 'cjs'
= require(absPath).default; // eslint-disable-line
/// #endif
const module = await importModule<T>(absPath);
if (module === undefined) {
return Err(SernError.UndefinedModule);
return Err(SernError.UndefinedModule);
}
try {
module = new (module as unknown as new () => T)();
} catch {}
checkIsProcessed(module)
return Ok(module);
}

View File

@@ -4,11 +4,9 @@
* and independent of each other
*/
import { concatMap, defaultIfEmpty, EMPTY, every, fromEvent, map, Observable, of, OperatorFunction, pipe, share, switchMap } from 'rxjs';
import type { AnyModule } from '../types/module';
import type { PluginResult, VoidResult } from '../types/plugin';
import { Result } from 'ts-results-es';
import { Awaitable } from '../types/handler';
import { ImportPayload, Processed } from '../types/core';
import { EventEmitter } from 'node:events';
/**
* if {src} is true, mapTo V, else ignore

View File

@@ -10,7 +10,6 @@ type AnyCommandInteraction = ChatInputCommandInteraction | MessageContextMenuCom
export function isMessageComponent(i: InteractionTypable): i is AnyMessageComponentInteraction {
return i.type === InteractionType.MessageComponent;
}
export function isCommand(i: InteractionTypable): i is AnyCommandInteraction {
return i.type === InteractionType.ApplicationCommand;
}

View File

@@ -4,8 +4,8 @@
* Storing all command modules
* This dependency is usually injected into ModuleManager
*/
export class ModuleStore {
readonly Commands = new Map<string, string>();
export class ModuleStore extends Map<string, string> {
}

View File

@@ -13,7 +13,6 @@ import { getCommands } from '../core/module-loading';
* @example
* ```ts title="src/index.ts"
* Sern.init({
* platform: djs('!'),
* commands: 'dist/commands',
* events: 'dist/events',
* containerConfig : {