Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 146 additions & 0 deletions migrate-nav-footer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
// Migration script to convert old cdnav/cdfooter to new reference components
const sanityClient = require('@sanity/client')
require('dotenv').config({ path: '../../.env.local' })

// Configure Sanity client
const client = sanityClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || process.env.SANITY_STUDIO_PROJECT_ID,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
apiVersion: '2023-01-01',
token: process.env.SANITY_API_TOKEN,
useCdn: false
})

async function findGlobalComponents() {
console.log('🔍 Finding global navigation and footer components...')

// Find global navigation
const globalNavs = await client.fetch(`*[_type == "cdnav"] | order(_createdAt desc)[0...5]{
_id,
title,
_createdAt
}`)

// Find global footer
const globalFooters = await client.fetch(`*[_type == "cdfooter"] | order(_createdAt desc)[0...5]{
_id,
title,
_createdAt
}`)

console.log('\n📋 Available Global Navigation Components:')
globalNavs.forEach((nav, i) => {
console.log(` ${i + 1}. ${nav.title || 'Untitled'} (ID: ${nav._id})`)
})

console.log('\n📋 Available Global Footer Components:')
globalFooters.forEach((footer, i) => {
console.log(` ${i + 1}. ${footer.title || 'Untitled'} (ID: ${footer._id})`)
})

// Return the first ones (most recent) as defaults
return {
navId: globalNavs[0]?._id,
footerId: globalFooters[0]?._id
}
}

async function migrateComponents() {
try {
console.log('🚀 Starting migration...\n')

// Get global component IDs
const { navId, footerId } = await findGlobalComponents()

if (!navId || !footerId) {
console.error('❌ Error: Could not find global navigation or footer components.')
console.log('\n📝 Please create global components first:')
console.log(' 1. Go to Sanity Studio')
console.log(' 2. Navigate to "Global Components"')
console.log(' 3. Create a Navigation (CD) document')
console.log(' 4. Create a Footer (CD) document')
console.log(' 5. Run this migration script again')
return
}

console.log(`\n✅ Using global navigation: ${navId}`)
console.log(`✅ Using global footer: ${footerId}\n`)

// Fetch all pages and homepage with old components
const documents = await client.fetch(`
*[_type in ["page", "homepage"] && defined(components) && count(components[_type in ["cdnav", "cdfooter"]]) > 0]{
_id,
_type,
_rev,
title,
"componentTypes": components[]._type,
components
}
`)

console.log(`📄 Found ${documents.length} documents to migrate\n`)

for (const doc of documents) {
console.log(`\n🔄 Processing ${doc._type}: ${doc.title || doc._id}`)
console.log(` Current components: ${doc.componentTypes.join(', ')}`)

// Transform components
const updatedComponents = doc.components.map((component, index) => {
if (component._type === 'cdnav') {
console.log(` ✏️ Converting cdnav at position ${index} to cdnavReference`)
return {
_type: 'cdnavReference',
_key: component._key || `nav-${Date.now()}`,
globalComponentSource: {
_type: 'reference',
_ref: navId
}
}
}

if (component._type === 'cdfooter') {
console.log(` ✏️ Converting cdfooter at position ${index} to cdfooterReference`)
return {
_type: 'cdfooterReference',
_key: component._key || `footer-${Date.now()}`,
globalComponentSource: {
_type: 'reference',
_ref: footerId
}
}
}

return component
})

// Update the document
try {
await client
.patch(doc._id)
.set({ components: updatedComponents })
.commit()

console.log(` ✅ Successfully updated ${doc._type}: ${doc.title || doc._id}`)
} catch (error) {
console.error(` ❌ Failed to update ${doc._id}:`, error.message)
}
}

console.log('\n✨ Migration complete!')
console.log('\n📝 Next steps:')
console.log(' 1. Rebuild your project: pnpm build')
console.log(' 2. Redeploy GraphQL API: pnpm sanity graphql deploy --force')
console.log(' 3. Test your site')
console.log(' 4. Remove temporary types from page.ts schema')

} catch (error) {
console.error('\n❌ Migration failed:', error)
console.error('\n💡 Make sure you have:')
console.error(' - Valid Sanity API token in SANITY_API_TOKEN')
console.error(' - Correct project ID and dataset')
console.error(' - Created global navigation and footer components')
}
}

// Run the migration
migrateComponents()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"test": "jest",
"chromatic": "npx chromatic --project-token=chpt_219e0bf9f15c669",
"postbuild": "find dist -name '*.js' -exec sed -i 's/\\.css\\.js/\\.css/g' {} +"
},
},
"dependencies": {
"@babel/cli": "7.23.9",
"@babel/core": "7.24.0",
Expand All @@ -48,6 +48,7 @@
"@fortawesome/free-solid-svg-icons": "^6.7.2",
"@fortawesome/react-fontawesome": "^0.2.2",
"@portabletext/react": "^3.0.0",
"@portabletext/to-html": "^2.0.14",
"@radix-ui/colors": "^0.1.8",
"@radix-ui/react-accessible-icon": "^1.1.0",
"@radix-ui/react-accordion": "^1.2.0",
Expand Down
75 changes: 75 additions & 0 deletions remove-old-components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Script to remove old cdnav/cdfooter components from pages
const sanityClient = require('@sanity/client')
require('dotenv').config({ path: '../../.env.local' })

// Configure Sanity client
const client = sanityClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || process.env.SANITY_STUDIO_PROJECT_ID,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
apiVersion: '2023-01-01',
token: process.env.SANITY_API_TOKEN,
useCdn: false
})

