106 lines
No EOL
3 KiB
TypeScript
106 lines
No EOL
3 KiB
TypeScript
import { join, relative, ensureFileSync, wait, S3 } from "../deps.ts";
|
|
import Pika from "../utils/instances/pika.ts";
|
|
|
|
const ROUTES_FILE = "routes.ts";
|
|
|
|
const S3inner = wait({
|
|
text: `Generating ${ROUTES_FILE}...`,
|
|
spinner: "aesthetic"
|
|
})
|
|
|
|
interface PageRoute {
|
|
name: string;
|
|
path: string;
|
|
route: string;
|
|
}
|
|
|
|
async function readDir(dir: URL): Promise<PageRoute[]> {
|
|
// Sort the Entries
|
|
const entries = [...Deno.readDirSync(dir)]
|
|
entries.sort((a, b) => a.name.toUpperCase() < b.name.toUpperCase() ? -1 : 1);
|
|
const routes: PageRoute[] = []
|
|
|
|
for (const file of entries) {
|
|
const path = join(dir.pathname, file.name)
|
|
if (file.isDirectory) {
|
|
const sub = await readDir(new URL(path, import.meta.url))
|
|
routes.push(...sub)
|
|
continue;
|
|
}
|
|
|
|
const relativePath = relative(
|
|
new URL(import.meta.url + "/..").pathname,
|
|
path
|
|
)
|
|
|
|
const route = "/" + relativePath.replaceAll(
|
|
/\[(.*?)\]/g,
|
|
"{$1}",
|
|
).replace(/(\.ts)|(\.js)|(\/index(\.ts|\.js))/, "")
|
|
.replace(/[\\/]+/g, '/')
|
|
|
|
const filePath = `../${relativePath}`;
|
|
const replaced = route.replace("/../src/routes", "")
|
|
let finalRoute = replaced;
|
|
if (replaced.includes('index') && !replaced.startsWith("/index"))
|
|
finalRoute = replaced.replace("/index", "")
|
|
else
|
|
finalRoute = replaced.replace('/index', '/')
|
|
|
|
S3inner.text = `Added ${finalRoute}`
|
|
|
|
routes.push({
|
|
name: Pika.gen("route"),
|
|
route: finalRoute,
|
|
path: filePath,
|
|
});
|
|
}
|
|
|
|
return routes;
|
|
}
|
|
|
|
|
|
function createFile(routes: PageRoute[]): string {
|
|
let string =
|
|
`// This File is generated by scripts/routes.ts
|
|
// Do not edit this File manually
|
|
// run \`vr routes\` to regenerate this File
|
|
|
|
import PageRoute from "../../utils/types/PageRoute.ts"
|
|
${routes.map((route) => (
|
|
`import ${route.name} from "${route.path.replace(/[\\/]+/g, '/')}";`).replace('./../src', ".")).join("\n")}
|
|
|
|
const routes: PageRoute[] = [\n ${routes.map((route) => (`{ route: "${route.route}", method: ${route.name}.method, handler: ${route.name}.handler, domains: ${route.name}.domains || undefined }`)).join(",\n ")}
|
|
]
|
|
|
|
|
|
export default routes
|
|
`
|
|
|
|
return string
|
|
}
|
|
|
|
|
|
async function write(folderwithroutes: string | URL, file: string | URL) {
|
|
if (typeof folderwithroutes === "string") folderwithroutes = new URL(folderwithroutes, import.meta.url);
|
|
// console.log(folderwithroutes)
|
|
const routes = await readDir(folderwithroutes)
|
|
const content = createFile(routes)
|
|
|
|
await Deno.writeTextFile(`./src/generated/${file}`, content)
|
|
}
|
|
|
|
|
|
if (import.meta.main) {
|
|
|
|
try {
|
|
ensureFileSync(`./src/generated/${ROUTES_FILE}`)
|
|
await write('../src/routes', ROUTES_FILE)
|
|
} catch (error) {
|
|
console.error(error)
|
|
S3inner.fail()
|
|
Deno.exit(1)
|
|
}
|
|
S3inner.succeed("Routes generated.")
|
|
Deno.exit(0)
|
|
} |