mirror of
https://github.com/sern-handler/handler
synced 2026-06-24 00:32:16 +00:00
chore: revert multi and clean up code
This commit is contained in:
@@ -1,12 +1,61 @@
|
||||
import { readdir, stat } from 'fs/promises';
|
||||
import { join, basename, resolve } from 'path';
|
||||
import { type Observable, from, mergeMap } from 'rxjs';
|
||||
import { SernError } from './structures/errors';
|
||||
import { type Result, Err, Ok } from 'ts-results-es';
|
||||
import { Processed } from '../types/core';
|
||||
import { Module } from '../types/module';
|
||||
import * as assert from 'node:assert'
|
||||
import * as util from 'node:util'
|
||||
import { type Observable, from, mergeMap, ObservableInput } from 'rxjs';
|
||||
import { readdir, stat } from 'fs/promises';
|
||||
import { basename, join, resolve } from 'path';
|
||||
|
||||
type ModuleResult<T> = Promise<Result<Processed<T>, SernError>>
|
||||
export type Loader<T> = (absPath: string) => ModuleResult<T>
|
||||
|
||||
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
|
||||
if (module === undefined) {
|
||||
return Err(SernError.UndefinedModule);
|
||||
}
|
||||
try {
|
||||
module = new (module as unknown as new () => T)();
|
||||
} catch {}
|
||||
checkIsProcessed(module)
|
||||
return Ok(module);
|
||||
}
|
||||
|
||||
function checkIsProcessed<T extends Module>(m: T): asserts m is Processed<T> {
|
||||
assert.ok(m.name !== undefined, `name is not defined for ${util.format(m)}`)
|
||||
}
|
||||
|
||||
|
||||
export const fmtFileName = (n: string) => n.substring(0, n.length - 3);
|
||||
/**
|
||||
* 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<Result<Processed<T>, SernError>> {
|
||||
return from(input).pipe(mergeMap(defaultModuleLoader<T>));
|
||||
}
|
||||
|
||||
export function getCommands(dir: string) {
|
||||
return readPath(resolve(dir));
|
||||
}
|
||||
|
||||
export function filename(path: string) {
|
||||
return fmtFileName(basename(path))
|
||||
}
|
||||
|
||||
async function* readPath(dir: string): AsyncGenerator<string> {
|
||||
try {
|
||||
@@ -29,53 +78,6 @@ async function* readPath(dir: string): AsyncGenerator<string> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const fmtFileName = (n: string) => n.substring(0, n.length - 3);
|
||||
|
||||
export async function defaultModuleLoader<T extends Module>(
|
||||
absPath: string,
|
||||
): Promise<Result< Processed<T>, SernError>> {
|
||||
// prettier-ignore
|
||||
let module: T | undefined
|
||||
/// #if MODE === 'esm'
|
||||
= (await import(absPath)).default
|
||||
/// #elif MODE === 'cjs'
|
||||
= require(absPath).default; // eslint-disable-line
|
||||
/// #endif
|
||||
if (module === undefined) {
|
||||
return Err(SernError.UndefinedModule);
|
||||
}
|
||||
try {
|
||||
module = new (module as unknown as new () => T)();
|
||||
} catch {}
|
||||
checkIsProcessed(module)
|
||||
return Ok(module);
|
||||
}
|
||||
|
||||
function checkIsProcessed<T extends Module>(m: T): asserts m is Processed<T> {
|
||||
assert.ok(m.name !== undefined, `name is not defined for ${util.format(m)}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 >(
|
||||
commandDir: string,
|
||||
): Observable<Result<Processed<T>, SernError>> {
|
||||
const commands = getCommands(commandDir);
|
||||
return from(commands).pipe(mergeMap(defaultModuleLoader<T>));
|
||||
}
|
||||
|
||||
export function getCommands(dir: string) {
|
||||
return readPath(resolve(dir));
|
||||
}
|
||||
|
||||
export function filename(path: string) {
|
||||
return fmtFileName(basename(path))
|
||||
}
|
||||
//https://stackoverflow.com/questions/16697791/nodejs-get-filename-of-caller-function
|
||||
export function filePath() {
|
||||
const err = new Error();
|
||||
@@ -89,5 +91,4 @@ export function filePath() {
|
||||
if(path === null) {
|
||||
throw Error("Could not get the name of commandModule.")
|
||||
}
|
||||
return path;
|
||||
}
|
||||
return path; }
|
||||
|
||||
Reference in New Issue
Block a user