@@ -12,7 +12,8 @@ import { symLinkConfig } from './symLinkConfig.js'
1212import { buildPropsData } from './buildPropsData.js'
1313import { hasFile } from './hasFile.js'
1414import { convertToMDX } from './convertToMDX.js'
15- import { mkdir } from 'fs/promises'
15+ import { mkdir , copyFile } from 'fs/promises'
16+ import { fileExists } from './fileExists.js'
1617
1718const currentDir = process . cwd ( )
1819const config = await getConfig ( `${ currentDir } /pf-docs.config.mjs` )
@@ -86,6 +87,26 @@ async function transformMDContentToMDX() {
8687 }
8788}
8889
90+ async function initializeApiIndex ( ) {
91+ const templateIndexPath = join ( astroRoot , 'cli' , 'templates' , 'apiIndex.json' )
92+ const targetIndexPath = join ( astroRoot , 'src' , 'apiIndex.json' )
93+
94+ const indexExists = await fileExists ( targetIndexPath )
95+
96+ // early return if the file exists from a previous build
97+ if ( indexExists ) {
98+ console . log ( 'apiIndex.json already exists, skipping initialization' )
99+ return
100+ }
101+
102+ try {
103+ await copyFile ( templateIndexPath , targetIndexPath )
104+ console . log ( 'Initialized apiIndex.json' )
105+ } catch ( e : any ) {
106+ console . error ( 'Error copying apiIndex.json template:' , e )
107+ }
108+ }
109+
89110async function buildProject ( ) : Promise < DocsConfig | undefined > {
90111 await updateContent ( program )
91112 await generateProps ( program , true )
@@ -103,6 +124,7 @@ async function buildProject(): Promise<DocsConfig | undefined> {
103124 return config
104125 }
105126
127+ await initializeApiIndex ( )
106128 await transformMDContentToMDX ( )
107129
108130 build ( {
@@ -172,6 +194,7 @@ program.command('init').action(async () => {
172194
173195program . command ( 'start' ) . action ( async ( ) => {
174196 await updateContent ( program )
197+ await initializeApiIndex ( )
175198
176199 // if a props file hasn't been generated yet, but the consumer has propsData, it will cause a runtime error so to
177200 // prevent that we're just creating a props file regardless of what they say if one doesn't exist yet
0 commit comments