/* eslint-disable @typescript-eslint/no-require-imports */ const fetch = require('node-fetch'); require('dotenv').config(); const DIRECTUS_URL = process.env.DIRECTUS_URL || 'https://cms.dk0.dev'; const DIRECTUS_TOKEN = process.env.DIRECTUS_STATIC_TOKEN; async function setupSnippets() { console.log('📦 Setting up Snippets collection...'); // 1. Create Collection try { await fetch(`${DIRECTUS_URL}/collections`, { method: 'POST', headers: { 'Authorization': `Bearer ${DIRECTUS_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ collection: 'snippets', meta: { icon: 'terminal', display_template: '{{title}}' }, schema: { name: 'snippets' } }) }); } catch (_e) {} // 2. Add Fields const fields = [ { field: 'status', type: 'string', meta: { interface: 'select-dropdown' }, schema: { default_value: 'published' } }, { field: 'title', type: 'string', meta: { interface: 'input' } }, { field: 'category', type: 'string', meta: { interface: 'input' } }, { field: 'code', type: 'text', meta: { interface: 'input-code' } }, { field: 'description', type: 'text', meta: { interface: 'textarea' } }, { field: 'language', type: 'string', meta: { interface: 'input' }, schema: { default_value: 'javascript' } }, { field: 'featured', type: 'boolean', meta: { interface: 'boolean' }, schema: { default_value: false } } ]; for (const f of fields) { try { await fetch(`${DIRECTUS_URL}/fields/snippets`, { method: 'POST', headers: { 'Authorization': `Bearer ${DIRECTUS_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify(f) }); } catch (_e) {} } // 3. Add Example Data const exampleSnippets = [ { title: 'Traefik SSL Config', category: 'Docker', language: 'yaml', featured: true, description: "Meine Standard-Konfiguration für automatisches SSL via Let's Encrypt in Docker Swarm.", code: "labels:\n - \"traefik.enable=true\"\n - \"traefik.http.routers.myapp.rule=Host(`example.com`)\"\n - \"traefik.http.routers.myapp.entrypoints=websecure\"\n - \"traefik.http.routers.myapp.tls.certresolver=myresolver\"" }, { title: 'Docker Cleanup Alias', category: 'ZSH', language: 'bash', featured: true, description: 'Ein einfacher Alias, um ungenutzte Docker-Container, Images und Volumes schnell zu entfernen.', code: "alias dclean='docker system prune -af --volumes'" } ]; for (const s of exampleSnippets) { try { await fetch(`${DIRECTUS_URL}/items/snippets`, { method: 'POST', headers: { 'Authorization': `Bearer ${DIRECTUS_TOKEN}`, 'Content-Type': 'application/json' }, body: JSON.stringify(s) }); } catch (_e) {} } console.log('✅ Snippets setup complete!'); } setupSnippets();