#!/bin/bash # Run from the root of your fdweb2 project set -e cd "$(git rev-parse --show-toplevel 2>/dev/null || pwd)" echo "=== FD TypeScript Error Fix Script ===" echo "" # ───────────────────────────────────────────────────────────── # GROUP 1: Remove dead template block imports from RenderBlocks # ───────────────────────────────────────────────────────────── RENDER_BLOCKS="src/app/(frontend)/(pages)/[slug]/RenderBlocks.tsx" # Try alternate path if first doesn't exist if [ ! -f "$RENDER_BLOCKS" ]; then RENDER_BLOCKS=$(find src -name "RenderBlocks.tsx" | head -1) fi echo "→ Fixing RenderBlocks.tsx at: $RENDER_BLOCKS" # Remove dead template block imports (leave all FD* blocks intact) sed -i '' \ '/^import { ArchiveBlock } from/d' \ "$RENDER_BLOCKS" sed -i '' \ '/^import { CallToActionBlock } from/d' \ "$RENDER_BLOCKS" sed -i '' \ '/^import { ContentBlock } from/d' \ "$RENDER_BLOCKS" sed -i '' \ '/^import { FormBlock } from/d' \ "$RENDER_BLOCKS" sed -i '' \ '/^import { MediaBlock } from/d' \ "$RENDER_BLOCKS" # Remove corresponding entries from blockComponents object sed -i '' \ '/^ archive: ArchiveBlock,/d' \ "$RENDER_BLOCKS" sed -i '' \ '/^ cta: CallToActionBlock,/d' \ "$RENDER_BLOCKS" sed -i '' \ '/^ content: ContentBlock,/d' \ "$RENDER_BLOCKS" sed -i '' \ '/^ formBlock: FormBlock,/d' \ "$RENDER_BLOCKS" sed -i '' \ '/^ mediaBlock: MediaBlock,/d' \ "$RENDER_BLOCKS" echo " ✓ Removed 5 dead template block imports and blockComponent entries" # ───────────────────────────────────────────────────────────── # GROUP 2: Fix CallToActionBlock import in RichText # ───────────────────────────────────────────────────────────── RICH_TEXT="src/components/RichText/index.tsx" if [ -f "$RICH_TEXT" ]; then # Remove CallToActionBlock from the payload-types import sed -i '' \ 's/import type { CallToActionBlock } from '\''@\/payload-types'\''/\/\/ CallToActionBlock removed - not in schema/' \ "$RICH_TEXT" # More general: remove just the named import if it's part of a multi-import sed -i '' \ 's/, CallToActionBlock//' \ "$RICH_TEXT" sed -i '' \ 's/CallToActionBlock, //' \ "$RICH_TEXT" echo " ✓ Removed CallToActionBlock from RichText" fi # ───────────────────────────────────────────────────────────── # GROUP 3: Fix null safety in FD block components # ───────────────────────────────────────────────────────────── echo "" echo "→ Fixing null safety issues in FD blocks..." # FDLocationsGridBlock - cards is possibly null LOCATIONS="src/blocks/FDLocationsGridBlock/Component.tsx" if [ -f "$LOCATIONS" ]; then # Change: cards.map( → (cards ?? []).map( sed -i '' \ 's/cards\.map(/\(cards ?? []\)\.map(/g' \ "$LOCATIONS" echo " ✓ FDLocationsGridBlock: cards null check" fi # FDNewsletterBlock - bulletPoints is possibly null NEWSLETTER="src/blocks/FDNewsletterBlock/Component.tsx" if [ -f "$NEWSLETTER" ]; then sed -i '' \ 's/bulletPoints\.map(/\(bulletPoints ?? []\)\.map(/g' \ "$NEWSLETTER" sed -i '' \ 's/bulletPoints\.length/\(bulletPoints ?? []\)\.length/g' \ "$NEWSLETTER" # Fix conditional renders: bulletPoints && → bulletPoints?.length && sed -i '' \ 's/bulletPoints &&/bulletPoints?.length \&\&/g' \ "$NEWSLETTER" echo " ✓ FDNewsletterBlock: bulletPoints null check" fi # FDServiceChooserBlock - categories is possibly null SERVICE_CHOOSER="src/blocks/FDServiceChooserBlock/Component.tsx" if [ -f "$SERVICE_CHOOSER" ]; then sed -i '' \ 's/categories\.map(/\(categories ?? []\)\.map(/g' \ "$SERVICE_CHOOSER" sed -i '' \ 's/categories\.filter(/\(categories ?? []\)\.filter(/g' \ "$SERVICE_CHOOSER" sed -i '' \ 's/categories\.find(/\(categories ?? []\)\.find(/g' \ "$SERVICE_CHOOSER" sed -i '' \ 's/categories\.length/\(categories ?? []\)\.length/g' \ "$SERVICE_CHOOSER" echo " ✓ FDServiceChooserBlock: categories null check" fi # FDStatisticsBlock - stats is possibly null STATISTICS="src/blocks/FDStatisticsBlock/Component.tsx" if [ -f "$STATISTICS" ]; then sed -i '' \ 's/stats\.map(/\(stats ?? []\)\.map(/g' \ "$STATISTICS" sed -i '' \ 's/stats\.length/\(stats ?? []\)\.length/g' \ "$STATISTICS" echo " ✓ FDStatisticsBlock: stats null check" fi # ───────────────────────────────────────────────────────────── # GROUP 4: Fix revalidatePath — Next.js now requires 2nd arg # ───────────────────────────────────────────────────────────── echo "" echo "→ Fixing revalidatePath calls (adding 'page' as second argument)..." REVALIDATE_FILES=( "src/collections/Pages/hooks/revalidatePage.ts" "src/collections/Posts/hooks/revalidatePost.ts" "src/Footer/hooks/revalidateFooter.ts" "src/Header/hooks/revalidateHeader.ts" "src/globals/PopupAnnouncement/hooks/revalidatePopup.ts" "src/hooks/revalidateRedirects.ts" ) for FILE in "${REVALIDATE_FILES[@]}"; do if [ -f "$FILE" ]; then # revalidatePath('/some/path') → revalidatePath('/some/path', 'page') # Match revalidatePath with a single string arg (no comma inside the parens) sed -i '' \ "s/revalidatePath('\([^']*\)')/revalidatePath('\1', 'page')/g" \ "$FILE" sed -i '' \ 's/revalidatePath("\([^"]*\)")/revalidatePath("\1", "page")/g' \ "$FILE" # Handle template literals: revalidatePath(\`...\`) → revalidatePath(\`...\`, 'page') # This one is trickier with backticks, handle separately perl -i '' -pe 's/revalidatePath\(`([^`]*)`\)/revalidatePath(`$1`, '\''page'\'')/g' "$FILE" 2>/dev/null || true echo " ✓ $FILE" fi done # ───────────────────────────────────────────────────────────── # GROUP 5: Delete BACKUP file (it's causing compilation errors) # ───────────────────────────────────────────────────────────── echo "" echo "→ Removing backup files from compilation..." BACKUP="src/Header/Nav/index BACKUP.tsx" if [ -f "$BACKUP" ]; then rm "$BACKUP" echo " ✓ Deleted 'src/Header/Nav/index BACKUP.tsx'" else echo " - BACKUP file not found (already removed?)" fi # ───────────────────────────────────────────────────────────── # GROUP 6: Fix Footer/Header getCachedGlobal type cast # ───────────────────────────────────────────────────────────── echo "" echo "→ Fixing Footer and Header getCachedGlobal type cast..." FOOTER_COMPONENT="src/Footer/Component.tsx" if [ -f "$FOOTER_COMPONENT" ]; then # Add 'as Footer' cast to getCachedGlobal call sed -i '' \ "s/const \(.*\) = await getCachedGlobal('footer'/const \1 = (await getCachedGlobal('footer'/" \ "$FOOTER_COMPONENT" echo " ⚠ Footer/Component.tsx needs manual fix — see note below" fi HEADER_COMPONENT="src/Header/Component.tsx" if [ -f "$HEADER_COMPONENT" ]; then echo " ⚠ Header/Component.tsx needs manual fix — see note below" fi # ───────────────────────────────────────────────────────────── # DONE # ───────────────────────────────────────────────────────────── echo "" echo "=== Script complete ===" echo "" echo "MANUAL FIXES STILL NEEDED:" echo "" echo "1. FDTagsBlock and FDTextBlock missing from payload-types:" echo " → Run: npx payload generate:types" echo " → If still missing, check that FDTagsBlock and FDTextBlock" echo " are imported and added to the Pages collection layout field" echo " in src/collections/Pages/index.ts" echo "" echo "2. Footer/Component.tsx and Header/Component.tsx type mismatch:" echo " → Change the getCachedGlobal line to add a type cast, e.g.:" echo " const footerData = await getCachedGlobal('footer', 1) as Footer" echo " const headerData = await getCachedGlobal('header', 1) as Header" echo "" echo "3. Footer/RowLabel.tsx and Header/RowLabel.tsx — 'link' property:" echo " → Change: data.link?.label" echo " To: data.label (the nav item schema changed, link is now flat)" echo "" echo "4. Media.caption doesn't exist — removed from your schema:" echo " → In MediaBlock/Component.tsx line 33: remove the .caption reference" echo " → In heros/MediumImpact/index.tsx lines 36/38: remove .caption" echo " → In src/endpoints/seed/image-*.ts: remove caption field" echo "" echo "5. Seed files (contact-page.ts, home.ts, index.ts):" echo " → These are dev seeds, not used in production." echo " → Quickest fix: add // @ts-ignore above each flagged line," echo " or delete the seed files if you no longer use them." echo "" echo "After manual fixes, run: npx tsc --noEmit to verify"