From c9f2d756656cb2f63c527bb1bfb86ab49c396237 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Fri, 18 Aug 2023 20:37:47 -0500 Subject: [PATCH] deprecate: mode (#325) * test: add tests for context * deprecate: mode * revert docs for deprecated option --- src/core/module-loading.ts | 71 ++++++++++++++++---------------------- src/sern.ts | 13 ++----- src/types/core.ts | 3 +- 3 files changed, 34 insertions(+), 53 deletions(-) diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index 0298c7b1..94f635aa 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -33,6 +33,7 @@ export async function importModule(absPath: string) { .wrap(() => module.getInstance()) .unwrapOr(module) as T; } + export async function defaultModuleLoader(absPath: string): ModuleResult { let module = await importModule(absPath); assert(module, `Found an undefined module: ${absPath}`); @@ -53,7 +54,7 @@ export function buildModuleStream( return from(input).pipe(mergeMap(defaultModuleLoader)); } -export const getFullPathTree = (dir: string, mode: boolean) => readPaths(resolve(dir), mode); +export const getFullPathTree = (dir: string) => readPaths(resolve(dir)); export const filename = (path: string) => fmtFileName(basename(path)); @@ -70,22 +71,18 @@ async function deriveFileInfo(dir: string, file: string) { base: basename(file), }; } -async function* readPaths(dir: string, shouldDebug: boolean): AsyncGenerator { +async function* readPaths(dir: string): AsyncGenerator { try { 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)) { - if (shouldDebug) console.info(`ignored directory: ${fullPath}`); - } else { - yield* readPaths(fullPath, shouldDebug); + if (!isSkippable(base)) { + yield* readPaths(fullPath); } } else { - if (isSkippable(base)) { - if (shouldDebug) console.info(`ignored: ${fullPath}`); - } else { + if (!isSkippable(base)) { yield 'file:///' + fullPath; } } @@ -98,38 +95,30 @@ async function* readPaths(dir: string, shouldDebug: boolean): AsyncGenerator - config.language === 'typescript' - ? join('dist', config.paths[dir]!) - : join(config.paths[dir]!); - - console.log('Loading config: ', config); - const commandsPath = makePath('commands'); + if (wrapper !== 'file') { + return wrapper; + } + console.log('Experimental loading of sern.config.json'); + const config = requir(resolve('sern.config.json')); - console.log('Commands path is set to', commandsPath); - let eventsPath: string | undefined; - if (config.paths.events) { - eventsPath = makePath('events'); - console.log('Events path is set to', eventsPath); - } - return { - defaultPrefix: config.defaultPrefix, - commands: commandsPath, - events: eventsPath, - mode: config.mode, - }; + const makePath = (dir: PropertyKey) => + config.language === 'typescript' + ? join('dist', config.paths[dir]!) + : join(config.paths[dir]!); + + console.log('Loading config: ', config); + const commandsPath = makePath('commands'); + + console.log('Commands path is set to', commandsPath); + let eventsPath: string | undefined; + if (config.paths.events) { + eventsPath = makePath('events'); + console.log('Events path is set to', eventsPath); } - return wrapper; + return { + defaultPrefix: config.defaultPrefix, + commands: commandsPath, + events: eventsPath, + }; + } diff --git a/src/sern.ts b/src/sern.ts index 7a6b0d6f..0dfd5b68 100644 --- a/src/sern.ts +++ b/src/sern.ts @@ -27,13 +27,12 @@ export function init(maybeWrapper: Wrapper | 'file') { const dependencies = useDependencies(); const logger = dependencies[2], errorHandler = dependencies[1]; - const mode = isDevMode(wrapper.mode ?? process.env.MODE); if (wrapper.events !== undefined) { - eventsHandler(dependencies, Files.getFullPathTree(wrapper.events, mode)); + eventsHandler(dependencies, Files.getFullPathTree(wrapper.events)); } //Ready event: load all modules and when finished, time should be taken and logged - startReadyEvent(dependencies, Files.getFullPathTree(wrapper.commands, mode)).add(() => { + startReadyEvent(dependencies, Files.getFullPathTree(wrapper.commands)).add(() => { const time = ((performance.now() - startTime) / 1000).toFixed(2); dependencies[0].emit('modulesLoaded'); logger?.info({ @@ -47,14 +46,6 @@ export function init(maybeWrapper: Wrapper | 'file') { merge(messages$, interactions$).pipe(handleCrash(errorHandler, logger)).subscribe(); } -function isDevMode(mode: string | undefined) { - console.info(`Detected mode: "${mode}"`); - if (mode === undefined) { - console.info('No mode found in process.env, assuming DEV'); - } - return mode === 'DEV' || mode == undefined; -} - function useDependencies() { return Services( '@sern/emitter', diff --git a/src/types/core.ts b/src/types/core.ts index 34074c6d..7473aa8b 100644 --- a/src/types/core.ts +++ b/src/types/core.ts @@ -10,8 +10,9 @@ export interface Wrapper { events?: string; /** * Overload to enable mode in case developer does not use a .env file. + * @deprecated - https://github.com/sern-handler/handler/pull/325 */ - mode?: 'DEV' | 'PROD'; + mode?: string /* * @deprecated */