Skip to content

Commit

Permalink
Fix controllerPath on addControllerToModule (#13)
Browse files Browse the repository at this point in the history
* Fixed controllerPath on addControllerToModule

* fix: removing empty string on controller to module

---------

Co-authored-by: Richard Zampieri <[email protected]>
  • Loading branch information
eryk-vieira and rsaz committed Aug 2, 2023
1 parent 6786854 commit 2aaab9e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/generate/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const createTemplate = async ({
if (schematic !== "service") {

// add to guarantee that the routing will always be the last part of the path
let routeSchema:string = "";
let routeSchema = "";

if (target.includes("/") || target.includes("\\") || target.includes("//")) {
routeSchema = path.split("/").pop();
Expand Down Expand Up @@ -106,7 +106,7 @@ export const createTemplate = async ({
}

// add to guarantee that the routing will always be the last part of the path
let routeSchema:string = "";
let routeSchema = "";

if (target.includes("/") || target.includes("\\") || target.includes("//")) {
routeSchema = path.split("/").pop();
Expand Down Expand Up @@ -156,20 +156,20 @@ export const createTemplate = async ({
}
}

let controllerPath = "";
let controllerPath = "./";
const pathCount = (path.split("/")).length;

if (path === "") {
controllerPath = `${file.slice(0, file.lastIndexOf('.'))}`;
controllerPath += `${file.slice(0, file.lastIndexOf('.'))}`;
} else if (pathCount === 1) {
controllerPath = `${path}/${file.slice(0, file.lastIndexOf('.'))}`;
controllerPath += `${path}/${file.slice(0, file.lastIndexOf('.'))}`;
} else if (pathCount === 2) {
controllerPath = `${path.split("/")[1]}/${file.slice(0, file.lastIndexOf('.'))}`;
controllerPath += `${path.split("/")[1]}/${file.slice(0, file.lastIndexOf('.'))}`;
} else {
const segments: string[] = path.split("/");
controllerPath = `${segments[segments.length-1]}/${file.slice(0, file.lastIndexOf('.'))}`;
controllerPath += `${segments[segments.length-1]}/${file.slice(0, file.lastIndexOf('.'))}`;
}

if (moduleExist) {
if (target.includes("/") || target.includes("\\") || target.includes("//")) {
await addControllerToModule(`${usecaseDir}/${modulePath}/${moduleName}.module.ts`, `${className}Controller`, controllerPath);
Expand All @@ -181,7 +181,7 @@ export const createTemplate = async ({
await addControllerToModule(`${usecaseDir}/${moduleName}/${moduleName}.module.ts`, `${className}Controller`, controllerPath);
}
}
} else {
} else {
writeTemplate({
outputPath: moduleOutPath,
template: {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/add-controller-to-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function addControllerToModule(filePath: string, controllerName: string, c
return;
}

const controllers = moduleDeclarationMatch[1].trim().split(',').map((c) => c.trim());
const controllers = moduleDeclarationMatch[1].trim().split(',').map((c) => c.trim()).filter((c) => c);

if (controllers.includes(controllerName)) {
return;
Expand All @@ -37,7 +37,7 @@ async function addControllerToModule(filePath: string, controllerName: string, c
controllers.push(controllerName);

const newControllers = controllers.join(', ');

const newModuleDeclaration = `CreateModule([${newControllers}]`;

const newFileContent = [...imports, ...notImports].join('\n').replace(moduleDeclarationRegex, newModuleDeclaration);
Expand Down

0 comments on commit 2aaab9e

Please sign in to comment.