-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove-old-components.js
More file actions
75 lines (64 loc) · 2.61 KB
/
Copy pathremove-old-components.js
File metadata and controls
75 lines (64 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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()