mediahub-fe/scripts/setup-tinymce.js

165 lines
4.7 KiB
JavaScript

#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const readline = require('readline');
console.log('🔑 TinyMCE API Key Setup');
console.log('========================\n');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Check if .env.local exists
const envPath = path.join(process.cwd(), '.env.local');
const envExists = fs.existsSync(envPath);
console.log('📋 Current Status:');
console.log(`- .env.local file: ${envExists ? '✅ Found' : '❌ Not found'}`);
if (envExists) {
const envContent = fs.readFileSync(envPath, 'utf8');
const hasApiKey = envContent.includes('NEXT_PUBLIC_TINYMCE_API_KEY');
console.log(`- API key configured: ${hasApiKey ? '✅ Yes' : '❌ No'}`);
}
console.log('\n🎯 Setup Options:');
console.log('1. Add API key to .env.local');
console.log('2. Create new .env.local file');
console.log('3. Check current configuration');
console.log('4. Exit\n');
rl.question('Choose an option (1-4): ', (choice) => {
switch (choice.trim()) {
case '1':
addApiKeyToEnv();
break;
case '2':
createNewEnvFile();
break;
case '3':
checkConfiguration();
break;
case '4':
console.log('👋 Setup cancelled');
rl.close();
break;
default:
console.log('❌ Invalid choice');
rl.close();
}
});
function addApiKeyToEnv() {
rl.question('\n🔑 Enter your TinyMCE API key: ', (apiKey) => {
if (!apiKey.trim()) {
console.log('❌ API key cannot be empty');
rl.close();
return;
}
try {
let envContent = '';
if (envExists) {
envContent = fs.readFileSync(envPath, 'utf8');
// Check if API key already exists
if (envContent.includes('NEXT_PUBLIC_TINYMCE_API_KEY')) {
// Replace existing key
envContent = envContent.replace(
/NEXT_PUBLIC_TINYMCE_API_KEY=.*/g,
`NEXT_PUBLIC_TINYMCE_API_KEY=${apiKey}`
);
} else {
// Add new key
envContent += `\nNEXT_PUBLIC_TINYMCE_API_KEY=${apiKey}`;
}
} else {
envContent = `NEXT_PUBLIC_TINYMCE_API_KEY=${apiKey}`;
}
fs.writeFileSync(envPath, envContent);
console.log('✅ API key added to .env.local');
console.log('🔄 Please restart your development server');
} catch (error) {
console.error('❌ Error writing to .env.local:', error.message);
}
rl.close();
});
}
function createNewEnvFile() {
rl.question('\n🔑 Enter your TinyMCE API key: ', (apiKey) => {
if (!apiKey.trim()) {
console.log('❌ API key cannot be empty');
rl.close();
return;
}
try {
const envContent = `# TinyMCE Configuration
NEXT_PUBLIC_TINYMCE_API_KEY=${apiKey}
# Other environment variables can be added here
`;
fs.writeFileSync(envPath, envContent);
console.log('✅ Created .env.local with API key');
console.log('🔄 Please restart your development server');
} catch (error) {
console.error('❌ Error creating .env.local:', error.message);
}
rl.close();
});
}
function checkConfiguration() {
console.log('\n📊 Configuration Check:');
// Check .env.local
if (envExists) {
const envContent = fs.readFileSync(envPath, 'utf8');
const apiKeyMatch = envContent.match(/NEXT_PUBLIC_TINYMCE_API_KEY=(.+)/);
if (apiKeyMatch) {
const apiKey = apiKeyMatch[1];
const maskedKey = apiKey.substring(0, 8) + '...' + apiKey.substring(apiKey.length - 4);
console.log(`✅ API key found: ${maskedKey}`);
} else {
console.log('❌ API key not found in .env.local');
}
} else {
console.log('❌ .env.local file not found');
}
// Check package.json
const packagePath = path.join(process.cwd(), 'package.json');
if (fs.existsSync(packagePath)) {
const packageJson = JSON.parse(fs.readFileSync(packagePath, 'utf8'));
const hasTinyMCE = packageJson.dependencies && packageJson.dependencies['@tinymce/tinymce-react'];
console.log(`✅ TinyMCE package: ${hasTinyMCE ? 'Installed' : 'Not installed'}`);
}
// Check editor components
const editorPath = path.join(process.cwd(), 'components', 'editor');
if (fs.existsSync(editorPath)) {
const files = fs.readdirSync(editorPath);
const editorFiles = files.filter(file => file.includes('editor'));
console.log(`✅ Editor components: ${editorFiles.length} found`);
}
console.log('\n📝 Next steps:');
console.log('1. Get API key from https://www.tiny.cloud/auth/signup/');
console.log('2. Add to .env.local file');
console.log('3. Restart development server');
console.log('4. Test editor functionality');
rl.close();
}