@@ -408,8 +408,27 @@ function countTotalLanes(lanesReferenceData: LanesConfig): number {
408408function getTokenName ( tokenSymbol : string , tokensData : TokensConfig ) : string {
409409 // Get token details from the tokens data
410410 const tokenDetails = tokensData [ tokenSymbol ]
411- const sampleChain = tokenDetails ? Object . keys ( tokenDetails ) [ 0 ] : null
412- return sampleChain && tokenDetails [ sampleChain ] ? tokenDetails [ sampleChain ] . name || tokenSymbol : tokenSymbol
411+
412+ if ( ! tokenDetails ) {
413+ logger . warn ( { tokenSymbol } , "Token details not found in reference data" )
414+ return tokenSymbol
415+ }
416+
417+ const availableChains = Object . keys ( tokenDetails )
418+ if ( availableChains . length === 0 ) {
419+ logger . warn ( { tokenSymbol } , "No chains found for token" )
420+ return tokenSymbol
421+ }
422+
423+ const sampleChain = availableChains [ 0 ]
424+ const chainTokenData = tokenDetails [ sampleChain ]
425+
426+ if ( ! chainTokenData ) {
427+ logger . warn ( { tokenSymbol, sampleChain } , "Chain token data not found" )
428+ return tokenSymbol
429+ }
430+
431+ return chainTokenData . name || tokenSymbol
413432}
414433
415434/**
@@ -1519,6 +1538,56 @@ function getFileFromGitHistory(filePath: string, date: string): string | null {
15191538 }
15201539}
15211540
1541+ /**
1542+ * Get a robust prettier configuration with fallbacks
1543+ *
1544+ * @param {string } filePath - File path to resolve config for
1545+ * @returns {Promise<prettier.Config> } Prettier configuration object
1546+ */
1547+ async function getPrettierConfig ( filePath : string ) : Promise < prettier . Config > {
1548+ try {
1549+ // Try resolving config for the specific file path first
1550+ let config = await prettier . resolveConfig ( filePath )
1551+
1552+ if ( ! config ) {
1553+ // Fallback: try resolving with the config file directly
1554+ config = await prettier . resolveConfig ( ".prettierrc" )
1555+ }
1556+
1557+ if ( ! config ) {
1558+ // Final fallback: use project defaults from .prettierrc
1559+ logger . warn ( "Could not resolve prettier config, using fallback defaults" )
1560+ config = {
1561+ semi : false ,
1562+ singleQuote : false ,
1563+ tabWidth : 2 ,
1564+ trailingComma : "es5" as const ,
1565+ printWidth : 120 ,
1566+ }
1567+ }
1568+
1569+ logger . debug ( { config } , "Resolved prettier configuration" )
1570+ return config
1571+ } catch ( error ) {
1572+ logger . error (
1573+ {
1574+ error : error instanceof Error ? error . message : String ( error ) ,
1575+ filePath,
1576+ } ,
1577+ "Error resolving prettier config, using fallback"
1578+ )
1579+
1580+ // Return safe fallback configuration
1581+ return {
1582+ semi : false ,
1583+ singleQuote : false ,
1584+ tabWidth : 2 ,
1585+ trailingComma : "es5" as const ,
1586+ printWidth : 120 ,
1587+ }
1588+ }
1589+ }
1590+
15221591/**
15231592 * Generate a changelog entry for newly supported tokens
15241593 *
@@ -1613,16 +1682,17 @@ async function generateChangelogEntry(
16131682 logger . debug ( `Created directory: ${ changelogDir } ` )
16141683 }
16151684
1616- // Format the JSON with prettier using project config
1617- const prettierConfig = await prettier . resolveConfig ( process . cwd ( ) )
1685+ // Get robust prettier configuration
1686+ const prettierConfig = await getPrettierConfig ( FILE_PATHS . CHANGELOG )
16181687
1619- // Format the JSON content directly before writing (like detect-new-data.ts)
1620- const formattedJson = await prettier . format ( JSON . stringify ( changelog ) , {
1688+ // Format the JSON content with proper initial formatting
1689+ const jsonString = JSON . stringify ( changelog , null , 2 )
1690+ const formattedJson = await prettier . format ( jsonString , {
16211691 ...prettierConfig ,
1622- parser : "json" , // Explicitly specify JSON parser
1692+ parser : "json" ,
16231693 } )
16241694
1625- // Write the formatted content directly
1695+ // Write the formatted content
16261696 fs . writeFileSync ( FILE_PATHS . CHANGELOG , formattedJson , "utf8" )
16271697
16281698 const changelogSize = fs . statSync ( FILE_PATHS . CHANGELOG ) . size
@@ -1632,7 +1702,7 @@ async function generateChangelogEntry(
16321702 entriesCount : changelog . data . length ,
16331703 sizeBytes : changelogSize ,
16341704 } ,
1635- "Changelog file updated and formatted"
1705+ "Changelog file updated and formatted successfully "
16361706 )
16371707 } catch ( error ) {
16381708 logger . error (
@@ -1642,6 +1712,7 @@ async function generateChangelogEntry(
16421712 } ,
16431713 "Error writing changelog file"
16441714 )
1715+ throw error // Re-throw to handle upstream
16451716 }
16461717
16471718 return {
0 commit comments