|
1
|
import { promises as fs } from "fs" |
|
2
|
import path from "path" |
|
3
|
import { fileURLToPath } from "url" |
|
4
|
import { parse as parseYaml } from "yaml" |
|
5
|
import { |
|
6
|
findAllPageHeadings, |
|
7
|
getApiRefIntroSlug, |
|
8
|
getApiRefPath, |
|
9
|
getApiRefTagOperationSlugs, |
|
10
|
getApiRefTagSlug, |
|
11
|
getSectionId, |
|
12
|
} from "docs-utils" |
|
13
|
import pluralize from "pluralize" |
|
14
|
|
|
15
|
const { singular } = pluralize |
|
16
|
|
|
17
|
const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
|
18
|
const appDir = path.join(__dirname, "..") |
|
19
|
|
|
20
|
const HTTP_METHODS = [ |
|
21
|
"get", |
|
22
|
"post", |
|
23
|
"put", |
|
24
|
"patch", |
|
25
|
"delete", |
|
26
|
"head", |
|
27
|
"options", |
|
28
|
] |
|
29
|
|
|
30
|
const AREAS = ["admin", "store"] |
|
31
|
|
|
32
|
const getMethodOrder = (method) => { |
|
33
|
switch (method) { |
|
34
|
case "get": |
|
35
|
return 1 |
|
36
|
case "post": |
|
37
|
return 2 |
|
38
|
case "delete": |
|
39
|
return 3 |
|
40
|
default: |
|
41
|
return 4 |
|
42
|
} |
|
43
|
} |
|
44
|
|
|
45
|
// Mirror of the api-reference `compareOperations` display sort so slug |
|
46
|
// de-duplication numbering (`-2`, `-3`, ...) matches the rendered order. |
|
47
|
const compareOperations = (a, b) => { |
|
48
|
const orderDiff = getMethodOrder(a.method) - getMethodOrder(b.method) |
|
49
|
if (orderDiff !== 0) { |
|
50
|
return orderDiff |
|
51
|
} |
|
52
|
|
|
53
|
return (a.summary || "").localeCompare(b.summary || "") |
|
54
|
} |
|
55
|
|
|
56
|
export async function generateSpecsPathsManifest() { |
|
57
|
const tagIndex = {} |
|
58
|
// operationsByTag[area][tagName] = [{ operationId, method, summary, xSidebarSummary }] |
|
59
|
const operationsByTag = {} |
|
60
|
|
|
61
|
for (const area of AREAS) { |
|
62
|
const pathsDir = path.join(appDir, "specs", area, "paths") |
|
63
|
let files = [] |
|
64
|
try { |
|
65
|
const allFiles = await fs.readdir(pathsDir) |
|
66
|
// Sort the file list so the generated output is deterministic |
|
67
|
// regardless of the OS/filesystem order that `readdir` returns. |
|
68
|
files = allFiles.filter((f) => f.endsWith(".yaml")).sort() |
|
69
|
} catch { |
|
70
|
// paths dir doesn't exist yet |
|
71
|
} |
|
72
|
tagIndex[area] = {} |
|
73
|
operationsByTag[area] = {} |
|
74
|
|
|
75
|
// Read and parse all files in parallel, but keep the results in the |
|
76
|
// sorted `files` order (`Promise.all` preserves input order) so the |
|
77
|
// arrays built below don't depend on I/O resolution timing. |
|
78
|
const parsedFiles = await Promise.all( |
|
79
|
files.map(async (file) => { |
|
80
|
const filePath = path.join(pathsDir, file) |
|
81
|
try { |
|
82
|
const content = await fs.readFile(filePath, "utf-8") |
|
83
|
return { file, parsed: parseYaml(content) } |
|
84
|
} catch { |
|
85
|
// skip unreadable files |
|
86
|
return { file, parsed: null } |
|
87
|
} |
|
88
|
}) |
|
89
|
) |
|
90
|
|
|
91
|
for (const { file, parsed } of parsedFiles) { |
|
92
|
for (const method of HTTP_METHODS) { |
|
93
|
const operation = parsed?.[method] |
|
94
|
if (!operation?.tags) { |
|
95
|
continue |
|
96
|
} |
|
97
|
for (const tag of operation.tags) { |
|
98
|
const sectionId = getSectionId([tag]) |
|
99
|
if (!tagIndex[area][sectionId]) { |
|
100
|
tagIndex[area][sectionId] = [] |
|
101
|
} |
|
102
|
if (!tagIndex[area][sectionId].includes(file)) { |
|
103
|
tagIndex[area][sectionId].push(file) |
|
104
|
} |
|
105
|
if (!operationsByTag[area][tag]) { |
|
106
|
operationsByTag[area][tag] = [] |
|
107
|
} |
|
108
|
if (operation.operationId) { |
|
109
|
operationsByTag[area][tag].push({ |
|
110
|
operationId: operation.operationId, |
|
111
|
method, |
|
112
|
summary: operation.summary, |
|
113
|
"x-sidebar-summary": operation["x-sidebar-summary"], |
|
114
|
}) |
|
115
|
} |
|
116
|
} |
|
117
|
} |
|
118
|
} |
|
119
|
} |
|
120
|
|
|
121
|
// Read ordered tags (with x-associatedSchema) from each area's base spec. |
|
122
|
const orderedTagsByArea = {} |
|
123
|
for (const area of AREAS) { |
|
124
|
const specPath = path.join(appDir, "specs", area, "openapi.yaml") |
|
125
|
try { |
|
126
|
const specContent = await fs.readFile(specPath, "utf-8") |
|
127
|
const spec = parseYaml(specContent) |
|
128
|
orderedTagsByArea[area] = spec.tags ?? [] |
|
129
|
} catch { |
|
130
|
orderedTagsByArea[area] = Object.keys(operationsByTag[area]).map( |
|
131
|
(name) => ({ name }) |
|
132
|
) |
|
133
|
} |
|
134
|
} |
|
135
|
|
|
136
|
// Build sitemap data: ordered by openapi.yaml tags, with precomputed section IDs |
|
137
|
const sitemapData = {} |
|
138
|
for (const area of AREAS) { |
|
139
|
sitemapData[area] = orderedTagsByArea[area].map((tag) => ({ |
|
140
|
tagSectionId: getSectionId([tag.name]), |
|
141
|
operationSectionIds: (operationsByTag[area][tag.name] ?? []).map((op) => |
|
142
|
getSectionId([tag.name, op.operationId]) |
|
143
|
), |
|
144
|
})) |
|
145
|
} |
|
146
|
|
|
147
|
// Parse intro-section headings (level-2) from each area's markdown intro, and |
|
148
|
// capture the raw MDX so it can be bundled (workerd has no filesystem, so the |
|
149
|
// `md-content` route reads the intro from the generated module, not `fs`). |
|
150
|
const apiRefIntroSections = {} |
|
151
|
const apiRefIntroContent = {} |
|
152
|
for (const area of AREAS) { |
|
153
|
let headings = [] |
|
154
|
let markdownContent = "" |
|
155
|
try { |
|
156
|
markdownContent = await fs.readFile( |
|
157
|
path.join(appDir, "markdown", `${area}.mdx`), |
|
158
|
"utf-8" |
|
159
|
) |
|
160
|
headings = findAllPageHeadings({ content: markdownContent, level: 2 }) |
|
161
|
} catch { |
|
162
|
// markdown file missing |
|
163
|
} |
|
164
|
|
|
165
|
apiRefIntroContent[area] = markdownContent |
|
166
|
apiRefIntroSections[area] = headings.map((heading) => ({ |
|
167
|
slug: getApiRefIntroSlug(heading), |
|
168
|
title: heading, |
|
169
|
})) |
|
170
|
} |
|
171
|
|
|
172
|
// Build the path map + old-hash -> new-path redirect map. |
|
173
|
const apiRefPaths = {} |
|
174
|
const apiRefRedirects = {} |
|
175
|
for (const area of AREAS) { |
|
176
|
const areaPaths = { intro: {}, tags: {} } |
|
177
|
const areaRedirects = {} |
|
178
|
|
|
179
|
// intro sections |
|
180
|
for (const { slug } of apiRefIntroSections[area]) { |
|
181
|
const introPath = getApiRefPath({ area, section: slug }) |
|
182
|
areaPaths.intro[slug] = introPath |
|
183
|
areaRedirects[slug] = introPath |
|
184
|
} |
|
185
|
|
|
186
|
// tags + operations + schema |
|
187
|
for (const tag of orderedTagsByArea[area]) { |
|
188
|
const tagName = tag.name |
|
189
|
const tagSlug = getApiRefTagSlug(tagName) |
|
190
|
const tagPath = getApiRefPath({ area, section: tagSlug }) |
|
191
|
|
|
192
|
const operations = (operationsByTag[area][tagName] ?? []) |
|
193
|
.slice() |
|
194
|
.sort(compareOperations) |
|
195
|
const slugMap = getApiRefTagOperationSlugs(operations) |
|
196
|
|
|
197
|
const operationsOutput = {} |
|
198
|
for (const op of operations) { |
|
199
|
const slug = slugMap.get(op.operationId) |
|
200
|
const operationPath = getApiRefPath({ |
|
201
|
area, |
|
202
|
section: tagSlug, |
|
203
|
operationSlug: slug, |
|
204
|
}) |
|
205
|
const oldHash = getSectionId([tagName, op.operationId]) |
|
206
|
operationsOutput[op.operationId] = { |
|
207
|
slug, |
|
208
|
path: operationPath, |
|
209
|
oldHash, |
|
210
|
title: op["x-sidebar-summary"] || op.summary || op.operationId, |
|
211
|
method: op.method, |
|
212
|
} |
|
213
|
areaRedirects[oldHash] = operationPath |
|
214
|
} |
|
215
|
|
|
216
|
let schemaPath = null |
|
217
|
if (tag["x-associatedSchema"]) { |
|
218
|
schemaPath = getApiRefPath({ |
|
219
|
area, |
|
220
|
section: tagSlug, |
|
221
|
operationSlug: "schema", |
|
222
|
}) |
|
223
|
const formattedName = singular(tagName).replaceAll(" ", "") |
|
224
|
const schemaOldHash = getSectionId([tagName, formattedName, "schema"]) |
|
225
|
areaRedirects[schemaOldHash] = schemaPath |
|
226
|
} |
|
227
|
|
|
228
|
areaPaths.tags[tagSlug] = { |
|
229
|
name: tagName, |
|
230
|
path: tagPath, |
|
231
|
schemaPath, |
|
232
|
operations: operationsOutput, |
|
233
|
} |
|
234
|
areaRedirects[tagSlug] = tagPath |
|
235
|
} |
|
236
|
|
|
237
|
apiRefPaths[area] = areaPaths |
|
238
|
apiRefRedirects[area] = areaRedirects |
|
239
|
} |
|
240
|
|
|
241
|
// Guard: intro-section slugs and tag slugs share the /[area]/[section] |
|
242
|
// namespace and must not collide. |
|
243
|
for (const area of AREAS) { |
|
244
|
const tagSlugs = new Set(Object.keys(apiRefPaths[area].tags)) |
|
245
|
for (const { slug } of apiRefIntroSections[area]) { |
|
246
|
if (tagSlugs.has(slug)) { |
|
247
|
console.warn( |
|
248
|
`[api-ref-paths] Slug collision in "${area}": intro section "${slug}" also matches a tag slug.` |
|
249
|
) |
|
250
|
} |
|
251
|
} |
|
252
|
} |
|
253
|
|
|
254
|
const generatedDir = path.join(appDir, "generated") |
|
255
|
await fs.mkdir(generatedDir, { recursive: true }) |
|
256
|
|
|
257
|
await fs.writeFile( |
|
258
|
path.join(generatedDir, "specs-tag-index.mjs"), |
|
259
|
`export const specsTagIndex = ${JSON.stringify(tagIndex, null, 2)}\n` |
|
260
|
) |
|
261
|
|
|
262
|
await fs.writeFile( |
|
263
|
path.join(generatedDir, "specs-sitemap-data.mjs"), |
|
264
|
`export const specsSitemapData = ${JSON.stringify(sitemapData, null, 2)}\n` |
|
265
|
) |
|
266
|
|
|
267
|
await fs.writeFile( |
|
268
|
path.join(generatedDir, "intro-content.mjs"), |
|
269
|
`export const apiRefIntroContent = ${JSON.stringify( |
|
270
|
apiRefIntroContent, |
|
271
|
null, |
|
272
|
2 |
|
273
|
)}\n` |
|
274
|
) |
|
275
|
|
|
276
|
await fs.writeFile( |
|
277
|
path.join(generatedDir, "api-ref-paths.mjs"), |
|
278
|
`export const apiRefIntroSections = ${JSON.stringify( |
|
279
|
apiRefIntroSections, |
|
280
|
null, |
|
281
|
2 |
|
282
|
)}\n\nexport const apiRefPaths = ${JSON.stringify( |
|
283
|
apiRefPaths, |
|
284
|
null, |
|
285
|
2 |
|
286
|
)}\n\nexport const apiRefRedirects = ${JSON.stringify( |
|
287
|
apiRefRedirects, |
|
288
|
null, |
|
289
|
2 |
|
290
|
)}\n` |
|
291
|
) |
|
292
|
} |