@@ -12,7 +12,7 @@ import { symLinkConfig } from './symLinkConfig.js'
1212import { buildPropsData } from './buildPropsData.js'
1313import { hasFile } from './hasFile.js'
1414import { convertToMDX } from './convertToMDX.js'
15- import { mkdir , copyFile } from 'fs/promises'
15+ import { mkdir , copyFile , readFile , writeFile } from 'fs/promises'
1616import { fileExists } from './fileExists.js'
1717
1818const 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
110135async 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
195222program . 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
0 commit comments