Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(router-cli): Fixes on Windows and Improvements #717

Merged
merged 4 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ ts-perf

.netlify

nx-cloud.env
nx-cloud.env
.vscode
2 changes: 1 addition & 1 deletion examples/react/file-based-routes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "concurrently 'tsr watch' 'vite --port=3000'",
"dev": "concurrently \"tsr watch\" \"vite --port=3000\"",
"build": "tsr generate && vite build",
"serve": "vite preview",
"start": "vite"
Expand Down
2 changes: 1 addition & 1 deletion examples/react/file-based-routes/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { route as rootRoute } from "./routes/__root"
import { route as PostsRoute } from "./routes/posts"
import { route as LayoutRoute } from "./routes/_layout"
import { route as IndexRoute } from "./routes/index"
import { route as IndexRoute } from "./routes"
import { route as PostsPostIdRoute } from "./routes/posts.$postId"
import { route as LayoutLayoutBRoute } from "./routes/_layout/layout-b"
import { route as LayoutLayoutARoute } from "./routes/_layout/layout-a"
Expand Down
64 changes: 44 additions & 20 deletions packages/router-cli/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ async function getRouteNodes(config: Config) {
} else {
const filePath = path.join(dir, fileName)
const filePathNoExt = removeExt(filePath)
let routePath = cleanPath(`/${filePathNoExt.split('.').join('/')}`)
let routePath = replaceBackslash(cleanPath(`/${filePathNoExt.split('.').join('/')}`)) ?? ''
const variableName = fileToVariable(routePath)

// Remove the index from the route path and
// if the route path is empty, use `/'
if (routePath.endsWith('/index')) {
routePath = routePath.replace(/\/index$/, '/')
} else if (routePath === 'index') {
if (routePath === 'index') {
routePath = '/'
} else if (routePath.endsWith('/index')) {
routePath = routePath.replace(/\/index$/, '/')
}

routeNodes.push({
Expand Down Expand Up @@ -129,14 +129,16 @@ export async function generator(config: Config) {
// Loop over the flat list of routeNodes and
// build up a tree based on the routeNodes' routePath
routeNodes.forEach((node) => {
routeNodes.forEach((existingNode) => {
if (
node.routePath?.startsWith(`${existingNode?.routePath ?? ''}/`)
// node.routePath.length > existingNode.routePath!.length
) {
node.parent = existingNode
}
})
// routeNodes.forEach((existingNode) => {
// if (
// node.routePath?.startsWith(`${existingNode?.routePath ?? ''}/`)
// // node.routePath.length > existingNode.routePath!.length
// ) {
// node.parent = existingNode
// }
// })
const parentRoute = hasParentRoute(routeNodes, node.routePath)
if (parentRoute) node.parent = parentRoute

node.path = node.parent
? node.routePath?.replace(node.parent.routePath!, '') || '/'
Expand Down Expand Up @@ -198,22 +200,21 @@ export async function generator(config: Config) {
const routeConfigChildrenText = await buildRouteConfig(routeTree)

const routeImports = [
`import { route as rootRoute } from './${path.relative(
`import { route as rootRoute } from './${sanitize(path.relative(
path.dirname(config.generatedRouteTree),
path.resolve(config.routesDirectory, rootPathId),
)}'`,
path.resolve(config.routesDirectory, rootPathId)))}'`,
...multiSortBy(routeNodes, [
(d) => (d.routePath?.includes(`/${rootPathId}`) ? -1 : 1),
(d) => d.routePath?.split('/').length,
(d) => (d.routePath?.endsWith("index'") ? -1 : 1),
(d) => d,
]).map((node) => {
return `import { route as ${node.variableName}Route } from './${removeExt(
return `import { route as ${node.variableName}Route } from './${sanitize(removeExt(
path.relative(
path.dirname(config.generatedRouteTree),
path.resolve(config.routesDirectory, node.filePath),
),
)}'`
))}'`
}),
].join('\n')

Expand All @@ -236,8 +237,7 @@ export async function generator(config: Config) {
routeNode.isNonPath
? `id: '${routeNode.cleanedPath}'`
: `path: '${routeNode.cleanedPath}'`,
`getParentRoute: () => ${
routeNode.parent?.variableName ?? 'root'
`getParentRoute: () => ${routeNode.parent?.variableName ?? 'root'
}Route`,
// `\n// ${JSON.stringify(
// {
Expand Down Expand Up @@ -350,6 +350,30 @@ function capitalize(s: string) {
return s.charAt(0).toUpperCase() + s.slice(1)
}

function sanitize(s?: string) {
return replaceBackslash(s?.replace(/\\index/gi, ''))
}

function removeUnderscores(s?: string) {
return s?.replace(/(^_|_$)/, '').replace(/(\/_|_\/)/, '/')
return s?.replace(/(^_|_$)/, '').replace(/(\/_|_\/)/, '/');
}

function replaceBackslash(s?: string) {
return s?.replace(/\\/gi, '/')
}

function hasParentRoute(routes: RouteNode[], routeToCheck: string | undefined): RouteNode | null {
if (!routeToCheck || routeToCheck === "/") {
return null;
}
for (const route of routes) {
if (route.routePath === '/') continue;
if (routeToCheck.startsWith(`${route.routePath}/`) && route.routePath !== routeToCheck) {
return route;
}
}
const segments = routeToCheck.split("/");
segments.pop(); // Remove the last segment
const parentRoute = segments.join("/");
return hasParentRoute(routes, parentRoute);
}
Loading