Files
portfolio/scripts/migrate-hobbies-to-directus.js
denshooter e431ff50fc feat: Add Directus setup scripts for collections, fields, and relations
- Created setup-directus-collections.js to automate the creation of tech stack collections, fields, and relations in Directus.
- Created setup-directus-hobbies.js for setting up hobbies collection with translations.
- Created setup-directus-projects.js for establishing projects collection with comprehensive fields and translations.
- Added setup-tech-stack-directus.js to populate tech_stack_items with predefined data.
2026-01-23 02:53:31 +01:00

186 lines
4.7 KiB
JavaScript

#!/usr/bin/env node
/**
* Migrate Hobbies to Directus
*
* Migriert Hobbies-Daten aus messages/en.json und messages/de.json nach Directus
*
* Usage:
* node scripts/migrate-hobbies-to-directus.js
*/
const fetch = require('node-fetch');
const fs = require('fs');
const path = require('path');
require('dotenv').config();
const DIRECTUS_URL = process.env.DIRECTUS_URL || 'https://cms.dk0.dev';
const DIRECTUS_TOKEN = process.env.DIRECTUS_STATIC_TOKEN;
if (!DIRECTUS_TOKEN) {
console.error('❌ Error: DIRECTUS_STATIC_TOKEN not found in .env');
process.exit(1);
}
const messagesEn = JSON.parse(
fs.readFileSync(path.join(__dirname, '../messages/en.json'), 'utf-8')
);
const messagesDe = JSON.parse(
fs.readFileSync(path.join(__dirname, '../messages/de.json'), 'utf-8')
);
const hobbiesEn = messagesEn.home.about.hobbies;
const hobbiesDe = messagesDe.home.about.hobbies;
const HOBBIES_DATA = [
{
key: 'self_hosting',
icon: 'Code',
titleEn: hobbiesEn.selfHosting,
titleDe: hobbiesDe.selfHosting
},
{
key: 'gaming',
icon: 'Gamepad2',
titleEn: hobbiesEn.gaming,
titleDe: hobbiesDe.gaming
},
{
key: 'game_servers',
icon: 'Server',
titleEn: hobbiesEn.gameServers,
titleDe: hobbiesDe.gameServers
},
{
key: 'jogging',
icon: 'Activity',
titleEn: hobbiesEn.jogging,
titleDe: hobbiesDe.jogging
}
];
async function directusRequest(endpoint, method = 'GET', body = null) {
const url = `${DIRECTUS_URL}/${endpoint}`;
const options = {
method,
headers: {
'Authorization': `Bearer ${DIRECTUS_TOKEN}`,
'Content-Type': 'application/json'
}
};
if (body) {
options.body = JSON.stringify(body);
}
try {
const response = await fetch(url, options);
if (!response.ok) {
const text = await response.text();
throw new Error(`HTTP ${response.status}: ${text}`);
}
return await response.json();
} catch (error) {
console.error(`Error calling ${method} ${endpoint}:`, error.message);
throw error;
}
}
async function migrateHobbies() {
console.log('\n📦 Migrating Hobbies to Directus...\n');
for (const hobby of HOBBIES_DATA) {
console.log(`\n🎮 Hobby: ${hobby.key}`);
try {
// 1. Create Hobby
console.log(' Creating hobby...');
const hobbyData = {
key: hobby.key,
icon: hobby.icon,
status: 'published',
sort: HOBBIES_DATA.indexOf(hobby) + 1
};
const { data: createdHobby } = await directusRequest(
'items/hobbies',
'POST',
hobbyData
);
console.log(` ✅ Hobby created with ID: ${createdHobby.id}`);
// 2. Create Translations
console.log(' Creating translations...');
// English Translation
await directusRequest(
'items/hobbies_translations',
'POST',
{
hobbies_id: createdHobby.id,
languages_code: 'en-US',
title: hobby.titleEn
}
);
// German Translation
await directusRequest(
'items/hobbies_translations',
'POST',
{
hobbies_id: createdHobby.id,
languages_code: 'de-DE',
title: hobby.titleDe
}
);
console.log(' ✅ Translations created (en-US, de-DE)');
} catch (error) {
console.error(` ❌ Error migrating ${hobby.key}:`, error.message);
}
}
console.log('\n✨ Migration complete!\n');
}
async function verifyMigration() {
console.log('\n🔍 Verifying Migration...\n');
try {
const { data: hobbies } = await directusRequest(
'items/hobbies?fields=key,icon,status,translations.title,translations.languages_code'
);
console.log(`✅ Found ${hobbies.length} hobbies in Directus:`);
hobbies.forEach(h => {
const enTitle = h.translations?.find(t => t.languages_code === 'en-US')?.title;
console.log(` - ${h.key}: "${enTitle}"`);
});
console.log('\n🎉 Hobbies successfully migrated!\n');
console.log('Next steps:');
console.log(' 1. Visit: https://cms.dk0.dev/admin/content/hobbies');
console.log(' 2. Update About.tsx to load hobbies from Directus\n');
} catch (error) {
console.error('❌ Verification failed:', error.message);
}
}
async function main() {
console.log('\n╔════════════════════════════════════════╗');
console.log('║ Hobbies Migration to Directus ║');
console.log('╚════════════════════════════════════════╝\n');
try {
await migrateHobbies();
await verifyMigration();
} catch (error) {
console.error('\n❌ Migration failed:', error);
process.exit(1);
}
}
main();