import { GluegunMenuToolbox } from '@lenne.tech/gluegun-menu'; import chalk = require('chalk'); import { exportDatabaseSchema } from '../../../common/schema-exporter'; const clipboardy = require('clipboardy'); module.exports = { name: 'exportschema', alias: ['es'], description: 'Export PostgreSQL database schema (es)', hidden: false, run: async (toolbox: GluegunMenuToolbox) => { const { print } = toolbox; print.info(chalk.cyan('🗄️ Exporting Database Schema...')); print.info(''); // Hardcoded values as per requirements const dbName = 'photowall'; const dbUser = 'photowall'; const dbHost = 'localhost'; const dbPort = 5432; print.info(chalk.gray(`Database: ${dbName}`)); print.info(chalk.gray(`User: ${dbUser}`)); print.info(chalk.gray(`Host: ${dbHost}:${dbPort}`)); print.info( chalk.gray( 'Auth: Using PostgreSQL default methods (.pgpass or PGPASSWORD)', ), ); print.info(''); // Check for .pgpass file const fs = require('fs'); const os = require('os'); const path = require('path'); const pgpassPath = path.join(os.homedir(), '.pgpass'); if (!fs.existsSync(pgpassPath)) { print.warning( chalk.yellow(`⚠️ Warning: .pgpass file not found at ${pgpassPath}`), ); print.info( chalk.gray( ' Ensure PGPASSWORD environment variable is set or .pgpass exists.', ), ); print.info(''); } const spinner = print.spin('Connecting to database...'); try { const result = await exportDatabaseSchema( dbName, dbUser, dbHost, dbPort, '/tmp', ); spinner.stop(); if (result.success && result.outputPath) { // Read the exported file and copy to clipboard try { const schemaContent = fs.readFileSync(result.outputPath, 'utf8'); await clipboardy.write(schemaContent); print.success( chalk.green( `✅ Schema exported successfully to: ${result.outputPath}`, ), ); print.success(chalk.green('📋 Schema has been copied to clipboard!')); } catch (clipError) { print.success( chalk.green( `✅ Schema exported successfully to: ${result.outputPath}`, ), ); print.warning( chalk.yellow( `⚠️ Could not copy to clipboard: ${clipError instanceof Error ? clipError.message : String(clipError)}`, ), ); } print.info(''); print.info( chalk.cyan( '💡 Use this schema file as context for AI-assisted database queries', ), ); } else { print.error(chalk.red(`❌ Export failed: ${result.error}`)); print.info(''); if ( result.error && result.error.includes('password authentication failed') ) { print.info(chalk.yellow('💡 Troubleshooting steps:')); print.info( chalk.gray( ` 1. Add an entry to ~/.pgpass file (format: ${dbHost}:${dbPort}:${dbName}:${dbUser}:password)`, ), ); print.info( chalk.gray( ` 2. Or set environment variable: export PGPASSWORD=your_password`, ), ); print.info( chalk.gray( ` 3. Or use trust authentication in PostgreSQL's pg_hba.conf`, ), ); print.info(''); print.info( chalk.gray( ` Current .pgpass location: ${pgpassPath}${fs.existsSync(pgpassPath) ? ' (exists)' : ' (not found)'}`, ), ); } } } catch (error) { spinner.stop(); print.error( chalk.red( `❌ Unexpected error: ${error instanceof Error ? error.message : String(error)}`, ), ); } // Return to the AI menu print.info(''); await toolbox.menu.showMenu('ai'); }, };