Skip to content

Commit

Permalink
feat: add single and sugar path validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaz committed Mar 29, 2024
1 parent 3dea624 commit b5738e5
Showing 1 changed file with 131 additions and 0 deletions.
131 changes: 131 additions & 0 deletions src/generate/utils/opinionated-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export async function opinionatedProcess(
});

if (pathStyle === PathStyle.Sugar) {
console.log("Sugar");
await generateModuleServiceSugarPath(
f.outputPath,
m.className,
Expand All @@ -82,7 +83,25 @@ export async function opinionatedProcess(
m.folderToScaffold,
);
} else if (pathStyle === PathStyle.Nested) {
console.log("Nested");
await generateModuleServiceNestedPath(
f.outputPath,
m.className,
m.moduleName,
m.path,
m.file,
m.folderToScaffold,
);
} else if (pathStyle === PathStyle.Single) {
console.log("Single");
await generateModuleServiceSinglePath(
f.outputPath,
m.className,
m.moduleName,
m.path,
m.file,
m.folderToScaffold,
);
}

await printGenerateSuccess("controller", f.file);
Expand Down Expand Up @@ -490,6 +509,118 @@ async function generateModuleServiceSugarPath(
);
}

async function generateModuleServiceSinglePath(
outputPathController: string,
className: string,
moduleName: string,
path: string,
file: string,
folderToScaffold: string,
): Promise<void> {
const newModuleFile = await extractFirstWord(file);
const newModulePath = nodePath.join(folderToScaffold, path).normalize();
const newModuleName = `${newModuleFile}.module.ts`;
const newModuleOutputPath = `${newModulePath}/${newModuleName}`.replace(
"\\",
"/",
);

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

const controllerFullPath = nodePath
.join(folderToScaffold, path, "..", newModuleName)
.normalize();

if (fs.existsSync(newModuleOutputPath)) {
await addControllerToModule(
controllerFullPath,
`${className}Controller`,
controllerToModule,
);
return;
}

writeTemplate({
outputPath: newModuleOutputPath,
template: {
path: "../templates/opinionated/module-service.tpl",
data: {
className,
moduleName: anyCaseToPascalCase(moduleName),
path: controllerToModule,
},
},
});

await addModuleToContainer(
anyCaseToPascalCase(moduleName),
`${moduleName}/${file.replace(".ts", "")}`,
path,
);
}

async function generateModuleServiceNestedPath(
outputPathController: string,
className: string,
moduleName: string,
path: string,
file: string,
folderToScaffold: string,
): Promise<void> {
const newModuleFile = await extractFirstWord(file);
const newModulePath = nodePath
.join(folderToScaffold, path, "..")
.normalize();
const newModuleName = `${newModuleFile}.module.ts`;
const newModuleOutputPath = `${newModulePath}/${newModuleName}`.replace(
"\\",
"/",
);

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

const controllerFullPath = nodePath
.join(folderToScaffold, path, "..", newModuleName)
.normalize();

if (fs.existsSync(newModuleOutputPath)) {
await addControllerToModule(
controllerFullPath,
`${className}Controller`,
controllerToModule,
);
return;
}

writeTemplate({
outputPath: newModuleOutputPath,
template: {
path: "../templates/opinionated/module-service.tpl",
data: {
className,
moduleName: anyCaseToPascalCase(moduleName),
path: controllerToModule,
},
},
});

await addModuleToContainer(
anyCaseToPascalCase(moduleName),
`${moduleName}/${file.replace(".ts", "")}`,
path,
);
}

/**
* Generate a module
* @param outputPath - The output path
Expand Down

0 comments on commit b5738e5

Please sign in to comment.