-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathroute-utils.ts
More file actions
163 lines (140 loc) · 5.64 KB
/
Copy pathroute-utils.ts
File metadata and controls
163 lines (140 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import fs from "node:fs";
import path from "node:path";
import { defaultLocale, locales } from "./src/i18n/config";
import apiSidebarMetadata from "./src/content/en/api/sidebar-metadata.json" with { type: "json" };
import completeCookbook from "./public/complete-cookbook.json" with { type: "json" };
type CookbookEntry = {
slug?: string;
};
const appLocaleRoot = path.join(process.cwd(), "src", "app", "[locale]");
const contentDocsRoot = path.join(process.cwd(), "src", "content", defaultLocale, "docs");
const modelFilesRoot = path.join(process.cwd(), "src", "schema", "models", "models");
export const targetLocales = locales.filter(locale => locale !== defaultLocale);
export function normalizeRoute(route: string): string {
if (!route.startsWith("/")) route = `/${route}`;
return route.length > 1 ? route.replace(/\/+$/, "") : route;
}
function encodeStaticRoute(route: string): string {
const normalized = normalizeRoute(route);
if (normalized === "/") return normalized;
return `/${normalized
.split("/")
.filter(Boolean)
.map(segment =>
encodeURIComponent(segment).replace(/[!'()*]/g, char =>
`%${char.charCodeAt(0).toString(16).toUpperCase()}`
)
)
.join("/")}`;
}
function addRoute(routes: Set<string>, route: string): void {
const normalized = encodeStaticRoute(route);
if (normalized === `/${defaultLocale}`) return;
if (locales.some(locale => normalized === `/${locale}` || normalized.startsWith(`/${locale}/`))) {
return;
}
if (normalized.startsWith("/api/og")) return;
routes.add(normalized);
}
function walkFiles(root: string, visit: (filePath: string) => void): void {
let entries: fs.Dirent[];
try {
entries = fs.readdirSync(root, { withFileTypes: true });
} catch {
return;
}
for (const entry of entries) {
const fullPath = path.join(root, entry.name);
if (entry.isDirectory()) {
if (entry.name.startsWith("_")) continue;
walkFiles(fullPath, visit);
continue;
}
visit(fullPath);
}
}
function routeFromAppPage(filePath: string): string | null {
const relative = path.relative(appLocaleRoot, filePath);
if (!relative.endsWith("page.tsx") && !relative.endsWith("page.mdx")) return null;
const segments = relative.split(path.sep).slice(0, -1).filter(segment => {
if (segment === "[locale]") return false;
if (segment.startsWith("(") && segment.endsWith(")")) return false;
return true;
});
if (segments.some(segment => segment.startsWith("[") && segment.endsWith("]"))) {
return null;
}
return segments.length === 0 ? "/" : `/${segments.join("/")}`;
}
function collectAppRoutes(): string[] {
const routes = new Set<string>();
walkFiles(appLocaleRoot, filePath => {
const route = routeFromAppPage(filePath);
if (route) addRoute(routes, route);
});
return Array.from(routes);
}
function collectDocsContentRoutes(): string[] {
const routes = new Set<string>();
const walk = (dir: string, parts: string[]): void => {
let entries: fs.Dirent[];
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch {
return;
}
const hasPage = entries.some(entry => entry.isFile() && (entry.name === "page.mdx" || entry.name === "page.md"));
if (hasPage && parts.length > 0) addRoute(routes, `/${parts.join("/")}`);
for (const entry of entries) {
if (!entry.isDirectory()) continue;
if (entry.name.startsWith("_")) continue;
if (entry.name.startsWith("[") && entry.name.endsWith("]")) continue;
walk(path.join(dir, entry.name), [...parts, entry.name]);
}
};
walk(contentDocsRoot, []);
return Array.from(routes);
}
function collectApiEndpointRoutes(): string[] {
const endpointPrefix = "endpoint/";
return apiSidebarMetadata
.map(category => category.slug)
.filter((slug): slug is string => typeof slug === "string" && slug.startsWith(endpointPrefix))
.map(slug => `/api/endpoint/${slug.slice(endpointPrefix.length)}`);
}
function collectModelCardRoutes(): string[] {
const routes = new Set<string>();
walkFiles(modelFilesRoot, filePath => {
if (!filePath.endsWith(".ts") || filePath.endsWith(`${path.sep}index.ts`)) return;
const source = fs.readFileSync(filePath, "utf8");
const slugMatch = source.match(/\bslug:\s*['"]([^'"]+)['"]/);
const aliasMatch = source.match(/\bslugAliases:\s*\[([^\]]*)\]/);
const apiNamesMatch = source.match(/\bapiNames:\s*\[([^\]]*)\]/);
const aliases = aliasMatch?.[1]?.match(/['"]([^'"]+)['"]/g)
?.map(alias => alias.slice(1, -1)) ?? [];
const latestApiNames = apiNamesMatch?.[1]?.match(/['"]([^'"]+-latest)['"]/g)
?.map(alias => alias.slice(1, -1)) ?? [];
for (const slug of [slugMatch?.[1], ...aliases, ...latestApiNames]) {
if (!slug) continue;
addRoute(routes, `/models/${slug}`);
addRoute(routes, `/models/model-cards/${slug}`);
}
});
return Array.from(routes);
}
function collectCookbookRoutes(): string[] {
return (completeCookbook as CookbookEntry[])
.map(cookbook => cookbook.slug)
.filter((slug): slug is string => typeof slug === "string" && slug.length > 0)
.map(slug => `/resources/cookbooks/${slug}`);
}
export function collectDefaultLocaleRoutes(): string[] {
const routes = new Set<string>();
addRoute(routes, "/");
for (const route of collectAppRoutes()) addRoute(routes, route);
for (const route of collectDocsContentRoutes()) addRoute(routes, route);
for (const route of collectApiEndpointRoutes()) addRoute(routes, route);
for (const route of collectModelCardRoutes()) addRoute(routes, route);
for (const route of collectCookbookRoutes()) addRoute(routes, route);
return Array.from(routes).sort((a, b) => a.localeCompare(b));
}