Skip to content

Commit

Permalink
feat: add Nested path validation
Browse files Browse the repository at this point in the history
  • Loading branch information
juliano-soares committed Mar 29, 2024
1 parent b5738e5 commit 0a105bb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
18 changes: 7 additions & 11 deletions src/generate/utils/opinionated-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -567,16 +565,15 @@ async function generateModuleServiceSinglePath(
async function generateModuleServiceNestedPath(
outputPathController: string,
className: string,
moduleName: string,
path: string,
file: string,
folderToScaffold: string,
): Promise<void> {
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(
"\\",
"/",
Expand Down Expand Up @@ -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,
);
}
Expand Down
53 changes: 52 additions & 1 deletion src/utils/add-module-to-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

0 comments on commit 0a105bb

Please sign in to comment.