Created
December 19, 2024 00:50
-
-
Save lifeiscontent/244f00ce581bd80ec5b5d6c99fddb65c to your computer and use it in GitHub Desktop.
Migration Script to Remix routes.ts
This file contains hidden or 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
import json from "../routes.json"; | |
type RouteNode = { | |
id: string; | |
path?: string; | |
file: string; | |
index?: boolean; | |
children?: RouteNode[]; | |
}; | |
function output({ id, file, children, index, path }: RouteNode, depth = 0) { | |
let out = ""; | |
if (id === "root") { | |
out += 'import { type RouteConfig } from "@remix-run/route-config";\n'; | |
out += | |
'import { remixRoutesOptionAdapter } from "@remix-run/routes-option-adapter";\n'; | |
out += "\n"; | |
out += "export default remixRoutesOptionAdapter((defineRoutes) => {\n"; | |
out += " return defineRoutes((route) => {\n"; | |
} else { | |
if (children) { | |
out += `${" ".repeat(depth * 2)}route(${JSON.stringify(path)}, ${JSON.stringify(file)}, () => {\n`; | |
} else { | |
out += `${" ".repeat(depth * 2)}route(${JSON.stringify(path ?? "")}, ${JSON.stringify(file)}${index || id ? `, ${JSON.stringify({ index, id: file.replace(/\.(ts|tsx|js|jsx)$/, "") === id ? undefined : id })}` : ""})\n`; | |
} | |
} | |
if (children) { | |
for (const child of children) { | |
out += output(child, depth + 1); | |
} | |
} | |
if (id === "root") { | |
out += " });\n"; | |
out += "}) satisfies RouteConfig;\n"; | |
} else { | |
if (children) { | |
out += `${" ".repeat(depth * 2)}});\n`; | |
} | |
} | |
return out; | |
} | |
console.log(output(json[0]!)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment