209 lines
8.4 KiB
TypeScript
209 lines
8.4 KiB
TypeScript
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, `<svg width="1920" height="1080" viewBox="0 0 1920 1080" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||
<rect width="1920" height="1080" fill="#001a4d"/>
|
||
<ellipse cx="960" cy="540" rx="800" ry="600" fill="url(#paint0_radial_premium)"/>
|
||
<defs>
|
||
<radialGradient id="paint0_radial_premium" cx="0" cy="0" r="1" gradientUnits="userSpaceOnUse" gradientTransform="translate(960 540) rotate(90) scale(600 800)">
|
||
<stop stop-color="#003d82" stop-opacity="0.8"/>
|
||
<stop offset="1" stop-color="#001a4d" stop-opacity="0"/>
|
||
</radialGradient>
|
||
</defs>
|
||
</svg>`);
|
||
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 = `
|
||
<style>
|
||
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
|
||
|
||
body, .v-app {
|
||
font-family: 'Inter', sans-serif !important;
|
||
-webkit-font-smoothing: antialiased;
|
||
}
|
||
|
||
.public-view .v-card {
|
||
background: rgba(255, 255, 255, 0.95) !important;
|
||
backdrop-filter: blur(20px);
|
||
border: 1px solid rgba(255, 255, 255, 0.3) !important;
|
||
border-radius: 32px !important;
|
||
box-shadow: 0 50px 100px -20px rgba(0, 0, 0, 0.4) !important;
|
||
padding: 40px !important;
|
||
}
|
||
|
||
.public-view .v-button {
|
||
border-radius: 9999px !important;
|
||
height: 56px !important;
|
||
font-weight: 600 !important;
|
||
letter-spacing: -0.01em !important;
|
||
transition: all 0.4s cubic-bezier(0.16, 1, 0.3, 1) !important;
|
||
}
|
||
|
||
.public-view .v-button:hover {
|
||
transform: translateY(-2px);
|
||
box-shadow: 0 15px 30px rgba(130, 237, 32, 0.2) !important;
|
||
}
|
||
|
||
.public-view .v-input {
|
||
--v-input-border-radius: 12px !important;
|
||
--v-input-background-color: #f8f9fa !important;
|
||
}
|
||
</style>
|
||
<div style="font-family: 'Inter', sans-serif; text-align: center; margin-top: 24px;">
|
||
<p style="color: rgba(255,255,255,0.7); font-size: 14px; margin-bottom: 4px; font-weight: 500;">KLZ INFRASTRUCTURE ENGINE</p>
|
||
<h1 style="color: #ffffff; font-size: 18px; font-weight: 700; margin: 0;">Sustainable Energy. <span style="color: #82ed20;">Industrial Reliability.</span></h1>
|
||
</div>
|
||
`;
|
||
|
||
await client.request(updateSettings({
|
||
project_name: 'KLZ Cables',
|
||
project_url: 'https://klz-cables.com',
|
||
project_color: COLOR_ACCENT,
|
||
project_descriptor: 'Sustainable Energy Infrastructure',
|
||
project_owner: 'KLZ Cables',
|
||
project_logo: logoWhiteId as any,
|
||
public_foreground: logoWhiteId as any,
|
||
public_background: backgroundId as any,
|
||
public_note: cssInjection,
|
||
public_favicon: faviconId as any,
|
||
theme_light_overrides: {
|
||
"primary": COLOR_ACCENT,
|
||
"secondary": COLOR_SECONDARY,
|
||
"background": "#f1f3f7",
|
||
"backgroundNormal": "#ffffff",
|
||
"backgroundAccent": "#eef2ff",
|
||
"navigationBackground": COLOR_PRIMARY,
|
||
"navigationForeground": "#ffffff",
|
||
"navigationBackgroundHover": "rgba(255,255,255,0.05)",
|
||
"navigationForegroundHover": "#ffffff",
|
||
"navigationBackgroundActive": "rgba(130, 237, 32, 0.15)",
|
||
"navigationForegroundActive": COLOR_ACCENT,
|
||
"moduleBarBackground": "#000d26",
|
||
"moduleBarForeground": "#ffffff",
|
||
"moduleBarForegroundActive": COLOR_ACCENT,
|
||
"borderRadius": "16px",
|
||
"borderWidth": "1px",
|
||
"borderColor": "#e2e8f0",
|
||
"formFieldHeight": "48px"
|
||
} as any,
|
||
theme_dark_overrides: {
|
||
"primary": COLOR_ACCENT,
|
||
"background": "#0a0a0a",
|
||
"navigationBackground": "#000000",
|
||
"moduleBarBackground": COLOR_PRIMARY,
|
||
"borderRadius": "16px",
|
||
"formFieldHeight": "48px"
|
||
} as any
|
||
}));
|
||
|
||
console.log('✨ System Cleaned & Branding Restored Successfully');
|
||
|
||
} catch (error: any) {
|
||
console.error('❌ Error restoring branding:', JSON.stringify(error, null, 2));
|
||
}
|
||
}
|
||
|
||
revertAndRestoreBranding().catch(console.error);
|