import client, { ensureAuthenticated } from '../lib/directus'; import { deleteCollection, deleteFile, readFiles, updateSettings, uploadFiles } from '@directus/sdk'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; // Helper for ESM __dirname const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); async function revertAndRestoreBranding() { console.log('đ¨ REVERTING EVERYTHING - RESTORING BRANDING ONLY đ¨'); await ensureAuthenticated(); // 1. DELETE ALL COLLECTIONS const collectionsToDelete = [ 'categories_link', 'categories_translations', 'categories', 'products_translations', 'products', 'posts_translations', 'posts', 'pages_translations', 'pages', 'globals_translations', 'globals' ]; console.log('đī¸ Deleting custom collections...'); for (const col of collectionsToDelete) { try { await client.request(deleteCollection(col)); console.log(`â Deleted collection: ${col}`); } catch (e: any) { console.log(`âšī¸ Collection ${col} not found or already deleted.`); } } // 2. DELETE ALL FILES console.log('đī¸ Deleting ALL files...'); try { const files = await client.request(readFiles({ limit: -1 })); if (files && files.length > 0) { const ids = files.map(f => f.id); await client.request(deleteFile(ids)); // Batch delete if supported by SDK version, else loop console.log(`â Deleted ${ids.length} files.`); } else { console.log('âšī¸ No files to delete.'); } } catch (e: any) { // Fallback to loop if batch fails try { const files = await client.request(readFiles({ limit: -1 })); for (const f of files) { await client.request(deleteFile(f.id)); } console.log(`â Deleted files individually.`); } catch (err) { } } // 3. RESTORE BRANDING (Exact copy of setup-directus-branding.ts logic) console.log('đ¨ Restoring Premium Branding...'); try { const getMimeType = (filePath: string) => { const ext = path.extname(filePath).toLowerCase(); switch (ext) { case '.svg': return 'image/svg+xml'; case '.png': return 'image/png'; case '.jpg': case '.jpeg': return 'image/jpeg'; case '.ico': return 'image/x-icon'; default: return 'application/octet-stream'; } }; const uploadAsset = async (filePath: string, title: string) => { if (!fs.existsSync(filePath)) { console.warn(`â ī¸ File not found: ${filePath}`); return null; } const mimeType = getMimeType(filePath); const form = new FormData(); const fileBuffer = fs.readFileSync(filePath); const blob = new Blob([fileBuffer], { type: mimeType }); form.append('file', blob, path.basename(filePath)); form.append('title', title); const res = await client.request(uploadFiles(form)); return res.id; }; const logoWhiteId = await uploadAsset(path.resolve(__dirname, '../public/logo-white.svg'), 'Logo White'); const logoBlueId = await uploadAsset(path.resolve(__dirname, '../public/logo-blue.svg'), 'Logo Blue'); const faviconId = await uploadAsset(path.resolve(__dirname, '../public/favicon.ico'), 'Favicon'); // Smoother Background SVG const bgSvgPath = path.resolve(__dirname, '../public/login-bg.svg'); fs.writeFileSync(bgSvgPath, ``); const backgroundId = await uploadAsset(bgSvgPath, 'Login Bg'); if (fs.existsSync(bgSvgPath)) fs.unlinkSync(bgSvgPath); // Update Settings const COLOR_PRIMARY = '#001a4d'; const COLOR_ACCENT = '#82ed20'; const COLOR_SECONDARY = '#003d82'; const cssInjection = `
KLZ INFRASTRUCTURE ENGINE