build: refactor/building (#252)

* refactor: conditional compilation of loading esm/cjs modules

* refactor: move file loading file

* refactor: add conditional compilation for building modules

* refactor: add conditional compilation for building modules

* perf: decrease build times

* test

* revert: typo and clean code

* build: smaller build

* chore:cleanscripts

* chore:refactor readme

* build:automerge lockfile

* chore: remove build and upgrade readme

* fix: dropdown

* chore: fix

* chore: more docs

---------

Co-authored-by: jacoobes <jacobnguyend@gmail.com>
This commit is contained in:
Jacob Nguyen
2023-03-15 21:08:27 -05:00
committed by GitHub
parent d381ff568e
commit 74c4b77d4b
14 changed files with 1771 additions and 2492 deletions

View File

@@ -28,7 +28,7 @@ const createMessageProcessor = (
//This concatMap checks if module is undefined, and if it is, do not continue.
// Synonymous to filterMap, but I haven't thought of a generic implementation for filterMap yet
concatMap(message => {
const [prefix, ...rest] = fmt(message, defaultPrefix);
const [prefix, ...rest] = fmt(message.content, defaultPrefix);
const module = get(ms => ms.TextCommands.get(prefix) ?? ms.BothCommands.get(prefix));
if (module === undefined) {
return EMPTY;

View File

@@ -3,7 +3,6 @@ import { concatMap, EMPTY, from, Observable, of, pipe, tap, throwError } from 'r
import type { SernError } from '../structures';
import { Result } from 'ts-results-es';
import type { AnyModule, CommandModule, EventModule, Module } from '../../types/module';
import { _const as i } from '../utilities/functions';
import SernEmitter from '../sernEmitter';
import { callPlugin, everyPluginOk, filterMapTo } from './operators';
import type { Processed } from '../../types/handler';
@@ -78,7 +77,7 @@ export function executeModule(
emitter.emit('module.activate', SernEmitter.success(module));
return EMPTY;
} else {
return throwError(i(SernEmitter.failure(module, result.val)));
return throwError(() => SernEmitter.failure(module, result.val));
}
}),
);

View File

@@ -1,5 +1,5 @@
import { fromEvent, pipe, switchMap, take } from 'rxjs';
import * as Files from '../utilities/readFile';
import * as Files from '../module-loading/readFile';
import { errTap, scanModule } from './observableHandling';
import { CommandType, type ModuleStore, SernError } from '../structures';
import { match } from 'ts-pattern';
@@ -8,13 +8,13 @@ import { ApplicationCommandType, ComponentType } from 'discord.js';
import type { CommandModule } from '../../types/module';
import type { Processed } from '../../types/handler';
import type { ErrorHandling, Logging, ModuleManager } from '../contracts';
import { _const, err, ok } from '../utilities/functions';
import { err, ok } from '../utilities/functions';
import { defineAllFields } from './operators';
import SernEmitter from '../sernEmitter';
import type { EventEmitter } from 'node:events';
export function makeReadyEvent(
[sEmitter, client, errorHandler, , moduleManager]: [
[sEmitter, client, errorHandler, ,moduleManager]: [
SernEmitter,
EventEmitter,
ErrorHandling,
@@ -61,7 +61,7 @@ function registerModule<T extends Processed<CommandModule>>(
): Result<void, void> {
const name = mod.name;
const insert = (cb: (ms: ModuleStore) => void) => {
const set = Result.wrap(_const(manager.set(cb)));
const set = Result.wrap(() => manager.set(cb));
return set.ok ? ok() : err();
};
return match(mod as Processed<CommandModule>)

View File

@@ -1,5 +1,5 @@
import { catchError, finalize, map, tap, mergeAll } from 'rxjs';
import { buildData } from '../utilities/readFile';
import { catchError, finalize, map, mergeAll } from 'rxjs';
import { buildData } from '../module-loading/readFile';
import type { Dependencies, Processed } from '../../types/handler';
import { errTap, scanModule } from './observableHandling';
import type { CommandModule, EventModule } from '../../types/module';

View File

@@ -1,6 +1,6 @@
import { readdirSync, statSync } from 'fs';
import { join } from 'path';
import { type Observable, from, concatAll } from 'rxjs';
import { type Observable, from, mergeAll } from 'rxjs';
import { SernError } from '../structures/errors';
import { type Result, Err, Ok } from 'ts-results-es';
@@ -18,16 +18,13 @@ function readPath(dir: string, arrayOfFiles: string[] = []): string[] {
return arrayOfFiles;
}
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 buildData<T>(commandDir: string): Observable<
Result<
{
@@ -41,13 +38,14 @@ export function buildData<T>(commandDir: string): Observable<
return from(
Promise.all(
commands.map(async absPath => {
let module: T | undefined;
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
module = require(absPath).default;
} catch {
module = (await import(`file:///` + absPath)).default;
}
let module: T | undefined
/// #if MODE === 'esm'
= (await import(`file:///` + absPath)).default
/// #elif MODE === 'cjs'
= require(absPath).default;
/// #endif
if (module === undefined) {
return Err(SernError.UndefinedModule);
}
@@ -57,7 +55,7 @@ export function buildData<T>(commandDir: string): Observable<
return Ok({ module, absPath });
}),
),
).pipe(concatAll());
).pipe(mergeAll());
}
export function getCommands(dir: string): string[] {

View File

@@ -1,4 +1,4 @@
import * as Files from './readFile';
import * as Files from '../module-loading/readFile';
import { basename } from 'path';
import { Err, Ok } from 'ts-results-es';
/**

View File

@@ -1,4 +1,3 @@
import type { Message } from 'discord.js';
/**
* Removes the first character(s) _[depending on prefix length]_ of the message
@@ -10,6 +9,6 @@ import type { Message } from 'discord.js';
* console.log(fmt(message, '!'));
* // [ 'ping' ]
*/
export function fmt(msg: Message, prefix: string): string[] {
return msg.content.slice(prefix.length).trim().split(/\s+/g);
export function fmt(msg: string, prefix: string): string[] {
return msg.slice(prefix.length).trim().split(/\s+/g);
}