(routePath: string, routesFolderNesting: number)
| 32 | }; |
| 33 | |
| 34 | export const convertRoutePathToTildeFromRelative = (routePath: string, routesFolderNesting: number) => { |
| 35 | const parentPath = routePath.match(/\.\.\//g); |
| 36 | const numParentLevels = parentPath ? parentPath.length : 0; |
| 37 | const convertedPath = routePath.replace(/\.\.\//g, ""); |
| 38 | // because everything is moved to root we do not need to change this |
| 39 | if (routePath.startsWith("./")) { |
| 40 | return routePath; |
| 41 | } |
| 42 | // means its a package dep such as "@remix-run/react" |
| 43 | if (!routePath.startsWith("../") && !routePath.startsWith("~/")) { |
| 44 | return routePath; |
| 45 | } |
| 46 | // if outside of routes folder we change the levels eg ../../../server to ../server |
| 47 | if (numParentLevels > routesFolderNesting) { |
| 48 | return "../".repeat(numParentLevels - routesFolderNesting) + convertedPath; |
| 49 | } |
| 50 | // We add a ~/ to the path eg routes/something => ~/routes/something |
| 51 | return convertedPath.startsWith("~/") ? convertedPath : `~/${convertedPath}`; |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * Helps calculate the nesting difference between two paths |
no outgoing calls
no test coverage detected