From 9a4a57abc931a822a13a6f469392683a79c71f8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pe=C3=AFo=20Thibault?= Date: Mon, 30 Oct 2023 21:11:47 +0100 Subject: [PATCH] fix(core): ensure modules have unique names (#120) --- src/core/checkUniqueModuleNames.ts | 0 src/core/createEnvForModule.ts | 11 +++++++---- src/main.ts | 16 +++++++++++++++- src/modules/modules.ts | 15 --------------- 4 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 src/core/checkUniqueModuleNames.ts delete mode 100644 src/modules/modules.ts diff --git a/src/core/checkUniqueModuleNames.ts b/src/core/checkUniqueModuleNames.ts new file mode 100644 index 00000000..e69de29b diff --git a/src/core/createEnvForModule.ts b/src/core/createEnvForModule.ts index dc1fe4de..1ac26242 100644 --- a/src/core/createEnvForModule.ts +++ b/src/core/createEnvForModule.ts @@ -17,12 +17,15 @@ const createEnvForModule = (constantName: string) => return acc; }, {}); -export const createAllModules = async ( - modules: Record, -): Promise => { +export const createAllModules = async (modules: ModuleCreator[]): Promise => { + const uniqueModuleNames = new Set(modules.map((module) => module.name)); + if (uniqueModuleNames.size !== modules.length) { + throw new Error('Found duplicate module names'); + } + const createdModules: CreatedModule[] = []; - for (const { name, factory } of Object.values(modules)) { + for (const { name, factory } of modules) { const moduleConstantName = constantCase(name); const env = createEnvForModule(moduleConstantName); const module = await factory({ env }); diff --git a/src/main.ts b/src/main.ts index 222428f1..d4677ffe 100644 --- a/src/main.ts +++ b/src/main.ts @@ -4,7 +4,21 @@ import { createAllModules } from './core/createEnvForModule'; import { env } from './core/env'; import { getIntentsFromModules } from './core/getIntentsFromModules'; import { loadModules } from './core/loadModules'; -import { modules } from './modules/modules'; +import { coolLinksManagement } from './modules/coolLinksManagement/coolLinksManagement.module'; +import { fart } from './modules/fart/fart.module'; +import { fixEmbedTwitterVideo } from './modules/fixEmbedTwitterVideo/fixEmbedTwitterVideo.module'; +import { quoiFeur } from './modules/quoiFeur/quoiFeur.module'; +import { recurringMessage } from './modules/recurringMessage/recurringMessage.module'; +import { voiceOnDemand } from './modules/voiceOnDemand/voiceOnDemand.module'; + +const modules = [ + fart, + voiceOnDemand, + coolLinksManagement, + quoiFeur, + recurringMessage, + fixEmbedTwitterVideo, +]; const createdModules = await createAllModules(modules); diff --git a/src/modules/modules.ts b/src/modules/modules.ts deleted file mode 100644 index 3c4bf9bf..00000000 --- a/src/modules/modules.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { coolLinksManagement } from './coolLinksManagement/coolLinksManagement.module'; -import { fart } from './fart/fart.module'; -import { fixEmbedTwitterVideo } from './fixEmbedTwitterVideo/fixEmbedTwitterVideo.module'; -import { quoiFeur } from './quoiFeur/quoiFeur.module'; -import { recurringMessage } from './recurringMessage/recurringMessage.module'; -import { voiceOnDemand } from './voiceOnDemand/voiceOnDemand.module'; - -export const modules = { - fart, - voiceOnDemand, - coolLinksManagement, - quoiFeur, - recurringMessage, - fixEmbedTwitterVideo, -};