forked from software-mansion/radon-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve url experience - get routes based on file based routing
2nd point software-mansion#887
- Loading branch information
Showing
8 changed files
with
169 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
packages/vscode-extension/src/utilities/getFileBasedRoutes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import path from "path"; | ||
import fs from "fs"; | ||
import { findAppRootFolder } from "../extension"; | ||
|
||
// assuming that people may put them in the app folder | ||
const DIRS_TO_SKIP = ["components", "(components)", "utils", "hooks"]; | ||
|
||
function computeRouteIdentifier(pathname: string, params = {}) { | ||
return pathname + JSON.stringify(params); | ||
} | ||
|
||
export type Route = { | ||
name: string; | ||
pathname: string; | ||
params: Record<string, any>; | ||
id: string; | ||
}; | ||
|
||
function createRoute(pathname: string): Route { | ||
pathname = pathname.replace(/\/?\([^)]*\)/g, ""); | ||
return { | ||
id: computeRouteIdentifier(pathname), | ||
pathname, | ||
name: pathname, | ||
params: {}, | ||
}; | ||
} | ||
|
||
function handleIndexRoute(basePath: string): Route { | ||
const pathname = basePath || "/"; | ||
return createRoute(pathname); | ||
} | ||
|
||
function handleParameterizedRoute(basePath: string, route: string): Route { | ||
const pathname = `${basePath}/${route}`; | ||
return createRoute(pathname); | ||
} | ||
|
||
function handleRegularRoute(basePath: string, route: string): Route { | ||
const pathname = `${basePath}/${route}`; | ||
return createRoute(pathname); | ||
} | ||
|
||
async function getRoutes(dir: string, basePath: string = ""): Promise<Route[]> { | ||
let routes: Route[] = []; | ||
try { | ||
const files = await fs.promises.readdir(dir); | ||
|
||
for (const file of files) { | ||
const fullPath = path.join(dir, file); | ||
const stat = await fs.promises.stat(fullPath); | ||
|
||
if (stat.isDirectory()) { | ||
if (DIRS_TO_SKIP.includes(file)) { | ||
continue; | ||
} | ||
routes = routes.concat(await getRoutes(fullPath, `${basePath}/${file}`)); | ||
} else if (file.endsWith(".js") || (file.endsWith(".tsx") && !file.includes("_layout"))) { | ||
const route = file.replace(/(\.js|\.tsx)$/, ""); | ||
if (route === "index") { | ||
routes.push(handleIndexRoute(basePath)); | ||
} else if (route.startsWith("[") && route.endsWith("]")) { | ||
routes.push(handleParameterizedRoute(basePath, route)); | ||
} else { | ||
routes.push(handleRegularRoute(basePath, route)); | ||
} | ||
} | ||
} | ||
} catch (error) { | ||
console.error(`Error reading directory ${dir}:`, error); | ||
} | ||
return routes; | ||
} | ||
|
||
export async function getAppRoutes() { | ||
const appRoot = await findAppRootFolder(); | ||
if (!appRoot) { | ||
return []; | ||
} | ||
return getRoutes(path.join(appRoot, "app")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters