|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +function loadLocalEnvFile() { |
| 5 | + const envPath = path.resolve(__dirname, '../.env.e2e.local'); |
| 6 | + if (!fs.existsSync(envPath)) return; |
| 7 | + const lines = fs.readFileSync(envPath, 'utf8').split(/\r?\n/); |
| 8 | + for (const rawLine of lines) { |
| 9 | + const line = rawLine.trim(); |
| 10 | + if (!line || line.startsWith('#')) continue; |
| 11 | + const eq = line.indexOf('='); |
| 12 | + if (eq <= 0) continue; |
| 13 | + const key = line.slice(0, eq).trim(); |
| 14 | + const value = line.slice(eq + 1).trim(); |
| 15 | + process.env[key] = value; |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +loadLocalEnvFile(); |
| 20 | + |
| 21 | +const GEMINI_API_KEY = process.env.GEMINI_API_KEY || ''; |
| 22 | +const MODEL = process.env.GEMINI_IMAGE_MODEL || 'gemini-2.0-flash-exp-image-generation'; |
| 23 | + |
| 24 | +if (!GEMINI_API_KEY) { |
| 25 | + console.error('GEMINI_API_KEY missing (.env.e2e.local)'); |
| 26 | + process.exit(1); |
| 27 | +} |
| 28 | + |
| 29 | +const prompt = [ |
| 30 | + 'Create a polished visual abstract hero image for a Chrome extension named Web2Comics.', |
| 31 | + 'Wide banner composition, clean modern flat illustration, friendly and professional.', |
| 32 | + 'Show a left-to-right workflow with 4 stages:', |
| 33 | + '1) a web page/article in a browser tab,', |
| 34 | + '2) an AI processing/storyboard stage (robot/AI spark icon),', |
| 35 | + '3) comic strip panels being generated,', |
| 36 | + '4) a side panel viewer and an exported PNG image.', |
| 37 | + 'Use bright colors, strong contrast, white or very light background, subtle gradients.', |
| 38 | + 'No logos from other companies. No watermarks. No tiny unreadable text.', |
| 39 | + 'If text is included, keep it minimal and legible: "Web Page", "AI", "Comic", "Export".', |
| 40 | + 'Style should feel inviting for a README visual abstract.', |
| 41 | + '16:9 landscape.' |
| 42 | +].join(' '); |
| 43 | + |
| 44 | +async function main() { |
| 45 | + const url = `https://generativelanguage.googleapis.com/v1beta/models/${MODEL}:generateContent?key=${GEMINI_API_KEY}`; |
| 46 | + const res = await fetch(url, { |
| 47 | + method: 'POST', |
| 48 | + headers: { 'Content-Type': 'application/json' }, |
| 49 | + body: JSON.stringify({ |
| 50 | + contents: [{ parts: [{ text: prompt }] }], |
| 51 | + generationConfig: { |
| 52 | + responseModalities: ['image', 'text'], |
| 53 | + maxOutputTokens: 1024 |
| 54 | + } |
| 55 | + }) |
| 56 | + }); |
| 57 | + |
| 58 | + const raw = await res.text(); |
| 59 | + let json = null; |
| 60 | + try { |
| 61 | + json = raw ? JSON.parse(raw) : null; |
| 62 | + } catch (_) {} |
| 63 | + |
| 64 | + if (!res.ok) { |
| 65 | + console.error(`Gemini request failed: ${res.status}`); |
| 66 | + console.error(raw.slice(0, 2000)); |
| 67 | + process.exit(1); |
| 68 | + } |
| 69 | + |
| 70 | + const parts = json?.candidates?.[0]?.content?.parts || []; |
| 71 | + const imagePart = parts.find((p) => p?.inlineData?.data); |
| 72 | + if (!imagePart) { |
| 73 | + console.error('No image returned by Gemini.'); |
| 74 | + console.error(JSON.stringify(json, null, 2).slice(0, 4000)); |
| 75 | + process.exit(1); |
| 76 | + } |
| 77 | + |
| 78 | + const mimeType = imagePart.inlineData?.mimeType || 'image/png'; |
| 79 | + const ext = mimeType.includes('jpeg') ? 'jpg' : 'png'; |
| 80 | + const outDir = path.resolve(__dirname, '../docs'); |
| 81 | + fs.mkdirSync(outDir, { recursive: true }); |
| 82 | + |
| 83 | + const outImage = path.join(outDir, `visual-abstract-gemini.${ext}`); |
| 84 | + fs.writeFileSync(outImage, Buffer.from(imagePart.inlineData.data, 'base64')); |
| 85 | + |
| 86 | + const outMeta = path.join(outDir, 'visual-abstract-gemini.json'); |
| 87 | + fs.writeFileSync( |
| 88 | + outMeta, |
| 89 | + JSON.stringify( |
| 90 | + { |
| 91 | + generatedAt: new Date().toISOString(), |
| 92 | + provider: 'gemini', |
| 93 | + model: MODEL, |
| 94 | + prompt, |
| 95 | + mimeType, |
| 96 | + textParts: parts.filter((p) => p?.text).map((p) => p.text) |
| 97 | + }, |
| 98 | + null, |
| 99 | + 2 |
| 100 | + ) |
| 101 | + ); |
| 102 | + |
| 103 | + console.log(`Saved image: ${path.relative(path.resolve(__dirname, '..'), outImage)}`); |
| 104 | + console.log(`Saved metadata: ${path.relative(path.resolve(__dirname, '..'), outMeta)}`); |
| 105 | +} |
| 106 | + |
| 107 | +main().catch((error) => { |
| 108 | + console.error(error); |
| 109 | + process.exit(1); |
| 110 | +}); |
0 commit comments