/** * Holt App-Symbol und Benachrichtigungssymbole vom Server (aus Logo und Markenfarbe * in der Redaktion) und erzeugt daraus alle Größen für iOS und Android. * npm run assets * Optional: ASSET_URL=http://localhost:5173 npm run assets (andere Quelle als APP_URL) */ import fs from 'node:fs'; import path from 'node:path'; import { execFileSync } from 'node:child_process'; import { fileURLToPath } from 'node:url'; import sharp from 'sharp'; import { loadEnv } from '../env.mjs'; const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..'); const env = loadEnv(); const base = (process.env.ASSET_URL || env.appUrl).replace(/\/+$/, ''); async function download(url) { const res = await fetch(url); if (!res.ok) throw new Error(`${url} → ${res.status}. Läuft die Web-App unter dieser Adresse?`); return Buffer.from(await res.arrayBuffer()); } // 1) Symbol in voller Größe (randlos, für App-Symbol und Startbildschirm) fs.mkdirSync(path.join(root, 'assets'), { recursive: true }); const icon = await download(`${base}/app-icon/1024?maskable`); fs.writeFileSync(path.join(root, 'assets', 'logo.png'), icon); // Hintergrundfarbe aus der Ecke des Symbols lesen (Markenfarbe oder Weiß bei hochgeladenem Logo) const { data } = await sharp(icon).extract({ left: 2, top: 2, width: 1, height: 1 }).raw().toBuffer({ resolveWithObject: true }); const bg = `#${[data[0], data[1], data[2]].map((v) => v.toString(16).padStart(2, '0')).join('')}`; console.log(`Hintergrundfarbe: ${bg}`); // 2) Android-Benachrichtigungssymbol (weiß auf transparent) in allen Dichten const densities = { mdpi: 24, hdpi: 36, xhdpi: 48, xxhdpi: 72, xxxhdpi: 96 }; if (fs.existsSync(path.join(root, 'android'))) { for (const [density, size] of Object.entries(densities)) { const dir = path.join(root, 'android', 'app', 'src', 'main', 'res', `drawable-${density}`); fs.mkdirSync(dir, { recursive: true }); fs.writeFileSync(path.join(dir, 'ic_stat_notify.png'), await download(`${base}/app-icon/${size}?notification`)); } console.log('✔ Android-Benachrichtigungssymbol'); } // 3) Alle Symbole und Startbildschirme erzeugen execFileSync( 'npx', [ 'capacitor-assets', 'generate', '--ios', '--android', '--iconBackgroundColor', bg, '--iconBackgroundColorDark', bg, '--splashBackgroundColor', env.brandColor, '--splashBackgroundColorDark', env.brandColor, '--logoSplashScale', '0.25' ], { cwd: root, stdio: 'inherit' } ); console.log('✔ Symbole und Startbildschirme erzeugt. Danach `npm run sync`.');