winterspec
Version:
Write Winter-CG compatible routes with filesystem routing and tons of features
31 lines (30 loc) • 919 B
JavaScript
import { getMatchingFilePaths } from "make-vfs";
export const createRoutePathMapFromDirectory = async (directoryPath) => {
const filePaths = await getMatchingFilePaths({
dirPath: directoryPath,
extensions: ["ts", "tsx"],
});
const routes = {};
for (let path of filePaths) {
let routeWithSlash = path;
if (!path.startsWith("/")) {
routeWithSlash = `/${path}`;
}
if (path.endsWith("index.ts") || path.endsWith("index.tsx")) {
routes[`${routeWithSlash.replace(/\/index\.tsx*$/g, "")}`] = {
relativePath: path,
};
}
else {
routes[`${routeWithSlash.replace(/\.tsx*$/g, "")}`] = {
relativePath: path,
};
}
}
// Fix root route
if (routes[""]) {
routes["/"] = routes[""];
delete routes[""];
}
return routes;
};