Skip to content

Commit e134bd6

Browse files
louis-preclaude
andcommitted
Centralize codegen path configuration
Add codegen/lib/config.ts with all path-related constants: - apiRootPath: Metalsmith file key prefix ('api-reference') - apiUrlPrefix: published URL prefix ('/api') - baseUrl: docs site base URL - siteUrlPrefix: URL prefix to strip ('/latest') - apiReferenceDir, apiSummaryPath: derived disk paths All codegen files now import from config instead of defining their own constants. Changing the output directory or URL prefix only requires editing config.ts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 29f4c35 commit e134bd6

6 files changed

Lines changed: 44 additions & 22 deletions

File tree

codegen/lib/config.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { join } from 'node:path'
2+
3+
// The Metalsmith file key prefix and disk directory name for generated API docs.
4+
export const apiRootPath = 'api-reference'
5+
6+
// The published URL prefix for the API Reference site section on docs.seam.co.
7+
export const apiUrlPrefix = '/api'
8+
9+
// The base URL for the published documentation site.
10+
export const baseUrl = 'https://docs.seam.co/latest/'
11+
12+
// The URL prefix to strip when resolving absolute URLs to file paths.
13+
export const siteUrlPrefix = '/latest'
14+
15+
// Derived paths
16+
export const apiReferenceDir = join('docs', apiRootPath)
17+
export const apiSummaryPath = join(apiReferenceDir, 'SUMMARY.md')

codegen/lib/postprocess.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import { dirname, join, relative, sep } from 'node:path'
33

44
import type Metalsmith from 'metalsmith'
55

6-
const baseUrl = 'https://docs.seam.co/latest/'
7-
const urlPrefix = '/latest'
8-
const apiUrlPrefix = '/api'
9-
const apiReferenceRoot = join('docs', 'api-reference')
6+
import {
7+
apiReferenceDir,
8+
apiUrlPrefix,
9+
baseUrl,
10+
siteUrlPrefix,
11+
} from './config.js'
1012

1113
export const postprocess = (
1214
files: Metalsmith.Files,
@@ -18,14 +20,14 @@ export const postprocess = (
1820
contents.replaceAll(new RegExp(`(${baseUrl}[^)]+)`, 'g'), ($1, $2) => {
1921
if (typeof $2 !== 'string') return $1
2022
const url = new URL($2)
21-
url.pathname = url.pathname.replace(urlPrefix, '')
23+
url.pathname = url.pathname.replace(siteUrlPrefix, '')
2224

2325
// Only relativize links to pages within the api-reference space.
24-
// The site section uses /api as its URL prefix.
26+
// The site section uses apiUrlPrefix as its URL prefix.
2527
// Cross-section links (to guides) stay as absolute URLs.
2628
if (!url.pathname.startsWith(apiUrlPrefix + '/')) return $1
2729
const apiPath = url.pathname.replace(apiUrlPrefix, '')
28-
const targetRoot = join(apiReferenceRoot, apiPath)
30+
const targetRoot = join(apiReferenceDir, apiPath)
2931

3032
const target = `${targetRoot}.md`
3133
if (existsSync(target)) {
@@ -44,7 +46,7 @@ export const postprocess = (
4446
}
4547

4648
const getRelativeLink = (name: string, target: string, url: URL): string => {
47-
const src = join(apiReferenceRoot, name)
49+
const src = join(apiReferenceDir, name)
4850
const relativePath = relative(dirname(src), target)
4951
const urlPath = relativePath.split(sep).join('/')
5052
return `${urlPath}${url.hash}`

codegen/lib/reference.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
type ApiSummaryLayoutContext,
1616
setSummaryLayoutContext,
1717
} from './layout/summary.js'
18+
import { apiRootPath } from './config.js'
1819
import { PathMetadataSchema } from './path-metadata.js'
1920

2021
interface Metadata {
@@ -27,7 +28,7 @@ type File = ApiEndpointLayoutContext &
2728
ApiNamespaceLayoutContext &
2829
ApiSummaryLayoutContext & { layout: string }
2930

30-
const rootPath = 'api-reference'
31+
const rootPath = apiRootPath
3132
const indexFile = 'README.md'
3233

3334
export const reference = (

codegen/lib/report.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import type {
1212
import { openapi } from '@seamapi/types/connect'
1313
import type Metalsmith from 'metalsmith'
1414

15+
import { apiRootPath } from './config.js'
16+
1517
const defaultDeprecatedMessage = 'No deprecated message provided'
1618
const defaultDraftMessage = 'No draft message provided'
1719
const defaultUndocumentedMessage = 'No undocumented message provided'
@@ -75,13 +77,13 @@ export const report = (
7577

7678
const reportData = generateReport(metadata)
7779

78-
files['api-reference/_report.md'] = {
80+
files[`${apiRootPath}/_report.md`] = {
7981
contents: Buffer.from('\n'),
8082
layout: 'report.hbs',
8183
...reportData,
8284
}
8385

84-
files['api-reference/_blueprint.json'] = {
86+
files[`${apiRootPath}/_blueprint.json`] = {
8587
contents: Buffer.from(JSON.stringify(metadata.blueprint, null, 2)),
8688
layout: 'default.hbs',
8789
}

codegen/lib/summary.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
import { readFile } from 'node:fs/promises'
2-
import { join } from 'node:path'
32

43
import type Metalsmith from 'metalsmith'
54

6-
const summaryPath = join('docs', 'api-reference', 'SUMMARY.md')
5+
import { apiRootPath, apiSummaryPath } from './config.js'
76

87
export const summary = async (
98
files: Metalsmith.Files,
109
_metalsmith: Metalsmith,
1110
): Promise<void> => {
12-
const apiSummary =
13-
files['api-reference/_summary.md']?.contents?.toString('utf-8')
11+
const summaryKey = `${apiRootPath}/_summary.md`
12+
const apiSummary = files[summaryKey]?.contents?.toString('utf-8')
1413
if (apiSummary == null) {
15-
throw new Error('Missing api-reference/_summary.md')
14+
throw new Error(`Missing ${summaryKey}`)
1615
}
1716

18-
const k = 'api-reference/SUMMARY.md'
17+
const k = `${apiRootPath}/SUMMARY.md`
1918
const summary = await readSummary()
2019
const contents = getUpdatedSummary(summary, apiSummary)
2120
files[k] = { contents: Buffer.from(`${contents}\n`) }
22-
delete files['api-reference/_summary.md']
21+
delete files[summaryKey]
2322
}
2423

2524
const readSummary = async (): Promise<string> => {
26-
const buf = await readFile(summaryPath)
25+
const buf = await readFile(apiSummaryPath)
2726
return buf.toString('utf-8')
2827
}
2928

codegen/smith.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as types from '@seamapi/types/connect'
99
import { deleteAsync } from 'del'
1010
import Metalsmith from 'metalsmith'
1111

12+
import { apiReferenceDir } from './lib/config.js'
1213
import {
1314
formatCode,
1415
helpers,
@@ -22,9 +23,9 @@ const rootDir = dirname(fileURLToPath(import.meta.url))
2223

2324
await Promise.all([
2425
deleteAsync([
25-
'./docs/api-reference/**',
26-
'!./docs/api-reference/SUMMARY.md',
27-
'!./docs/api-reference/.gitbook.yaml',
26+
`./${apiReferenceDir}/**`,
27+
`!./${apiReferenceDir}/SUMMARY.md`,
28+
`!./${apiReferenceDir}/.gitbook.yaml`,
2829
]),
2930
])
3031

0 commit comments

Comments
 (0)