@writedocs/generator 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/astro.config.mjs +12 -1
- package/package.json +1 -1
- package/src/cli/dev.js +268 -261
- package/src/cli/generate-api-pages.js +121 -36
- package/src/content.config.ts +203 -189
- package/src/lib/cli-report.js +22 -20
- package/src/lib/content-store-reload-integration.js +101 -0
- package/src/lib/icon-data-dev.js +53 -0
- package/src/lib/moved-api-pages.js +49 -54
- package/src/lib/openapi-dev-status.js +80 -0
- package/src/lib/openapi-reload-integration.js +193 -0
- package/src/pages/[...slug].astro +4 -0
|
@@ -9,7 +9,8 @@ import { projectIgnore } from '../lib/project-ignore.js';
|
|
|
9
9
|
import { readConfigText } from '../lib/config-file.js';
|
|
10
10
|
import { HTTP_METHODS, collectOpenApiGroups } from '../lib/openapi-spec.js';
|
|
11
11
|
import { operationMetaDescription } from '../lib/meta-description.js';
|
|
12
|
-
import {
|
|
12
|
+
import { movedApiPagesFile, untaggedRedirects } from '../lib/moved-api-pages.js';
|
|
13
|
+
import { canonicalPath } from '../lib/canonical-path.js';
|
|
13
14
|
|
|
14
15
|
|
|
15
16
|
/** Canonical "METHOD /path" key used everywhere an operation needs to be
|
|
@@ -204,19 +205,26 @@ function operationJson(operation) {
|
|
|
204
205
|
return JSON.stringify(cut(operation), null, 2);
|
|
205
206
|
}
|
|
206
207
|
|
|
207
|
-
|
|
208
|
-
|
|
208
|
+
/** Parses and dereferences the spec at `specPath`, adding every local file it
|
|
209
|
+
* read - the spec and each file a `$ref` pointed into - to `sources`, which
|
|
210
|
+
* `writedocs dev` watches (lib/openapi-reload-integration.js). */
|
|
211
|
+
async function dereferenceSpec(specPath, sources) {
|
|
212
|
+
sources.add(canonicalPath(specPath));
|
|
213
|
+
const parser = new SwaggerParser();
|
|
214
|
+
const spec = await parser.dereference(specPath);
|
|
215
|
+
for (const file of parser.$refs.paths('file')) sources.add(canonicalPath(path.resolve(file)));
|
|
216
|
+
return spec;
|
|
209
217
|
}
|
|
210
218
|
|
|
211
|
-
/**
|
|
212
|
-
* navigation node: dereferences its spec,
|
|
213
|
-
* operation (unless a hand-written override already claims it)
|
|
214
|
-
*
|
|
219
|
+
/** Works out every page for one `{ group, openapi: { src, path } }`
|
|
220
|
+
* navigation node: dereferences its spec, and adds to `files` a stub page
|
|
221
|
+
* per operation (unless a hand-written override already claims it) and
|
|
222
|
+
* this spec's own manifest + resolved operation JSON under
|
|
215
223
|
* writedocsTempDir()'s openapi/<path>/ directory - namespaced by `path` so multiple specs
|
|
216
224
|
* in the same writedocs.json never collide with each other on disk, exactly
|
|
217
225
|
* as they won't collide in the URL space either (both keyed off the
|
|
218
|
-
* same `path` value). */
|
|
219
|
-
async function generateApiPagesForGroup({ contentDir, generatedDocsDir, group, overrides, summary, moves }) {
|
|
226
|
+
* same `path` value). Nothing is written here - see writeGeneratedFiles(). */
|
|
227
|
+
async function generateApiPagesForGroup({ contentDir, generatedDocsDir, group, overrides, summary, moves, files, sources }) {
|
|
220
228
|
const pathPrefix = normalizePathPrefix(group.openapi.path);
|
|
221
229
|
const specPath = path.resolve(contentDir, group.openapi.src);
|
|
222
230
|
if (!fs.existsSync(specPath)) {
|
|
@@ -226,11 +234,10 @@ async function generateApiPagesForGroup({ contentDir, generatedDocsDir, group, o
|
|
|
226
234
|
);
|
|
227
235
|
}
|
|
228
236
|
|
|
229
|
-
const spec = await
|
|
237
|
+
const spec = await dereferenceSpec(specPath, sources);
|
|
230
238
|
const operations = buildOperations(spec);
|
|
231
239
|
|
|
232
240
|
const openapiOutDir = path.join(writedocsTempDir(contentDir), 'openapi', pathPrefix);
|
|
233
|
-
fs.mkdirSync(path.join(openapiOutDir, 'operations'), { recursive: true });
|
|
234
241
|
|
|
235
242
|
const manifest = [];
|
|
236
243
|
const untagged = [];
|
|
@@ -252,7 +259,6 @@ async function generateApiPagesForGroup({ contentDir, generatedDocsDir, group, o
|
|
|
252
259
|
// exactly this value regardless of whatever id Astro's own
|
|
253
260
|
// default (path-derived) computation would otherwise have picked.
|
|
254
261
|
const filePath = path.join(generatedDocsDir, `${slug}.mdx`);
|
|
255
|
-
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
256
262
|
const frontmatter = [
|
|
257
263
|
'---',
|
|
258
264
|
`title: ${JSON.stringify(title)}`,
|
|
@@ -270,7 +276,7 @@ async function generateApiPagesForGroup({ contentDir, generatedDocsDir, group, o
|
|
|
270
276
|
]
|
|
271
277
|
.filter((line) => line !== null)
|
|
272
278
|
.join('\n');
|
|
273
|
-
|
|
279
|
+
files.set(filePath, frontmatter);
|
|
274
280
|
if (!operation.tags.length) untagged.push({ oldSlug: legacyUntaggedSlugFor(pathPrefix, operation.method, operation.path), newSlug: slug });
|
|
275
281
|
}
|
|
276
282
|
moves.slugs.add(slug);
|
|
@@ -278,10 +284,10 @@ async function generateApiPagesForGroup({ contentDir, generatedDocsDir, group, o
|
|
|
278
284
|
manifest.push({ slug, method: operation.method, path: operation.path, tags: operation.tags, title, generated });
|
|
279
285
|
|
|
280
286
|
const opFile = path.join(openapiOutDir, 'operations', operationFileName(operation.method, operation.path));
|
|
281
|
-
|
|
287
|
+
files.set(opFile, operationJson(operation));
|
|
282
288
|
}
|
|
283
289
|
|
|
284
|
-
|
|
290
|
+
files.set(path.join(openapiOutDir, 'manifest.json'), JSON.stringify(manifest, null, 2));
|
|
285
291
|
if (untagged.length) moves.untagged.push({ pathPrefix, moved: untagged });
|
|
286
292
|
summary.groups.push({ group: group.group, src: group.openapi.src, operations: operations.length });
|
|
287
293
|
}
|
|
@@ -316,29 +322,48 @@ async function generateApiPagesForGroup({ contentDir, generatedDocsDir, group, o
|
|
|
316
322
|
* writedocs.json's navigation has no openapi groups at all - most sites don't
|
|
317
323
|
* have one.
|
|
318
324
|
*
|
|
325
|
+
* Every spec is parsed, and every file worked out, before anything is
|
|
326
|
+
* written: a spec that doesn't parse throws with the previous output still
|
|
327
|
+
* in place - `writedocs dev` keeps serving the last version that worked
|
|
328
|
+
* while the spec is mid-edit (lib/openapi-reload-integration.js). Then the
|
|
329
|
+
* two folders are brought to exactly that output, writing only the files
|
|
330
|
+
* that changed (writeGeneratedFiles()) - so a dev regeneration after a
|
|
331
|
+
* one-line edit to the spec touches one page, not all of them. The result
|
|
332
|
+
* on disk is the same as clearing both folders and writing everything.
|
|
333
|
+
* `keepEmptyFolders` keeps a folder left with no files (the dev preview,
|
|
334
|
+
* whose watchers are on it); otherwise it goes, as it always did -
|
|
335
|
+
* content.config.ts skips a generated-docs/ that doesn't exist.
|
|
336
|
+
*
|
|
319
337
|
* Prints nothing: returns what it did, for the CLI to report -
|
|
320
338
|
* { groups: [{ group, src, operations }], pageSpecs: [{ spec, operations }],
|
|
321
|
-
* warnings: [message]
|
|
339
|
+
* warnings: [message], sources: [every local spec file read, $refs
|
|
340
|
+
* included], written: [paths], removed: [paths] }.
|
|
322
341
|
*/
|
|
323
|
-
export async function generateApiPages({ contentDir }) {
|
|
324
|
-
const summary
|
|
342
|
+
export async function generateApiPages({ contentDir, keepEmptyFolders = false }) {
|
|
343
|
+
const { summary, files, roots } = await planApiPages({ contentDir });
|
|
344
|
+
const { written, removed } = writeGeneratedFiles(roots, files, { keepEmptyFolders });
|
|
345
|
+
return { ...summary, written, removed };
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** Everything generateApiPages() would write, without writing it:
|
|
349
|
+
* `{ summary, files, roots }` - `files` maps each absolute path to its
|
|
350
|
+
* content, `roots` are the two folders it owns (generated-docs/ first).
|
|
351
|
+
* `writedocs dev` writes it in two steps (lib/openapi-reload-integration.js). */
|
|
352
|
+
export async function planApiPages({ contentDir }) {
|
|
353
|
+
const summary = { groups: [], pageSpecs: [], warnings: [], sources: [] };
|
|
325
354
|
const generatedDocsDir = path.join(writedocsTempDir(contentDir), 'generated-docs');
|
|
326
355
|
const openapiOutDir = path.join(writedocsTempDir(contentDir), 'openapi');
|
|
356
|
+
const roots = [generatedDocsDir, openapiOutDir];
|
|
327
357
|
|
|
328
358
|
let config;
|
|
329
359
|
try {
|
|
330
360
|
config = JSON.parse(readConfigText(contentDir));
|
|
331
361
|
} catch {
|
|
332
|
-
|
|
362
|
+
// preflightCheck() (run first, see dev.js/build.js) already reports this.
|
|
363
|
+
// Nothing to write - and nothing to delete either.
|
|
364
|
+
return { summary, files: null, roots };
|
|
333
365
|
}
|
|
334
366
|
|
|
335
|
-
// Always start from a clean slate: a group's `path` (or the group
|
|
336
|
-
// itself) may have changed or been removed since the last run, and
|
|
337
|
-
// there's no cheap way to tell stale generated output apart from
|
|
338
|
-
// still-current output without just regenerating everything.
|
|
339
|
-
rmrf(generatedDocsDir);
|
|
340
|
-
rmrf(openapiOutDir);
|
|
341
|
-
|
|
342
367
|
const groups = collectOpenApiGroups(config.navigation);
|
|
343
368
|
|
|
344
369
|
const seenPaths = new Map(); // normalized path -> owning group's label
|
|
@@ -359,22 +384,83 @@ export async function generateApiPages({ contentDir }) {
|
|
|
359
384
|
// overriding page is served at - for the redirects from where untagged
|
|
360
385
|
// operations used to be (lib/moved-api-pages.js).
|
|
361
386
|
const moves = { untagged: [], slugs: new Set() };
|
|
387
|
+
// Absolute path -> content: everything the two folders will hold.
|
|
388
|
+
const files = new Map();
|
|
389
|
+
const sources = new Set();
|
|
362
390
|
for (const group of groups) {
|
|
363
|
-
await generateApiPagesForGroup({ contentDir, generatedDocsDir, group, overrides, summary, moves });
|
|
391
|
+
await generateApiPagesForGroup({ contentDir, generatedDocsDir, group, overrides, summary, moves, files, sources });
|
|
364
392
|
}
|
|
365
393
|
const moved = moves.untagged.flatMap(({ pathPrefix, moved: list }) => untaggedRedirects(pathPrefix, list, moves.slugs));
|
|
366
|
-
if (moved.length)
|
|
394
|
+
if (moved.length) files.set(movedApiPagesFile(contentDir), JSON.stringify(moved, null, 2));
|
|
367
395
|
|
|
368
396
|
const groupSpecs = groups.map((g) => path.resolve(contentDir, g.openapi.src));
|
|
369
|
-
const pageSpecs = await parsePageSpecs({ contentDir, openapiOutDir, groupSpecs, summary });
|
|
397
|
+
const pageSpecs = await parsePageSpecs({ contentDir, openapiOutDir, groupSpecs, summary, files, sources });
|
|
370
398
|
// Every spec the site reads, as paths from the project root: these are
|
|
371
399
|
// how the site is built, not files it publishes (lib/project-files.js).
|
|
372
400
|
if (groupSpecs.length || pageSpecs.length) {
|
|
373
401
|
const specs = [...new Set([...groupSpecs, ...pageSpecs].map((abs) => path.relative(contentDir, abs).split(path.sep).join('/')))];
|
|
374
|
-
|
|
375
|
-
|
|
402
|
+
files.set(path.join(openapiOutDir, 'specs.json'), JSON.stringify(specs, null, 2));
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
summary.sources = [...sources];
|
|
406
|
+
return { summary, files, roots };
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
function listFiles(dir) {
|
|
410
|
+
let entries;
|
|
411
|
+
try {
|
|
412
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
413
|
+
} catch {
|
|
414
|
+
return [];
|
|
415
|
+
}
|
|
416
|
+
return entries.flatMap((entry) => {
|
|
417
|
+
const full = path.join(dir, entry.name);
|
|
418
|
+
return entry.isDirectory() ? listFiles(full) : [full];
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** Removes the empty folders under `dir`, and `dir` itself when it ends up
|
|
423
|
+
* empty and `keepRoot` is false. */
|
|
424
|
+
function removeEmptyFolders(dir, keepRoot) {
|
|
425
|
+
let entries;
|
|
426
|
+
try {
|
|
427
|
+
entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
428
|
+
} catch {
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
for (const entry of entries) if (entry.isDirectory()) removeEmptyFolders(path.join(dir, entry.name), false);
|
|
432
|
+
if (!keepRoot && fs.readdirSync(dir).length === 0) fs.rmdirSync(dir);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** Brings the folders in `roots` to exactly `files` (absolute path ->
|
|
436
|
+
* content): every other file under them is deleted, a file whose content is
|
|
437
|
+
* already the same is left alone, and the rest are written. Folders left
|
|
438
|
+
* empty are removed - each root too, unless `keepEmptyFolders`. Returns the
|
|
439
|
+
* paths written and removed. `files` null (writedocs.json unreadable) leaves
|
|
440
|
+
* everything as it is. */
|
|
441
|
+
export function writeGeneratedFiles(roots, files, { keepEmptyFolders = false } = {}) {
|
|
442
|
+
if (!files) return { written: [], removed: [] };
|
|
443
|
+
const written = [];
|
|
444
|
+
const removed = [];
|
|
445
|
+
for (const root of roots) {
|
|
446
|
+
for (const file of listFiles(root)) {
|
|
447
|
+
if (files.has(file)) continue;
|
|
448
|
+
fs.rmSync(file, { force: true });
|
|
449
|
+
removed.push(file);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
for (const [file, content] of files) {
|
|
453
|
+
let current = null;
|
|
454
|
+
try {
|
|
455
|
+
current = fs.readFileSync(file, 'utf-8');
|
|
456
|
+
} catch {}
|
|
457
|
+
if (current === content) continue;
|
|
458
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
459
|
+
fs.writeFileSync(file, content);
|
|
460
|
+
written.push(file);
|
|
376
461
|
}
|
|
377
|
-
|
|
462
|
+
for (const root of roots) removeEmptyFolders(root, keepEmptyFolders);
|
|
463
|
+
return { written, removed };
|
|
378
464
|
}
|
|
379
465
|
|
|
380
466
|
/** Mintlify's page-level form, `openapi: "/spec.json METHOD /path"`: the
|
|
@@ -385,7 +471,7 @@ export async function generateApiPages({ contentDir }) {
|
|
|
385
471
|
* pages are generated - the pages that name the spec are the pages. A
|
|
386
472
|
* spec that's missing or doesn't parse is a warning, not a build failure:
|
|
387
473
|
* its pages show the playground's own "no operation found" notice. */
|
|
388
|
-
async function parsePageSpecs({ contentDir, openapiOutDir, groupSpecs, summary }) {
|
|
474
|
+
async function parsePageSpecs({ contentDir, openapiOutDir, groupSpecs, summary, files, sources }) {
|
|
389
475
|
const specs = new Set();
|
|
390
476
|
for (const rel of findAllPages(contentDir)) {
|
|
391
477
|
let data;
|
|
@@ -406,15 +492,14 @@ async function parsePageSpecs({ contentDir, openapiOutDir, groupSpecs, summary }
|
|
|
406
492
|
}
|
|
407
493
|
let operations;
|
|
408
494
|
try {
|
|
409
|
-
operations = buildOperations(await
|
|
495
|
+
operations = buildOperations(await dereferenceSpec(specPath, sources));
|
|
410
496
|
} catch (err) {
|
|
411
497
|
summary.warnings.push(`Couldn't parse the OpenAPI spec "${spec}" that pages name: ${err.message}`);
|
|
412
498
|
continue;
|
|
413
499
|
}
|
|
414
500
|
const outDir = path.join(openapiOutDir, '_pages', specDirName(spec), 'operations');
|
|
415
|
-
fs.mkdirSync(outDir, { recursive: true });
|
|
416
501
|
for (const operation of operations) {
|
|
417
|
-
|
|
502
|
+
files.set(path.join(outDir, operationFileName(operation.method, operation.path)), operationJson(operation));
|
|
418
503
|
}
|
|
419
504
|
summary.pageSpecs.push({ spec, operations: operations.length });
|
|
420
505
|
}
|