diff --git a/src/generate/utils/opinionated-cmd.ts b/src/generate/utils/opinionated-cmd.ts index 6522637..c41d823 100644 --- a/src/generate/utils/opinionated-cmd.ts +++ b/src/generate/utils/opinionated-cmd.ts @@ -16,7 +16,7 @@ import { writeTemplate, } from "./command-utils"; import { addControllerToModule } from "../../utils/add-controller-to-module"; -import { addModuleToContainer } from "../../utils/add-module-to-container"; +import { addModuleToContainer, addModuleToContainerNestedPath } from "../../utils/add-module-to-container"; import { ExpressoConfig } from "../../@types"; export async function opinionatedProcess( @@ -87,9 +87,7 @@ export async function opinionatedProcess( await generateModuleServiceNestedPath( f.outputPath, m.className, - m.moduleName, m.path, - m.file, m.folderToScaffold, ); } else if (pathStyle === PathStyle.Single) { @@ -567,16 +565,15 @@ async function generateModuleServiceSinglePath( async function generateModuleServiceNestedPath( outputPathController: string, className: string, - moduleName: string, path: string, - file: string, folderToScaffold: string, ): Promise { - const newModuleFile = await extractFirstWord(file); + const moduleFileName = nodePath.basename(path, '/'); const newModulePath = nodePath .join(folderToScaffold, path, "..") .normalize(); - const newModuleName = `${newModuleFile}.module.ts`; + + const newModuleName = `${moduleFileName}.module.ts`; const newModuleOutputPath = `${newModulePath}/${newModuleName}`.replace( "\\", "/", @@ -608,15 +605,14 @@ async function generateModuleServiceNestedPath( path: "../templates/opinionated/module-service.tpl", data: { className, - moduleName: anyCaseToPascalCase(moduleName), + moduleName: anyCaseToPascalCase(moduleFileName), path: controllerToModule, }, }, }); - await addModuleToContainer( - anyCaseToPascalCase(moduleName), - `${moduleName}/${file.replace(".ts", "")}`, + await addModuleToContainerNestedPath( + anyCaseToPascalCase(moduleFileName), path, ); } diff --git a/src/utils/add-module-to-container.ts b/src/utils/add-module-to-container.ts index e9cc292..da3e1bb 100644 --- a/src/utils/add-module-to-container.ts +++ b/src/utils/add-module-to-container.ts @@ -127,4 +127,55 @@ async function addModuleToContainer( await fs.promises.writeFile(containerData.path, newFileContent, "utf8"); } -export { addModuleToContainer }; +async function addModuleToContainerNestedPath( + name: string, + path?: string, +) { + const containerData: AppContainerType = await validateAppContainer(); + + const moduleName = (name[0].toUpperCase() + name.slice(1)).trimStart(); + const { opinionated } = await Compiler.loadConfig(); + + let usecaseDir: string; + if (opinionated) { + usecaseDir = `@useCases/`; + } else { + usecaseDir = `./`; + } + + if (path.endsWith('/')) { + path = path.slice(0, -1); + } + + const newImport = `import { ${moduleName}Module } from "${usecaseDir}${path}.module";`; + + if ( + containerData.imports.includes(newImport) && + containerData.modules.includes(`${moduleName}Module`) + ) { + return; + } + + containerData.imports.push(newImport); + containerData.modules.push(`${moduleName}Module`); + + const newModule = containerData.modules.join(", "); + const newModuleDeclaration = `.create([${newModule}]`; + + const newFileContent = [ + ...containerData.imports, + ...containerData.notImports, + ] + .join("\n") + .replace(containerData.regex, newModuleDeclaration); + + console.log( + " ", + chalk.greenBright(`[container]`.padEnd(14)), + chalk.bold.white(`${moduleName}Module added to ${APP_CONTAINER}! ✔️`), + ); + + await fs.promises.writeFile(containerData.path, newFileContent, "utf8"); +} + +export { addModuleToContainer, addModuleToContainerNestedPath };