Skip to content

Commit 3f29ff8

Browse files
committed
Add extractInnerWord utility and update route generators to utilize it
1 parent a760984 commit 3f29ff8

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

src/templates/next-js/route-generator.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
generateNextJsDynamicSegmentInterface,
1212
getNextJsDynamicSegments,
1313
} from "./utils/get-next-js-dynamic-segments";
14+
import { extractInnerWord } from "../../utils/extract-inner-bracket-word";
1415

1516
const successLog = (text: string) => console.log(chalk.greenBright(text));
1617
const infoLog = (text: string) => console.log(chalk.blueBright(text));
@@ -52,7 +53,7 @@ function Page(${isDynamic ? "{ params }:{ params: Promise<PageProps> }" : ""}) {
5253
}
5354
5455
export default Page;
55-
`
56+
`,
5657
);
5758
successLog(`✓ Generated route file: ${newRoutePath}`);
5859
} else {
@@ -80,7 +81,7 @@ const generateModule = ({
8081
"src",
8182
"modules",
8283
parentPath,
83-
parentPath.endsWith(lastStaticPath) ? "" : lastStaticPath // ? If route is: /user/:id/:orders/create then the complete route will be /user/create
84+
parentPath.endsWith(lastStaticPath) ? "" : lastStaticPath, // ? If route is: /user/:id/:orders/create then the complete route will be /user/create
8485
);
8586
fs.ensureDirSync(modulePath);
8687
const newModulePath = `${modulePath}/index.${fileExtension}x`;
@@ -99,7 +100,7 @@ const generateModule = ({
99100
}
100101
101102
export default ${moduleName};
102-
`
103+
`,
103104
);
104105
successLog(`✓ Generated module file: ${newModulePath}`);
105106
} else {
@@ -144,7 +145,7 @@ export const ${textToCamelCase({
144145
// });
145146
// },
146147
};
147-
`
148+
`,
148149
);
149150
successLog(`✓ Generated service file: ${newServicePath}`);
150151
} else {
@@ -191,7 +192,7 @@ const generateTypescriptType = ({
191192
})}Type {
192193
// Define your type properties here
193194
}
194-
`
195+
`,
195196
);
196197
successLog(`✓ Generated typescript type file: ${newTypePath}`);
197198
} else {
@@ -207,8 +208,15 @@ const generateTypescriptType = ({
207208
*/
208209
export const nextRouteGenerator = (route: string) => {
209210
try {
210-
const parentPath = staticParentPath(route);
211-
const lastStaticPath = lastStaticSegment(route);
211+
const parentPath = staticParentPath(route)
212+
.split("/")
213+
.map((seg) => extractInnerWord(seg))
214+
.join("/");
215+
const lastStaticPath = lastStaticSegment(route)
216+
.split("/")
217+
.map((seg) => extractInnerWord(seg))
218+
.join("/");
219+
212220
const { fileExtension } = getCalmUiJson();
213221

214222
// ---------- GENERATING ROUTE FILE ----------

src/templates/vite-react/route-generator.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { staticParentPath } from "./utils/static-parent-path";
88
import { lastStaticSegment } from "./utils/last-static-segment";
99
import { routeToTanstackPath } from "./utils/route-to-tanstack-path";
1010
import { getTanstackDynamicSegments } from "./utils/get-tanstack-dynamic-segments";
11+
import { extractInnerWord } from "../../utils/extract-inner-bracket-word";
1112

1213
const successLog = (text: string) => console.log(chalk.greenBright(text));
1314
const infoLog = (text: string) => console.log(chalk.blueBright(text));
@@ -58,7 +59,7 @@ function RouteComponent() {
5859
}
5960
return <div>Hello "${route}"!</div>
6061
}
61-
`
62+
`,
6263
);
6364
successLog(`✓ Generated route file: ${newRoutePath}`);
6465
} else {
@@ -86,7 +87,7 @@ const generateModule = ({
8687
"src",
8788
"modules",
8889
parentPath,
89-
parentPath.endsWith(lastStaticPath) ? "" : lastStaticPath // ? If route is: /user/:id/:orders/create then the complete route will be /user/create
90+
parentPath.endsWith(lastStaticPath) ? "" : lastStaticPath, // ? If route is: /user/:id/:orders/create then the complete route will be /user/create
9091
);
9192
fs.ensureDirSync(modulePath);
9293
const newModulePath = `${modulePath}/index.${fileExtension}x`;
@@ -105,7 +106,7 @@ const generateModule = ({
105106
}
106107
107108
export default ${moduleName};
108-
`
109+
`,
109110
);
110111
successLog(`✓ Generated module file: ${newModulePath}`);
111112
} else {
@@ -150,7 +151,7 @@ export const ${textToCamelCase({
150151
// });
151152
// },
152153
};
153-
`
154+
`,
154155
);
155156
successLog(`✓ Generated service file: ${newServicePath}`);
156157
} else {
@@ -197,7 +198,7 @@ const generateTypescriptType = ({
197198
})}Type {
198199
// Define your type properties here
199200
}
200-
`
201+
`,
201202
);
202203
successLog(`✓ Generated typescript type file: ${newTypePath}`);
203204
} else {
@@ -214,8 +215,15 @@ const generateTypescriptType = ({
214215
export const viteRouteGenerator = (route: string) => {
215216
try {
216217
const { fileExtension } = getCalmUiJson();
217-
const parentPath = staticParentPath(route);
218-
const lastStaticPath = lastStaticSegment(route);
218+
const parentPath = staticParentPath(route)
219+
.split("/")
220+
.map((seg) => extractInnerWord(seg))
221+
.join("/");
222+
223+
const lastStaticPath = lastStaticSegment(route)
224+
.split("/")
225+
.map((seg) => extractInnerWord(seg))
226+
.join("/");
219227

220228
// ---------- GENERATING ROUTES ----------
221229
generateRoute({
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function extractInnerWord(s: string) {
2+
if (s.startsWith("(") && s.endsWith(")")) {
3+
return s.slice(1, -1);
4+
}
5+
return s;
6+
}

0 commit comments

Comments
 (0)