179 lines
5.1 KiB
JavaScript
179 lines
5.1 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Interactive Setup Wizard
|
|
* Guides users through the complete setup process
|
|
*/
|
|
|
|
const readline = require('readline');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const { execSync } = require('child_process');
|
|
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
});
|
|
|
|
function question(query) {
|
|
return new Promise(resolve => rl.question(query, resolve));
|
|
}
|
|
|
|
function displayBanner() {
|
|
console.log('\n================================================');
|
|
console.log('🚀 Property Image Tagging API - Setup Wizard');
|
|
console.log('================================================\n');
|
|
}
|
|
|
|
async function checkEnvFile() {
|
|
const envPath = path.join(process.cwd(), '.env');
|
|
if (fs.existsSync(envPath)) {
|
|
console.log('✅ .env file found\n');
|
|
return true;
|
|
} else {
|
|
console.log('❌ .env file not found\n');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function setupEnvironment() {
|
|
console.log('📝 Environment Configuration\n');
|
|
|
|
const dbPassword = await question('Enter MySQL root password (press Enter if no password): ');
|
|
const anthropicKey = await question('Enter your Anthropic API key (or press Enter to skip): ');
|
|
const port = await question('Server port (default 3000): ') || '3000';
|
|
const skipAuth = await question('Skip authentication for development? (y/n, default: y): ');
|
|
|
|
const envContent = `# Environment Configuration
|
|
NODE_ENV=development
|
|
PORT=${port}
|
|
|
|
# Database Configuration
|
|
DB_HOST=localhost
|
|
DB_PORT=3306
|
|
DB_USER=root
|
|
DB_PASSWORD=${dbPassword}
|
|
DB_NAME=property_tagging
|
|
|
|
# Claude AI Configuration (REQUIRED)
|
|
# Get your API key from: https://console.anthropic.com/
|
|
ANTHROPIC_API_KEY=${anthropicKey}
|
|
|
|
# Authentication (Optional - for development only)
|
|
# Set to true to skip API key authentication during development
|
|
SKIP_AUTH=${skipAuth.toLowerCase() !== 'n'}
|
|
|
|
# Logging
|
|
LOG_LEVEL=info
|
|
`;
|
|
|
|
const envPath = path.join(process.cwd(), '.env');
|
|
fs.writeFileSync(envPath, envContent);
|
|
console.log('\n✅ .env file created\n');
|
|
}
|
|
|
|
async function setupDatabase() {
|
|
console.log('🗄️ Setting up database...\n');
|
|
|
|
try {
|
|
execSync('node scripts/setup-database.js', { stdio: 'inherit' });
|
|
return true;
|
|
} catch (error) {
|
|
console.log('\n❌ Database setup failed\n');
|
|
console.log('You can try setting it up manually later with: npm run db:setup\n');
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function createApiKey() {
|
|
const create = await question('\n🔑 Do you want to create an API key now? (y/n): ');
|
|
|
|
if (create.toLowerCase() === 'y') {
|
|
try {
|
|
console.log('\n');
|
|
execSync('node scripts/manage-api-keys.js create', { stdio: 'inherit' });
|
|
console.log('\n✅ API key created! Save it securely.\n');
|
|
} catch (error) {
|
|
console.log('\n❌ Failed to create API key\n');
|
|
console.log('You can create one later with: npm run apikey:create\n');
|
|
}
|
|
}
|
|
}
|
|
|
|
async function displayNextSteps(dbSetup) {
|
|
console.log('\n================================================');
|
|
console.log('✅ Setup Complete!');
|
|
console.log('================================================\n');
|
|
|
|
console.log('Next steps:\n');
|
|
|
|
if (!dbSetup) {
|
|
console.log('⚠️ Database setup incomplete. Run:');
|
|
console.log(' npm run db:setup\n');
|
|
}
|
|
|
|
console.log('🚀 Start the server:');
|
|
console.log(' npm start (production mode)');
|
|
console.log(' npm run dev (development mode with auto-reload)\n');
|
|
|
|
console.log('📚 Available commands:');
|
|
console.log(' npm run db:setup - Set up database');
|
|
console.log(' npm run apikey:create - Create API key');
|
|
console.log(' npm run apikey:list - List API keys');
|
|
console.log(' npm run apikey:revoke - Revoke API key\n');
|
|
|
|
console.log('📖 For more information, see SETUP_GUIDE.md\n');
|
|
|
|
console.log('🌐 The API will be available at: http://localhost:' + (process.env.PORT || '3000'));
|
|
console.log('');
|
|
}
|
|
|
|
async function main() {
|
|
try {
|
|
displayBanner();
|
|
|
|
const envExists = await checkEnvFile();
|
|
|
|
if (!envExists) {
|
|
const create = await question('Would you like to create it now? (y/n): ');
|
|
if (create.toLowerCase() === 'y') {
|
|
await setupEnvironment();
|
|
} else {
|
|
console.log('\nSetup cancelled. Please create a .env file manually.\n');
|
|
console.log('You can copy .env.example and fill in the values:\n');
|
|
console.log(' cp .env.example .env\n');
|
|
rl.close();
|
|
return;
|
|
}
|
|
} else {
|
|
// Load existing .env
|
|
require('dotenv').config();
|
|
|
|
const update = await question('Would you like to update the .env configuration? (y/n): ');
|
|
if (update.toLowerCase() === 'y') {
|
|
await setupEnvironment();
|
|
// Reload after update
|
|
require('dotenv').config();
|
|
}
|
|
}
|
|
|
|
const dbSetup = await setupDatabase();
|
|
|
|
if (dbSetup) {
|
|
await createApiKey();
|
|
}
|
|
|
|
await displayNextSteps(dbSetup);
|
|
|
|
} catch (error) {
|
|
console.error('\n❌ Setup failed:', error.message);
|
|
console.error('\nPlease check the error and try again.\n');
|
|
} finally {
|
|
rl.close();
|
|
}
|
|
}
|
|
|
|
// Run the wizard
|
|
main();
|
|
|