mirror of
https://github.com/sern-handler/handler
synced 2026-06-20 23:02:15 +00:00
TEAR IT UP
This commit is contained in:
@@ -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<T>(absPath: string) {
|
||||
return { module: commandModule } as T;
|
||||
}
|
||||
|
||||
export async function defaultModuleLoader<T extends Module>(absPath: string): ModuleResult<T> {
|
||||
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<T extends Module>(
|
||||
input: ObservableInput<string>,
|
||||
): Observable<ImportPayload<T>> {
|
||||
return from(input).pipe(mergeMap(defaultModuleLoader<T>));
|
||||
}
|
||||
|
||||
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<string> {
|
||||
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 {
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
export * from './dispatchers';
|
||||
export * from './event-utils';
|
||||
@@ -99,15 +99,7 @@ export function createMessageHandler(
|
||||
return Ok({ args: contextArgs(event, rest), module: fullPath as Processed<CommandModule> })
|
||||
});
|
||||
}
|
||||
/**
|
||||
* This function assigns remaining, incomplete data to each imported module.
|
||||
*/
|
||||
|
||||
export function buildModules<T extends AnyModule>(
|
||||
input: ObservableInput<string>,
|
||||
) {
|
||||
return Files.buildModuleStream<Processed<T>>(input);
|
||||
}
|
||||
|
||||
|
||||
interface ExecutePayload {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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';
|
||||
|
||||
/**
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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<EventModule>(allPaths)
|
||||
.pipe(
|
||||
callInitPlugins(emitter),
|
||||
map(intoDispatcher),
|
||||
/**
|
||||
* Where all events are turned on
|
||||
*/
|
||||
mergeAll(),
|
||||
handleCrash(err, emitter, log))
|
||||
.subscribe();
|
||||
// buildModules<EventModule>(allPaths)
|
||||
// .pipe(
|
||||
// callInitPlugins(emitter),
|
||||
// map(intoDispatcher),
|
||||
// /**
|
||||
// * Where all events are turned on
|
||||
// */
|
||||
// mergeAll(),
|
||||
// handleCrash(err, emitter, log))
|
||||
// .subscribe();
|
||||
}
|
||||
|
||||
@@ -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';
|
||||
|
||||
Reference in New Issue
Block a user