From 071d5eac490315065238ba2cf083617d0f32db2f Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Sun, 28 Apr 2024 22:16:54 -0500 Subject: [PATCH] the end of sern init?? --- src/core/create-plugins.ts | 5 +---- src/core/ioc/base.ts | 5 +---- src/core/ioc/dependency-injection.ts | 7 ------- src/core/module-loading.ts | 6 +++--- src/index.ts | 8 +++++--- src/sern.ts | 17 +++++++++-------- src/types/core.ts | 12 ------------ test/core/ioc.test.ts | 9 +++------ 8 files changed, 22 insertions(+), 47 deletions(-) delete mode 100644 src/types/core.ts diff --git a/src/core/create-plugins.ts b/src/core/create-plugins.ts index b8d6192..4db4ce6 100644 --- a/src/core/create-plugins.ts +++ b/src/core/create-plugins.ts @@ -7,10 +7,7 @@ export function makePlugin( type: PluginType, execute: (...args: any[]) => any, ): Plugin { - return { - type, - execute, - } as Plugin; + return { type, execute, } as Plugin; } /** * @since 2.5.0 diff --git a/src/core/ioc/base.ts b/src/core/ioc/base.ts index 6c65c75..2f3d90b 100644 --- a/src/core/ioc/base.ts +++ b/src/core/ioc/base.ts @@ -1,5 +1,4 @@ import * as assert from 'assert'; -import { useContainer } from './dependency-injection'; import type { CoreDependencies, DependencyConfiguration } from '../../types/ioc'; import { CoreContainer } from './container'; import { Result } from 'ts-results-es'; @@ -141,7 +140,7 @@ function composeRoot( container.ready(); } -export async function makeDependencies +export async function makeDependencies (conf: ValidDependencyConfig) { containerSubject = new CoreContainer(); if(typeof conf === 'function') { @@ -160,7 +159,5 @@ export async function makeDependencies } else { composeRoot(containerSubject, conf); } - - return useContainer(); } diff --git a/src/core/ioc/dependency-injection.ts b/src/core/ioc/dependency-injection.ts index f792b8a..ac1b369 100644 --- a/src/core/ioc/dependency-injection.ts +++ b/src/core/ioc/dependency-injection.ts @@ -48,10 +48,3 @@ export function Services(...keys: [...T] const container = useContainerRaw(); return keys.map(k => container.get(k)!) as IntoDependencies; } - - -export function useContainer() { - return (...keys: [...V]) => - keys.map(key => useContainerRaw().get(key as keyof Dependencies)) as IntoDependencies; -} - diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index 3f6ff9e..48c6886 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -1,7 +1,7 @@ import path from 'node:path'; import assert from 'assert'; import { createRequire } from 'node:module'; -import type { ImportPayload, Wrapper } from '../types/core'; +import type { Wrapper } from '../types/core'; import { existsSync } from 'fs'; import type { Logging } from './interfaces'; @@ -17,7 +17,7 @@ export const parseCallsite = (fpath: string) => { export const shouldHandle = (pth: string, filenam: string) => { const file_name = filenam+path.extname(pth); let newPath = path.join(path.dirname(pth), file_name) - .replace(/file:\\?/, ""); + .replace(/file:\\?/, ""); return { exists: existsSync(newPath), path: 'file:///'+newPath }; } @@ -42,7 +42,7 @@ export async function importModule(absPath: string) { let commandModule = fileModule.default; - assert(commandModule , `Found no export @ ${absPath}. Forgot to ignore with "!"? (!${path.basename(absPath)})?`); + assert(commandModule , `No export @ ${absPath}. Forgot to ignore with "!"? (!${path.basename(absPath)})?`); if ('default' in commandModule) { commandModule = commandModule.default; } diff --git a/src/index.ts b/src/index.ts index 9bbe41e..dcb35dc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -36,10 +36,12 @@ export type { AnyCommandPlugin, } from './types/core-plugin'; -export type { Wrapper } from './types/core'; - +export interface Wrapper { + commands: string; + defaultPrefix?: string; + events?: string; +} export type { Args, SlashOptions, Payload, SernEventsMapping } from './types/utility'; - export type { Singleton, Transient, CoreDependencies } from './types/ioc'; export { diff --git a/src/sern.ts b/src/sern.ts index b64d79a..d716ee1 100644 --- a/src/sern.ts +++ b/src/sern.ts @@ -1,15 +1,16 @@ import { handleCrash } from './handlers/event-utils'; import callsites from 'callsites'; import { Files } from './core/_internal'; +import path from 'node:path' import { merge } from 'rxjs'; import { Services } from './core/ioc'; -import { Wrapper } from './types/core'; import { eventsHandler } from './handlers/user-defined-events'; import { readyHandler } from './handlers/ready-event'; import { messageHandler } from './handlers/message-event'; import { interactionHandler } from './handlers/interaction-event'; import { presenceHandler } from './handlers/presence'; import type { Client } from 'discord.js'; +import { type Wrapper } from './'; /** @@ -24,7 +25,7 @@ import type { Client } from 'discord.js'; * }) * ``` */ -export function init(maybeWrapper: Wrapper | 'file') { +export function init() { const startTime = performance.now(); const dependencies = Services('@sern/emitter', '@sern/errors', @@ -33,14 +34,15 @@ export function init(maybeWrapper: Wrapper | 'file') { const logger = dependencies[2], errorHandler = dependencies[1]; - const wrapper = Files.loadConfig(maybeWrapper, logger); - if (wrapper.events !== undefined) { + //const wrapper = Files.loadConfig(maybeWrapper, logger); + //if (wrapper.events !== undefined) { //eventsHandler(dependencies, Files.getFullPathTree(wrapper.events)); - } - + // } + //import(path.resolve("commands.js")) + //.then(({ commands }) => { }) + //.catch(message => logger?.error({ message })) const initCallsite = callsites()[1].getFileName(); const presencePath = Files.shouldHandle(initCallsite!, "presence"); - //Ready event: load all modules and when finished, time should be taken and logged // readyHandler(dependencies, Files.getFullPathTree(wrapper.commands)) // .add(() => { @@ -55,7 +57,6 @@ export function init(maybeWrapper: Wrapper | 'file') { // presenceHandler(presencePath.path, setPresence).subscribe(); // } // }); - //const messages$ = messageHandler(dependencies, wrapper.defaultPrefix); //const interactions$ = interactionHandler(dependencies); // listening to the message stream and interaction stream diff --git a/src/types/core.ts b/src/types/core.ts deleted file mode 100644 index e388cf7..0000000 --- a/src/types/core.ts +++ /dev/null @@ -1,12 +0,0 @@ - -export interface ImportPayload { - module: T; - absPath: string; - [key: string]: unknown; -} - -export interface Wrapper { - commands: string; - defaultPrefix?: string; - events?: string; -} diff --git a/test/core/ioc.test.ts b/test/core/ioc.test.ts index e790307..51f712b 100644 --- a/test/core/ioc.test.ts +++ b/test/core/ioc.test.ts @@ -1,7 +1,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; import { CoreContainer } from '../../src/core/ioc/container'; import { EventEmitter } from 'events'; -import { DefaultLogging, Disposable, Emitter, Init, Logging } from '../../src/core'; +import { Disposable, Emitter, Init, Logging, __Services } from '../../src/core'; import { CoreDependencies } from '../../src/types/ioc'; describe('ioc container', () => { @@ -43,13 +43,12 @@ describe('ioc container', () => { }); it('should container all core dependencies', async () => { const keys = [ - '@sern/modules', '@sern/emitter', '@sern/logger', '@sern/errors', ] satisfies (keyof CoreDependencies)[]; container.add({ - '@sern/logger': () => new DefaultLogging(), + '@sern/logger': () => new __Services.DefaultLogging(), '@sern/client': () => new EventEmitter(), }); for (const k of keys) { @@ -92,9 +91,7 @@ describe('ioc container', () => { it('should init dependency depending on something else', () => { container.add({ '@sern/client': dependency2 }); - container.upsert((cntr) => ({ - '@sern/logger': dependency - })); + container.upsert((cntr) => ({ '@sern/logger': dependency })); container.ready(); expect(dependency.init).toHaveBeenCalledTimes(1); })