Skip to content

Commit 7b27a98

Browse files
Update to output the apiIndex to the user specified output directory
1 parent d75e164 commit 7b27a98

File tree

11 files changed

+64
-25
lines changed

11 files changed

+64
-25
lines changed

cli/cli.ts

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { symLinkConfig } from './symLinkConfig.js'
1212
import { buildPropsData } from './buildPropsData.js'
1313
import { hasFile } from './hasFile.js'
1414
import { convertToMDX } from './convertToMDX.js'
15-
import { mkdir, copyFile } from 'fs/promises'
15+
import { mkdir, copyFile, readFile, writeFile } from 'fs/promises'
1616
import { fileExists } from './fileExists.js'
1717

1818
const currentDir = process.cwd()
@@ -87,29 +87,52 @@ async function transformMDContentToMDX() {
8787
}
8888
}
8989

90-
async function initializeApiIndex() {
91-
const templateIndexPath = join(astroRoot, 'cli', 'templates', 'apiIndex.json')
92-
const targetIndexPath = join(astroRoot, 'src', 'apiIndex.json')
90+
async function updateTsConfigOutputDirPath(program: Command) {
91+
const { verbose } = program.opts()
92+
const tsConfigPath = join(astroRoot, 'tsconfig.json')
93+
94+
try {
95+
const tsConfigFile = await readFile(tsConfigPath, 'utf-8')
96+
const tsConfig = JSON.parse(tsConfigFile)
97+
const formattedOutputDir = join(absoluteOutputDir, '*')
98+
99+
tsConfig.compilerOptions.paths["outputDir/*"] = [formattedOutputDir]
100+
101+
await writeFile(tsConfigPath, JSON.stringify(tsConfig, null, 2))
102+
103+
if (verbose) {
104+
console.log(`Updated tsconfig.json with outputDir path: ${formattedOutputDir}`)
105+
}
106+
} catch (e: any) {
107+
console.error('Error updating tsconfig.json with outputDir path:', e)
108+
}
109+
}
93110

111+
async function initializeApiIndex(program: Command) {
112+
const { verbose } = program.opts()
113+
const templateIndexPath = join(astroRoot, 'cli', 'templates', 'apiIndex.json')
114+
const targetIndexPath = join(absoluteOutputDir, 'apiIndex.json')
94115
const indexExists = await fileExists(targetIndexPath)
95116

96117
// early return if the file exists from a previous build
97118
if (indexExists) {
98-
console.log('apiIndex.json already exists, skipping initialization')
119+
if (verbose) {
120+
console.log('apiIndex.json already exists, skipping initialization')
121+
}
99122
return
100123
}
101124

102125
try {
103126
await copyFile(templateIndexPath, targetIndexPath)
104-
console.log('Initialized apiIndex.json')
127+
if (verbose) {
128+
console.log('Initialized apiIndex.json')
129+
}
105130
} catch (e: any) {
106131
console.error('Error copying apiIndex.json template:', e)
107132
}
108133
}
109134

110135
async function buildProject(): Promise<DocsConfig | undefined> {
111-
await updateContent(program)
112-
await generateProps(program, true)
113136
if (!config) {
114137
console.error(
115138
'No config found, please run the `setup` command or manually create a pf-docs.config.mjs file',
@@ -123,13 +146,17 @@ async function buildProject(): Promise<DocsConfig | undefined> {
123146
)
124147
return config
125148
}
126-
127-
await initializeApiIndex()
149+
await updateTsConfigOutputDirPath(program)
150+
await updateContent(program)
151+
await generateProps(program, true)
152+
await initializeApiIndex(program)
128153
await transformMDContentToMDX()
129154

130-
build({
155+
const docsOutputDir = join(absoluteOutputDir, 'docs')
156+
157+
await build({
131158
root: astroRoot,
132-
outDir: join(absoluteOutputDir, 'docs'),
159+
outDir: docsOutputDir,
133160
})
134161

135162
return config
@@ -193,8 +220,9 @@ program.command('init').action(async () => {
193220
})
194221

195222
program.command('start').action(async () => {
223+
await updateTsConfigOutputDirPath(program)
196224
await updateContent(program)
197-
await initializeApiIndex()
225+
await initializeApiIndex(program)
198226

199227
// if a props file hasn't been generated yet, but the consumer has propsData, it will cause a runtime error so to
200228
// prevent that we're just creating a props file regardless of what they say if one doesn't exist yet

jest.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ const config: Config = {
1616
testPathIgnorePatterns: ['/node_modules/', '/__tests__/helpers/'],
1717
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
1818
moduleNameMapper: {
19+
'^outputDir/(.*)$': '<rootDir>/dist/$1',
1920
'\\.(css|less)$': '<rootDir>/src/__mocks__/styleMock.ts',
20-
'(.+)\\.js': '$1',
2121
'^astro:content$': '<rootDir>/src/__mocks__/astro-content.ts',
22+
'(.+)\\.js': '$1',
2223
},
2324
setupFilesAfterEnv: ['<rootDir>/test.setup.ts'],
2425
transformIgnorePatterns: [

src/__tests__/pages/api/__tests__/[version].test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { GET } from '../../../../pages/api/[version]'
44
* Mock apiIndex.json with multiple versions (v5, v6)
55
* to test section retrieval for different versions
66
*/
7-
jest.mock('../../../../apiIndex.json', () => ({
7+
jest.mock('outputDir/apiIndex.json', () => ({
88
versions: ['v5', 'v6'],
99
sections: {
1010
v5: ['getting-started'],

src/__tests__/pages/api/__tests__/versions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GET } from '../../../../pages/api/versions'
33
/**
44
* Mock apiIndex.json with multiple versions
55
*/
6-
jest.mock('../../../../apiIndex.json', () => ({
6+
jest.mock('outputDir/apiIndex.json', () => ({
77
versions: ['v5', 'v6'],
88
sections: {},
99
pages: {},

src/pages/api/[version].ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { APIRoute } from 'astro'
22
import { createJsonResponse, createIndexKey } from '../../utils/apiHelpers'
3-
import { sections as sectionsData } from '../../apiIndex.json'
3+
import { sections as sectionsData } from 'outputDir/apiIndex.json'
44

55
export const prerender = false
66

src/pages/api/[version]/[section].ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { APIRoute } from 'astro'
22
import { createJsonResponse, createIndexKey } from '../../../utils/apiHelpers'
3-
import { pages as pagesData } from '../../../apiIndex.json'
3+
import { pages as pagesData } from 'outputDir/apiIndex.json'
44

55
export const prerender = false
66

src/pages/api/[version]/[section]/[page].ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { APIRoute } from 'astro'
22
import { createJsonResponse, createIndexKey } from '../../../../utils/apiHelpers'
3-
import { tabs as tabsData } from '../../../../apiIndex.json'
3+
import { tabs as tabsData } from 'outputDir/apiIndex.json'
44

55
export const prerender = false
66

src/pages/api/openapi.json.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { APIRoute } from 'astro'
2-
import index from '../../apiIndex.json'
2+
import index from 'outputDir/apiIndex.json'
33

44
export const prerender = false
55

src/pages/api/versions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { APIRoute } from 'astro'
22
import { createJsonResponse } from '../../utils/apiHelpers'
3-
import { versions as versionsData } from '../../apiIndex.json'
3+
import { versions as versionsData } from 'outputDir/apiIndex.json'
44

55
export const prerender = false
66

src/utils/apiIndex/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export async function generateApiIndex(): Promise<ApiIndex> {
133133
}
134134

135135
/**
136-
* Writes API index to src/apiIndex.json
136+
* Writes API index to a apiIndex.json file in the user defined output directory
137137
* This file is used by server-side API routes to avoid runtime getCollection() calls
138138
*
139139
* @param index - The API index structure to write

0 commit comments

Comments
 (0)