@@ -9,8 +9,11 @@ import { difference, isEqual } from 'lodash-es'
99
1010import { allVersions } from '@/versions/lib/all-versions'
1111import getApplicableVersions from '@/versions/lib/get-applicable-versions'
12+ import { createLogger } from '@/observability/logger'
1213import type { MarkdownFrontmatter } from '@/types'
1314
15+ const logger = createLogger ( import . meta. url )
16+
1417// Type definitions - extending existing type to add missing fields and make most fields optional
1518type FrontmatterData = Partial < MarkdownFrontmatter > & {
1619 autogenerated ?: string
@@ -89,6 +92,12 @@ async function removeMarkdownFiles(
8992 // If the first array contains items that the second array does not,
9093 // it means that a Markdown page was deleted from the OpenAPI schema
9194 const filesToRemove = difference ( autogeneratedFiles , sourceFiles )
95+ if ( filesToRemove . length > 0 ) {
96+ logger . info ( 'Removing stale markdown files' , {
97+ targetDirectory,
98+ count : filesToRemove . length ,
99+ } )
100+ }
92101 // Markdown files that need to be deleted
93102 for ( const file of filesToRemove ) {
94103 unlinkSync ( file )
@@ -159,6 +168,7 @@ async function updateMarkdownFile(
159168 const matcher = new RegExp ( commentDelimiter , 'g' )
160169 const matches = content . match ( matcher )
161170 if ( matches && matches . length > 1 ) {
171+ logger . error ( 'File has multiple comment delimiters' , { file } )
162172 throw new Error ( `Error: ${ file } has multiple comment delimiters` )
163173 }
164174
@@ -171,9 +181,17 @@ async function updateMarkdownFile(
171181 const isVersionsSame = isEqual ( sourceData . versions , data . versions )
172182 // Only proceed if the content or versions have changed
173183 if ( isContentSame && isVersionsSame && ! isDelimiterMissing ) {
184+ logger . debug ( 'Markdown file unchanged, skipping' , { file } )
174185 return
175186 }
176187
188+ logger . debug ( 'Updating existing markdown file' , {
189+ file,
190+ contentChanged : ! isContentSame ,
191+ versionsChanged : ! isVersionsSame ,
192+ delimiterMissing : isDelimiterMissing ,
193+ } )
194+
177195 // Create a new object so that we don't mutate the original data
178196 const newData = { ...data }
179197 // Only modify the versions property when a file already exists
@@ -182,6 +200,7 @@ async function updateMarkdownFile(
182200 const newFileContent = appendVersionComment ( matter . stringify ( targetContent , newData ) )
183201 await writeFile ( file , newFileContent )
184202 } else {
203+ logger . info ( 'Creating new markdown file' , { file } )
185204 await createDirectory ( path . dirname ( file ) )
186205 const newFileContent = appendVersionComment (
187206 matter . stringify ( commentDelimiter + sourceContent , sourceData ) ,
@@ -202,6 +221,7 @@ async function updateDirectory(
202221 const initialDirectoryListing = await getDirectoryInfo ( directory )
203222 // If there are no children on disk, remove the directory
204223 if ( initialDirectoryListing . directoryContents . length === 0 && ! rootDirectoryOnly ) {
224+ logger . info ( 'Removing empty directory' , { directory } )
205225 await rimraf ( directory )
206226 return
207227 }
@@ -231,6 +251,7 @@ async function updateDirectory(
231251 const itemsToAdd = difference ( childrenOnDisk , indexChildren )
232252 const itemsToRemove = difference ( indexChildren , childrenOnDisk )
233253 if ( itemsToRemove . length === 0 && itemsToAdd . length === 0 ) {
254+ logger . debug ( 'Index children unchanged, skipping' , { indexFile } )
234255 return
235256 }
236257
@@ -268,6 +289,7 @@ function getChildrenToCompare(
268289 fmChildren : string [ ] | undefined ,
269290) : ChildrenComparison {
270291 if ( ! fmChildren ) {
292+ logger . error ( 'No children property found in index file' , { indexFile } )
271293 throw new Error ( `No children property found in ${ indexFile } ` )
272294 }
273295
@@ -386,6 +408,7 @@ async function getIndexFileVersions(
386408 files . map ( async ( file ) => {
387409 const filepath = path . join ( directory , file )
388410 if ( ! existsSync ( filepath ) ) {
411+ logger . error ( 'File does not exist while assembling index versions' , { filepath } )
389412 throw new Error (
390413 `File ${ filepath } does not exist while assembling directory index.md files to create parent version.` ,
391414 )
@@ -396,6 +419,7 @@ async function getIndexFileVersions(
396419 }
397420 const { data } = matter ( await readFile ( filepath , 'utf-8' ) )
398421 if ( ! data || ! data . versions ) {
422+ logger . error ( 'Frontmatter missing versions' , { filepath } )
399423 throw new Error ( `Frontmatter in ${ filepath } does not contain versions.` )
400424 }
401425 const fmVersions = getApplicableVersions ( data . versions )
@@ -526,6 +550,7 @@ async function createDirectory(targetDirectory: string): Promise<void> {
526550
527551async function getDirectoryInfo ( directory : string ) : Promise < DirectoryInfo > {
528552 if ( ! existsSync ( directory ) ) {
553+ logger . error ( 'Directory does not exist' , { directory } )
529554 throw new Error ( `Directory ${ directory } did not exist when attempting to get directory info.` )
530555 }
531556 const directoryContents = ( await readdir ( directory ) ) . filter (
0 commit comments