one
Version:
One is a React framework for web, iOS, and Android apps.
39 lines (36 loc) • 1.51 kB
JavaScript
import micromatch from "micromatch";
import path from "node:path";
import { globDir } from "./globDir.mjs";
import { isPathInsideDirectory, isRouteFilePath } from "./routeFileWatch.mjs";
function getRoutePaths(routerRoot, ignoredRouteFiles) {
const routePaths = globDir(routerRoot).filter((routePath) => !routePath.endsWith(".d.ts"));
if (!ignoredRouteFiles?.length) return routePaths;
return micromatch.not(routePaths, ignoredRouteFiles, { matchBase: true });
}
function createRouteIndex({ routerRoot, ignoredRouteFiles }) {
const absoluteRouterRoot = path.resolve(routerRoot);
const routePaths = new Set(getRoutePaths(absoluteRouterRoot, ignoredRouteFiles));
return {
getPaths() {
return [...routePaths].sort();
},
update(event, filePath) {
if (event !== "add" && event !== "delete" && event !== "unlink") return false;
if (!isPathInsideDirectory(filePath, absoluteRouterRoot)) return false;
if (!isRouteFilePath(filePath)) return false;
const relativePath = path.relative(absoluteRouterRoot, path.resolve(filePath)).replaceAll("\\", "/");
const routePath = `./${relativePath}`;
if (ignoredRouteFiles?.length && micromatch.isMatch(routePath, ignoredRouteFiles, { matchBase: true })) {
return false;
}
if (event === "add") {
const size = routePaths.size;
routePaths.add(routePath);
return routePaths.size !== size;
}
return routePaths.delete(routePath);
}
};
}
export { createRouteIndex, getRoutePaths };
//# sourceMappingURL=routeIndex.mjs.map