feat: add generic to readFile.ts buildData, adding different event loading strategies

This commit is contained in:
Jacob Nguyen
2022-06-14 09:39:29 -05:00
parent 0fc0782e55
commit cccfecc325
3 changed files with 8 additions and 5 deletions

View File

@@ -28,7 +28,7 @@ import { errTap } from './observableHandling';
export const onReady = (wrapper: Wrapper) => {
const { client, commands } = wrapper;
const ready$ = fromEvent(client, 'ready').pipe(take(1), skip(1));
const processCommandFiles$ = Files.buildData(commands).pipe(
const processCommandFiles$ = Files.buildData<Module>(commands).pipe(
errTap(reason => {
wrapper.sernEmitter?.emit('module.register', {
type: 'failure',

View File

@@ -16,7 +16,10 @@ interface Wrapper {
readonly sernEmitter?: SernEmitter;
readonly defaultPrefix?: string;
readonly commands: string;
readonly events?: (DiscordEvent | EventEmitterRegister | SernEvent)[];
readonly events?:
| (DiscordEvent | EventEmitterRegister | SernEvent)[]
| string
| (() => (DiscordEvent | EventEmitterRegister | SernEvent)[]);
}
export default Wrapper;

View File

@@ -48,10 +48,10 @@ export const fmtFileName = (n: string) => n.substring(0, n.length - 3);
* @param commandDir
*/
export function buildData(commandDir: string): Observable<
export function buildData<T>(commandDir: string): Observable<
Result<
{
mod: Module;
mod: T;
absPath: string;
},
SernError.UndefinedModule
@@ -60,7 +60,7 @@ export function buildData(commandDir: string): Observable<
return from(
getCommands(commandDir).map(absPath => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const mod = <Module | undefined>require(absPath).default;
const mod = <T | undefined>require(absPath).default;
if (mod !== undefined) {
return Ok({ mod, absPath });
} else return Err(SernError.UndefinedModule as const);