async function removeOldComponents() {
try {
console.log('🚀 Starting cleanup of old components...\n')

// Fetch all pages and homepage with old components
const documents = await client.fetch(`
*[_type in ["page", "homepage"] && defined(components) && count(components[_type in ["cdnav", "cdfooter"]]) > 0]{
_id,
_type,
_rev,
title,
"oldComponentCount": count(components[_type in ["cdnav", "cdfooter"]]),
components
}
`)

console.log(`📄 Found ${documents.length} documents with old components\n`)

for (const doc of documents) {
console.log(`\n🔄 Processing ${doc._type}: ${doc.title || doc._id}`)
console.log(` Found ${doc.oldComponentCount} old components to remove`)

// Filter out old components
const updatedComponents = doc.components.filter(component => {
if (component._type === 'cdnav' || component._type === 'cdfooter') {
console.log(` ❌ Removing ${component._type}`)
return false
}
return true
})

// Update the document
try {
await client
.patch(doc._id)
.set({ components: updatedComponents })
.commit()

console.log(` ✅ Successfully cleaned ${doc._type}: ${doc.title || doc._id}`)
console.log(` 📊 Components: ${doc.components.length} → ${updatedComponents.length}`)
} catch (error) {
console.error(` ❌ Failed to update ${doc._id}:`, error.message)
}
}

console.log('\n✨ Cleanup complete!')
console.log('\n📝 Next steps:')
console.log(' 1. Go to Sanity Studio')
console.log(' 2. Add new Navigation and Footer reference components to your pages')
console.log(' 3. Select the global components you created')
console.log(' 4. Publish the changes')

} catch (error) {
console.error('\n❌ Cleanup failed:', error)
console.error('\n💡 Make sure you have:')
console.error(' - Valid Sanity API token in SANITY_API_TOKEN')
console.error(' - Correct project ID and dataset')
}
}

// Run the cleanup
removeOldComponents()
82 changes: 82 additions & 0 deletions remove-old-components.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Script to remove old cdnav/cdfooter components from pages
import { createClient } from '@sanity/client'
import dotenv from 'dotenv'
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'

// Load environment variables
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
dotenv.config({ path: join(__dirname, '../../.env.local') })

// Configure Sanity client
const client = createClient({
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || process.env.SANITY_STUDIO_PROJECT_ID,
dataset: process.env.NEXT_PUBLIC_SANITY_DATASET || 'production',
apiVersion: '2023-01-01',
token: process.env.SANITY_API_TOKEN,
useCdn: false
})

async function removeOldComponents() {
try {
console.log('🚀 Starting cleanup of old components...\n')

// Fetch all pages and homepage with old components
const documents = await client.fetch(`
*[_type in ["page", "homepage"] && defined(components) && count(components[_type in ["cdnav", "cdfooter"]]) > 0]{
_id,
_type,
_rev,
title,
"oldComponentCount": count(components[_type in ["cdnav", "cdfooter"]]),
components
}
`)

console.log(`📄 Found ${documents.length} documents with old components\n`)

for (const doc of documents) {
console.log(`\n🔄 Processing ${doc._type}: ${doc.title || doc._id}`)
console.log(` Found ${doc.oldComponentCount} old components to remove`)

// Filter out old components
const updatedComponents = doc.components.filter(component => {
if (component._type === 'cdnav' || component._type === 'cdfooter') {
console.log(` ❌ Removing ${component._type}`)
return false
}
return true
})

// Update the document
try {
await client
.patch(doc._id)
.set({ components: updatedComponents })
.commit()

console.log(` ✅ Successfully cleaned ${doc._type}: ${doc.title || doc._id}`)
console.log(` 📊 Components: ${doc.components.length} → ${updatedComponents.length}`)
} catch (error) {
console.error(` ❌ Failed to update ${doc._id}:`, error.message)
}
}

console.log('\n✨ Cleanup complete!')
console.log('\n📝 Next steps:')
console.log(' 1. Go to Sanity Studio')
console.log(' 2. Add new Navigation and Footer reference components to your pages')
console.log(' 3. Select the global components you created')
console.log(' 4. Publish the changes')

} catch (error) {
console.error('\n❌ Cleanup failed:', error)
console.error('\n💡 Make sure you have:')
console.error(' - Valid Sanity API token in SANITY_API_TOKEN')
console.error(' - Correct project ID and dataset')
}
}

// Run the cleanup
removeOldComponents()
Loading
Loading