diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index 3040d67..978aa25 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -1,10 +1,7 @@ -import { type Observable, from, mergeMap, ObservableInput } from 'rxjs'; -import { readdir, stat } from 'fs/promises'; import path from 'node:path'; import assert from 'assert'; import { createRequire } from 'node:module'; import type { ImportPayload, Wrapper } from '../types/core'; -import type { Module } from '../types/core-modules'; import { existsSync } from 'fs'; import type { Logging } from './interfaces'; @@ -52,60 +49,11 @@ export async function importModule(absPath: string) { return { module: commandModule } as T; } -export async function defaultModuleLoader(absPath: string): ModuleResult { - let { module } = await importModule<{ module: T }>(absPath); - assert(module, `Found an undefined module: ${absPath}`); - return { module, absPath }; -} export const fmtFileName = (fileName: string) => path.parse(fileName).name; -/** - * a directory string is converted into a stream of modules. - * starts the stream of modules that sern needs to process on init - * @returns {Observable<{ mod: Module; absPath: string; }[]>} data from command files - * @param commandDir - */ -export function buildModuleStream( - input: ObservableInput, -): Observable> { - return from(input).pipe(mergeMap(defaultModuleLoader)); -} - -export const getFullPathTree = (dir: string) => readPaths(path.resolve(dir)); - export const filename = (p: string) => fmtFileName(path.basename(p)); -const validExtensions = ['.js', '.ts', '']; -const isSkippable = (filename: string) => { - //empty string is for non extension files (directories) - return filename[0] === '!' || !validExtensions.includes(path.extname(filename)); -}; - -async function deriveFileInfo(dir: string, file: string) { - const fullPath = path.join(dir, file); - return { fullPath, - fileStats: await stat(fullPath), - base: path.basename(file) }; -} - -async function* readPaths(dir: string): AsyncGenerator { - const files = await readdir(dir); - for (const file of files) { - const { fullPath, fileStats, base } = await deriveFileInfo(dir, file); - if (fileStats.isDirectory()) { - //Todo: refactor so that i dont repeat myself for files (line 71) - if (!isSkippable(base)) { - yield* readPaths(fullPath); - } - } else { - if (!isSkippable(base)) { - yield 'file:///' + fullPath; - } - } - } -} - const requir = createRequire(import.meta.url); export function loadConfig(wrapper: Wrapper | 'file', log: Logging | undefined): Wrapper { diff --git a/src/handlers/_internal.ts b/src/handlers/_internal.ts deleted file mode 100644 index 4813218..0000000 --- a/src/handlers/_internal.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './dispatchers'; -export * from './event-utils'; diff --git a/src/handlers/event-utils.ts b/src/handlers/event-utils.ts index f66f4e9..6902021 100644 --- a/src/handlers/event-utils.ts +++ b/src/handlers/event-utils.ts @@ -99,15 +99,7 @@ export function createMessageHandler( return Ok({ args: contextArgs(event, rest), module: fullPath as Processed }) }); } -/** - * This function assigns remaining, incomplete data to each imported module. - */ -export function buildModules( - input: ObservableInput, -) { - return Files.buildModuleStream>(input); -} interface ExecutePayload { diff --git a/src/handlers/interaction-event.ts b/src/handlers/interaction-event.ts index e9ec3fe..62a125a 100644 --- a/src/handlers/interaction-event.ts +++ b/src/handlers/interaction-event.ts @@ -11,7 +11,7 @@ import { filterTap, resultPayload, } from '../core/_internal'; -import { createInteractionHandler, executeModule, makeModuleExecutor } from './_internal'; +import { createInteractionHandler, executeModule, makeModuleExecutor } from './event-utils'; import type { DependencyList } from '../types/ioc'; export function interactionHandler([emitter, err, log, modules, client]: DependencyList) { diff --git a/src/handlers/message-event.ts b/src/handlers/message-event.ts index a1c1a69..cc6fda1 100644 --- a/src/handlers/message-event.ts +++ b/src/handlers/message-event.ts @@ -2,7 +2,7 @@ import { mergeMap, EMPTY, concatMap } from 'rxjs'; import type { Message } from 'discord.js'; import { PayloadType } from '../core'; import { sharedEventStream, SernError, filterTap, resultPayload } from '../core/_internal'; -import { createMessageHandler, executeModule, makeModuleExecutor } from './_internal'; +import { createMessageHandler, executeModule, makeModuleExecutor } from './event-utils'; import type { DependencyList } from '../types/ioc'; /** diff --git a/src/handlers/ready-event.ts b/src/handlers/ready-event.ts index 8b1a671..6274638 100644 --- a/src/handlers/ready-event.ts +++ b/src/handlers/ready-event.ts @@ -1,6 +1,6 @@ import { ObservableInput, concat, first, fromEvent, ignoreElements, pipe, tap } from 'rxjs'; import { _Module } from '../core/_internal'; -import { Logging, } from '../core/interfaces'; +import { Logging } from '../core/interfaces'; import type { DependencyList } from '../types/ioc'; const once = (log: Logging | undefined) => pipe( diff --git a/src/handlers/user-defined-events.ts b/src/handlers/user-defined-events.ts index 66e7dfb..6c794f6 100644 --- a/src/handlers/user-defined-events.ts +++ b/src/handlers/user-defined-events.ts @@ -1,7 +1,8 @@ import { ObservableInput, map, mergeAll } from 'rxjs'; import { EventType } from '../core/structures'; import { SernError } from '../core/_internal'; -import { buildModules, callInitPlugins, handleCrash, eventDispatcher } from './_internal'; +import { callInitPlugins, handleCrash } from './event-utils'; +import { eventDispatcher } from './dispatchers' import { Service } from '../core/ioc'; import type { DependencyList } from '../types/ioc'; import type { EventModule, Processed } from '../types/core-modules'; @@ -23,14 +24,14 @@ export function eventsHandler( throw Error(SernError.InvalidModuleType + ' while creating event handler'); } }; - buildModules(allPaths) - .pipe( - callInitPlugins(emitter), - map(intoDispatcher), - /** - * Where all events are turned on - */ - mergeAll(), - handleCrash(err, emitter, log)) - .subscribe(); +// buildModules(allPaths) +// .pipe( +// callInitPlugins(emitter), +// map(intoDispatcher), +// /** +// * Where all events are turned on +// */ +// mergeAll(), +// handleCrash(err, emitter, log)) +// .subscribe(); } diff --git a/src/sern.ts b/src/sern.ts index 769c8d2..b64d79a 100644 --- a/src/sern.ts +++ b/src/sern.ts @@ -1,4 +1,4 @@ -import { handleCrash } from './handlers/_internal'; +import { handleCrash } from './handlers/event-utils'; import callsites from 'callsites'; import { Files } from './core/_internal'; import { merge } from 'rxjs';