import { ContentGenerator, OptimizationOptions } from "@mintel/content-engine"; import * as fs from "node:fs/promises"; import * as path from "node:path"; import { fileURLToPath } from "node:url"; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); async function main() { const OPENROUTER_KEY = process.env.OPENROUTER_KEY; if (!OPENROUTER_KEY) { console.error("โŒ Error: OPENROUTER_KEY not found in environment."); process.exit(1); } const args = process.argv.slice(2); let targetFile = args[0]; if (!targetFile) { console.error("โŒ Usage: npx tsx scripts/optimize-blog-post.ts "); process.exit(1); } // Resolve absolute path if (!path.isAbsolute(targetFile)) { targetFile = path.resolve(process.cwd(), targetFile); } console.log(`๐Ÿ“„ Reading file: ${targetFile}`); let content = ""; try { content = await fs.readFile(targetFile, "utf8"); } catch (err: any) { console.error(`โŒ Could not read file: ${err.message}`); process.exit(1); } // Backup original const backupPath = `${targetFile}.bak`; await fs.writeFile(backupPath, content); console.log(`๐Ÿ’พ Backup created at: ${backupPath}`); // Instantiate Generator const generator = new ContentGenerator(OPENROUTER_KEY); const context = ` Project: Mintel.me Style: Industrial, Technical, High-Performance, "No-BS". Author: Marc Mintel (Digital Architect). Focus: Web Architecture, PageSpeed, Core Web Vitals, Data-Driven Design. `; // Define Optimization Options based on user request ("astrein verbessert mit daten gestรผtzt, links zu quellen usw... mermaid, memes") const options: OptimizationOptions = { enhanceFacts: true, addMemes: true, addDiagrams: true, projectContext: context, availableComponents: [ { name: "StatsDisplay", description: "A row of 3 clear statistic cards with values and labels.", usageExample: ` 3s" }, { value: "0.1s", label: "Latency", description: "Edge Network" } ]} />` }, { name: "ComparisonRow", description: "A comparison component showing a negative 'Standard' vs a positive 'Mintel' approach.", usageExample: `` } ] }; // 1. Separate Frontmatter from Body const fmMatch = content.match(/^---\n([\s\S]*?)\n---/); const frontmatter = fmMatch ? fmMatch[0] : ""; const body = fmMatch ? content.replace(frontmatter, "").trim() : content; console.log("๐Ÿš€ Starting Optimization via ContentEngine..."); const result = await generator.optimizePost(body, options); console.log("โœ… Optimization Complete!"); console.log(` - Added ${result.research.length} facts`); console.log(` - Added ${result.memes.length} meme concepts`); console.log(` - Generated ${result.diagrams.length} diagrams`); // We write the content back (re-attaching frontmatter if it was there) const finalContent = frontmatter ? `${frontmatter}\n\n${result.content}` : result.content; await fs.writeFile(targetFile, finalContent); console.log(`๐Ÿ’พ Saved optimized content to: ${targetFile}`); } main().catch(console.error);