Skip to content

Commit 1075add

Browse files
committed
docs: add docs:generate root script and update api doc generator
1 parent 76c2fac commit 1075add

4 files changed

Lines changed: 51 additions & 7 deletions

File tree

apps/docs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"type": "module",
66
"scripts": {
77
"dev": "next dev --turbopack -p 3002",
8-
"build": "next build --turbopack",
8+
"build": "bun run generate:api && next build --turbopack",
99
"start": "next start",
1010
"typecheck": "fumadocs-mdx && tsc --noEmit",
1111
"generate:api": "bun run scripts/generate-api-docs.ts"
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
import { generateFiles } from 'fumadocs-openapi';
22
import { spec } from '../../api/src/openapi.js';
3+
import { generateLlmsDocuments } from '../../api/src/llms.js';
34
import { writeFileSync, mkdirSync } from 'node:fs';
45
import { join } from 'node:path';
56

6-
const outDir = join(import.meta.dirname, '..', 'content', 'docs', 'api-reference');
7+
const appRoot = join(import.meta.dirname, '..');
8+
const outDir = join(appRoot, 'content', 'docs', 'api-reference');
9+
const publicDir = join(appRoot, 'public');
10+
const generatedSpecPath = join(outDir, 'openapi.json');
711

812
mkdirSync(outDir, { recursive: true });
13+
mkdirSync(publicDir, { recursive: true });
914

10-
const specPath = join(outDir, 'openapi.json');
11-
writeFileSync(specPath, JSON.stringify(spec, null, 2));
15+
writeFileSync(generatedSpecPath, JSON.stringify(spec, null, 2));
16+
17+
for (const [filename, content] of Object.entries(generateLlmsDocuments())) {
18+
writeFileSync(join(publicDir, filename), content);
19+
}
20+
21+
process.chdir(appRoot);
1222

1323
void generateFiles({
14-
input: [specPath],
15-
output: outDir,
24+
input: ['content/docs/api-reference/openapi.json'],
25+
output: 'content/docs/api-reference',
1626
groupBy: 'tag',
1727
});

apps/docs/src/lib/source.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { describe, test, expect } from 'bun:test'
2-
import { existsSync } from 'node:fs'
2+
import { existsSync, readFileSync } from 'node:fs'
33
import { join } from 'node:path'
44

55
const contentDir = join(import.meta.dirname, '../../content/docs')
6+
const apiReferenceDir = join(contentDir, 'api-reference')
7+
const repoRoot = join(import.meta.dirname, '../../../..')
68

79
describe('docs content', () => {
810
test('content directory exists', () => {
@@ -90,4 +92,35 @@ describe('openapi spec generation', () => {
9092
expect(tagNames).toContain('Artifacts')
9193
expect(tagNames).toContain('Replay')
9294
})
95+
96+
test('openapi.json artifact is generated from the live openapi spec', async () => {
97+
const { spec } = await import('../../../api/src/openapi.js')
98+
const specPath = join(apiReferenceDir, 'openapi.json')
99+
100+
expect(existsSync(specPath)).toBe(true)
101+
expect(JSON.parse(readFileSync(specPath, 'utf8'))).toEqual(spec)
102+
})
103+
104+
test('generated api pages live under the canonical docs tree without absolute path leakage', () => {
105+
const generatedPagePath = join(apiReferenceDir, 'health/get-health.mdx')
106+
const leakedOutputRoot = join(import.meta.dirname, '../../Users')
107+
108+
expect(existsSync(generatedPagePath)).toBe(true)
109+
expect(existsSync(leakedOutputRoot)).toBe(false)
110+
111+
const content = readFileSync(generatedPagePath, 'utf8')
112+
expect(content).toContain('<APIPage document={"content/docs/api-reference/openapi.json"}')
113+
expect(content.includes(repoRoot)).toBe(false)
114+
})
115+
116+
test('llms artifacts are generated from the live openapi spec', async () => {
117+
const { generateLlmsDocuments } = await import('../../../api/src/llms.js')
118+
const publicDir = join(import.meta.dirname, '../../public')
119+
120+
for (const [filename, content] of Object.entries(generateLlmsDocuments())) {
121+
const filePath = join(publicDir, filename)
122+
expect(existsSync(filePath)).toBe(true)
123+
expect(readFileSync(filePath, 'utf8')).toBe(content)
124+
}
125+
})
93126
})

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"test": "turbo test",
1414
"test:e2e": "turbo test:e2e",
1515
"dev": "turbo run dev",
16+
"docs:generate": "bun run --filter @sandchest/docs generate:api",
1617
"db:up": "bun run scripts/db-up.ts",
1718
"db:down": "docker compose down",
1819
"db:reset": "docker compose down -v && bun run db:up",

0 commit comments

Comments
 (0)