Skip to content

Commit

Permalink
refactor: adjust opinionated path module insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaz committed Mar 28, 2024
1 parent fda129f commit e9b3822
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 38 deletions.
2 changes: 1 addition & 1 deletion expressots.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ExpressoConfig, Pattern } from "./src/types";
const config: ExpressoConfig = {
sourceRoot: "src",
scaffoldPattern: Pattern.KEBAB_CASE,
opinionated: true,
opinionated: false,
scaffoldSchematics: {
entity: "model",
provider: "adapter",
Expand Down
4 changes: 2 additions & 2 deletions src/generate/templates/opinionated/dto.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface I{{className}}RequestDto {}
export interface I{{className}}RequestDTO {}

export interface I{{className}}ResponseDto {}
export interface I{{className}}ResponseDTO {}
4 changes: 2 additions & 2 deletions src/generate/templates/opinionated/entity.tpl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { provide } from "inversify-binding-decorators";
import { randomUUID } from "node:crypto";

@provide({{className}})
export class {{className}} {
@provide({{className}}Entity)
export class {{className}}Entity {
id: string;
constructor() {
Expand Down
8 changes: 8 additions & 0 deletions src/generate/templates/opinionated/usecase-service-delete.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { provide } from "inversify-binding-decorators";

@provide({{className}}UseCase)
export class {{className}}UseCase {
execute(id: string) {
return "Use Case";
}
}
2 changes: 1 addition & 1 deletion src/generate/templates/opinionated/usecase.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { provide } from "inversify-binding-decorators";
@provide({{className}}UseCase)
export class {{className}}UseCase {
execute() {
return "UseCase";
return "Use Case";
}
}
29 changes: 27 additions & 2 deletions src/generate/utils/command-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,43 @@ export async function validateAndPrepareFile(fp: FilePreparation) {
process.exit(1);
}

let folderSchematic = "";
if (opinionated) {
folderSchematic = schematicFolder(fp.schematic);
const folderSchematic = schematicFolder(fp.schematic);

const folderToScaffold = `${sourceRoot}/${folderSchematic}`;
const { path, file, className, moduleName, modulePath } =
await splitTarget({
target: fp.target,
schematic: fp.schematic,
});

const outputPath = `${folderToScaffold}/${path}/${file}`;
await verifyIfFileExists(outputPath, fp.schematic);
mkdirSync(`${folderToScaffold}/${path}`, { recursive: true });

return {
path,
file,
className,
moduleName,
modulePath,
outputPath,
folderToScaffold,
fileName: getFileNameWithoutExtension(file),
schematic: fp.schematic,
};
}

const folderSchematic = "";

const folderToScaffold = `${sourceRoot}/${folderSchematic}`;
const { path, file, className, moduleName, modulePath } = await splitTarget(
{
target: fp.target,
schematic: fp.schematic,
},
);

const fileBaseSchema =
scaffoldSchematics?.[fp.schematic as keyof typeof scaffoldSchematics];
const validateFileSchema =
Expand Down
106 changes: 78 additions & 28 deletions src/generate/utils/opinionated-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function opinionatedProcess(
method: string,
expressoConfig: ExpressoConfig,
): Promise<string> {
let f: FileOutput = await validateAndPrepareFile({
const f: FileOutput = await validateAndPrepareFile({
schematic,
target,
method,
Expand All @@ -40,41 +40,42 @@ export async function opinionatedProcess(
f.file,
);

f = await validateAndPrepareFile({
const u = await validateAndPrepareFile({
schematic: "usecase",
target,
method,
expressoConfig,
});
await generateUseCase(
f.outputPath,
f.className,
f.moduleName,
f.path,
f.fileName,
"../templates/opinionated/usecase-service.tpl",
await generateUseCaseService(
u.outputPath,
u.className,
method,
u.moduleName,
u.path,
u.fileName,
);

f = await validateAndPrepareFile({
const d = await validateAndPrepareFile({
schematic: "dto",
target,
method,
expressoConfig,
});
await generateDTO(f.outputPath, f.className, f.moduleName, f.path);
await generateDTO(d.outputPath, d.className, d.moduleName, d.path);

f = await validateAndPrepareFile({
const m = await validateAndPrepareFile({
schematic: "module",
target,
method,
expressoConfig,
});
await generateModuleService(
f.className,
f.moduleName,
f.path,
f.file,
f.folderToScaffold,
f.outputPath,
m.className,
m.moduleName,
m.path,
m.file,
m.folderToScaffold,
);

await printGenerateSuccess("controller", f.file);
Expand Down Expand Up @@ -214,18 +215,62 @@ async function generateControllerService(
* @param path - The path
* @param template - The template
*/
async function generateUseCaseService(
outputPath: string,
className: string,
method: string,
moduleName: string,
path: string,
fileName: string,
): Promise<void> {
let templateBasedMethod = "";

switch (method) {
case "put":
templateBasedMethod =
"../templates/opinionated/usecase-service.tpl";
break;
case "patch":
templateBasedMethod =
"../templates/opinionated/usecase-service.tpl";
break;
case "post":
templateBasedMethod =
"../templates/opinionated/usecase-service.tpl";
break;
case "delete":
templateBasedMethod =
"../templates/opinionated/usecase-service-delete.tpl";
break;
default:
templateBasedMethod = "../templates/opinionated/usecase.tpl";
break;
}
writeTemplate({
outputPath,
template: {
path: templateBasedMethod,
data: {
className,
moduleName,
path,
fileName,
},
},
});
}

async function generateUseCase(
outputPath: string,
className: string,
moduleName: string,
path: string,
fileName: string,
template?: string,
): Promise<void> {
writeTemplate({
outputPath,
template: {
path: template ? template : "../templates/opinionated/usecase.tpl",
path: "../templates/opinionated/usecase.tpl",
data: {
className,
moduleName,
Expand Down Expand Up @@ -382,6 +427,7 @@ async function generateMiddleware(
* @param path - The path
*/
async function generateModuleService(
outputPathController: string,
className: string,
moduleName: string,
path: string,
Expand All @@ -393,14 +439,18 @@ async function generateModuleService(
.join(folderToScaffold, path, "..")
.normalize();
const newModuleName = `${newModuleFile}.module.ts`;
const newModuleOutputPath = `${newModulePath}/${newModuleName}`;
const newModuleOutputPath = `${newModulePath}/${newModuleName}`.replace(
"\\",
"/",
);

const controllerToModule = nodePath
.relative(newModuleOutputPath, outputPathController)
.normalize()
.replace(/\.ts$/, "")
.replace(/\\/g, "/")
.replace(/\.\./g, ".");

const controllerPathLength = path.split("/").length - 1 - 1;
const controllerPath = path.split("/")[controllerPathLength];
const controllerName = file
.replace("module", "controller")
.replace(".ts", "");
const controllerFileName = `./${controllerPath}/${controllerName}`;
const controllerFullPath = nodePath
.join(folderToScaffold, path, "..", newModuleName)
.normalize();
Expand All @@ -409,7 +459,7 @@ async function generateModuleService(
await addControllerToModule(
controllerFullPath,
`${className}Controller`,
controllerFileName,
controllerToModule,
);
return;
}
Expand All @@ -421,7 +471,7 @@ async function generateModuleService(
data: {
className,
moduleName: anyCaseToPascalCase(moduleName),
path: controllerFileName,
path: controllerToModule,
},
},
});
Expand Down
4 changes: 2 additions & 2 deletions src/utils/add-module-to-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ async function addModuleToContainer(
if (path.split("/").length > 1) {
newImport = `import { ${moduleName}Module } from "${usecaseDir}${name.toLowerCase()}/${name.toLowerCase()}.module";`;
} else {
newImport = `import { ${moduleName}Module } from "${usecaseDir}${name}.module";`;
newImport = `import { ${moduleName}Module } from "${usecaseDir}${name.toLowerCase()}.module";`;
}
} else {
newImport = `import { ${moduleName}Module } from "${usecaseDir}${name}/${name}.module";`;
newImport = `import { ${moduleName}Module } from "${usecaseDir}${name}/${name.toLowerCase()}.module";`;
}

if (
Expand Down

0 comments on commit e9b3822

Please sign in to comment.