#!/usr/bin/env node /** * CC Site Maker CLI Tool * * A simple command-line utility to generate HTML using the CC Site Maker API * Usage: node cli-generator.js "Your prompt" output.html */ import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; import readline from 'readline'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); // Default configuration - modify as needed const CONFIG = { apiUrl: process.env.API_URL || 'http://localhost:7860/api/external/ask-ai', apiKey: process.env.API_KEY || '' }; // Create readline interface for user input const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); /** * Generate HTML from prompt using the API * @param {string} prompt - The prompt to send to the AI * @param {string} apiKey - Your API key * @returns {Promise} The generated HTML */ async function generateHTML(prompt, apiKey) { console.log('Generating HTML...'); const response = await fetch(CONFIG.apiUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': apiKey }, body: JSON.stringify({ prompt: prompt }) }); if (!response.ok) { let errorMessage; try { const errorData = await response.json(); errorMessage = errorData.message; } catch (e) { errorMessage = `HTTP error! Status: ${response.status}`; } throw new Error(errorMessage || 'Failed to generate HTML'); } // For streamed responses const reader = response.body.getReader(); const decoder = new TextDecoder('utf-8'); let result = ''; let progress = 0; process.stdout.write('Receiving response: '); while (true) { const { done, value } = await reader.read(); if (done) break; result += decoder.decode(value, { stream: true }); progress += value.length; // Show some progress indicators process.stdout.write('.'); } process.stdout.write('\n'); return result; } /** * Save HTML to a file * @param {string} html - HTML content * @param {string} filename - Output filename */ function saveHTMLToFile(html, filename) { const outputPath = path.resolve(process.cwd(), filename); fs.writeFileSync(outputPath, html); console.log(`HTML saved to: ${outputPath}`); } /** * Main function */ async function main() { // Get command line arguments const args = process.argv.slice(2); let prompt = args[0]; let outputFile = args[1] || 'output.html'; // If no prompt provided, ask for it if (!prompt) { prompt = await new Promise((resolve) => { rl.question('Enter your prompt: ', (answer) => resolve(answer)); }); } // If no API key, ask for it if (!CONFIG.apiKey) { CONFIG.apiKey = await new Promise((resolve) => { rl.question('Enter your API key: ', (answer) => resolve(answer)); }); } try { const html = await generateHTML(prompt, CONFIG.apiKey); saveHTMLToFile(html, outputFile); } catch (error) { console.error('Error:', error.message); process.exit(1); } finally { rl.close(); } } // Run the main function main().catch((error) => { console.error('Unhandled error:', error); process.exit(1); });