Skip to content

Commit

Permalink
feat: add path command style to opinionated services
Browse files Browse the repository at this point in the history
  • Loading branch information
rsaz committed Mar 29, 2024
1 parent 44f17f1 commit 3dea624
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/generate/form.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { s } from "vitest/dist/reporters-cb94c88b";
import Compiler from "../utils/compiler";
import { checkPathStyle } from "./utils/command-utils";
import { nonOpinionatedProcess } from "./utils/nonopininated-cmd";
import { opinionatedProcess } from "./utils/opinionated-cmd";

Expand Down Expand Up @@ -28,15 +28,16 @@ export const createTemplate = async ({
method,
}: CreateTemplateProps) => {
const config = await Compiler.loadConfig();
const pathStyle = checkPathStyle(target);

let returnFile = "";

if (config.opinionated) {
returnFile = await opinionatedProcess(
schematic,
target,
method,
config,
pathStyle,
);
} else {
returnFile = await nonOpinionatedProcess(
Expand Down
25 changes: 25 additions & 0 deletions src/generate/utils/command-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import { verifyIfFileExists } from "../../utils/verify-file-exists";
import Compiler from "../../utils/compiler";
import { ExpressoConfig, Pattern } from "../../types";

export const enum PathStyle {
None = "none",
Single = "single",
Nested = "nested",
Sugar = "sugar",
}

/**
* File preparation
* @param schematic
Expand Down Expand Up @@ -364,3 +371,21 @@ export async function extractFirstWord(file: string) {
return anyCaseToCamelCase(firstWord);
}
}

/**
* Check if the path is a nested path, a single path or a sugar path
* @param path
* @returns the path style
*/
export const checkPathStyle = (path: string): PathStyle => {
const singleOrNestedPathRegex = /\/|\\/;
const sugarPathRegex = /^\w+-\w+$/;

if (singleOrNestedPathRegex.test(path)) {
return PathStyle.Nested;
} else if (sugarPathRegex.test(path)) {
return PathStyle.Sugar;
} else {
return PathStyle.Single;
}
};
25 changes: 16 additions & 9 deletions src/generate/utils/opinionated-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
FileOutput,
getFileNameWithoutExtension,
getHttpMethod,
PathStyle,
validateAndPrepareFile,
writeTemplate,
} from "./command-utils";
Expand All @@ -23,6 +24,7 @@ export async function opinionatedProcess(
target: string,
method: string,
expressoConfig: ExpressoConfig,
pathStyle: string,
): Promise<string> {
const f: FileOutput = await validateAndPrepareFile({
schematic,
Expand Down Expand Up @@ -69,14 +71,19 @@ export async function opinionatedProcess(
method,
expressoConfig,
});
await generateModuleService(
f.outputPath,
m.className,
m.moduleName,
m.path,
m.file,
m.folderToScaffold,
);

if (pathStyle === PathStyle.Sugar) {
await generateModuleServiceSugarPath(
f.outputPath,
m.className,
m.moduleName,
m.path,
m.file,
m.folderToScaffold,
);
} else if (pathStyle === PathStyle.Nested) {
} else if (pathStyle === PathStyle.Single) {
}

await printGenerateSuccess("controller", f.file);
await printGenerateSuccess("usecase", f.file);
Expand Down Expand Up @@ -426,7 +433,7 @@ async function generateMiddleware(
* @param moduleName - The module name
* @param path - The path
*/
async function generateModuleService(
async function generateModuleServiceSugarPath(
outputPathController: string,
className: string,
moduleName: string,
Expand Down

0 comments on commit 3dea624

Please sign in to comment.