|
| 1 | +const { exec } = require('child_process'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const readline = require('readline'); |
| 5 | + |
| 6 | +// Create readline interface |
| 7 | +const rl = readline.createInterface({ |
| 8 | + input: process.stdin, |
| 9 | + output: process.stdout |
| 10 | +}); |
| 11 | + |
| 12 | +// Function to run a command |
| 13 | +function runCommand(command) { |
| 14 | + return new Promise((resolve, reject) => { |
| 15 | + exec(command, (error, stdout, stderr) => { |
| 16 | + if (error) { |
| 17 | + console.error(`Error: ${error.message}`); |
| 18 | + return reject(error); |
| 19 | + } |
| 20 | + resolve({ stdout, stderr }); |
| 21 | + }); |
| 22 | + }); |
| 23 | +} |
| 24 | + |
| 25 | +// Function to check if a file exists |
| 26 | +function fileExists(filePath) { |
| 27 | + return fs.existsSync(path.join(__dirname, '..', filePath)); |
| 28 | +} |
| 29 | + |
| 30 | +// Function to check environment variables |
| 31 | +async function checkEnvironment() { |
| 32 | + console.log('\n📋 Checking environment variables...'); |
| 33 | + try { |
| 34 | + const { stdout } = await runCommand('npm run check:env'); |
| 35 | + console.log(stdout); |
| 36 | + return !stdout.includes('Missing') && !stdout.includes('Empty'); |
| 37 | + } catch (error) { |
| 38 | + console.error('Failed to check environment variables.'); |
| 39 | + return false; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Function to check database connection |
| 44 | +async function checkDatabase() { |
| 45 | + console.log('\n📋 Checking database connection...'); |
| 46 | + |
| 47 | + // Check if .env file exists and has database configuration |
| 48 | + const envPath = path.join(__dirname, '../.env'); |
| 49 | + if (!fs.existsSync(envPath)) { |
| 50 | + console.error('❌ .env file not found. Please run npm run setup first.'); |
| 51 | + return false; |
| 52 | + } |
| 53 | + |
| 54 | + try { |
| 55 | + // Create a simple test script to check database connection |
| 56 | + const testScript = ` |
| 57 | + const { Pool } = require('pg'); |
| 58 | + require('dotenv').config(); |
| 59 | + |
| 60 | + const pool = new Pool({ |
| 61 | + connectionString: process.env.DATABASE_URL, |
| 62 | + ssl: process.env.NODE_ENV === 'production' ? { rejectUnauthorized: false } : false, |
| 63 | + }); |
| 64 | + |
| 65 | + async function testConnection() { |
| 66 | + try { |
| 67 | + const client = await pool.connect(); |
| 68 | + const result = await client.query('SELECT NOW()'); |
| 69 | + client.release(); |
| 70 | + console.log('✅ Database connection successful'); |
| 71 | + return true; |
| 72 | + } catch (error) { |
| 73 | + console.error('❌ Database connection failed:', error.message); |
| 74 | + return false; |
| 75 | + } finally { |
| 76 | + await pool.end(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + testConnection(); |
| 81 | + `; |
| 82 | + |
| 83 | + const testScriptPath = path.join(__dirname, 'temp-db-test.js'); |
| 84 | + fs.writeFileSync(testScriptPath, testScript); |
| 85 | + |
| 86 | + try { |
| 87 | + const { stdout } = await runCommand('node ' + testScriptPath); |
| 88 | + console.log(stdout); |
| 89 | + const success = stdout.includes('Database connection successful'); |
| 90 | + |
| 91 | + // Clean up |
| 92 | + fs.unlinkSync(testScriptPath); |
| 93 | + |
| 94 | + return success; |
| 95 | + } catch (error) { |
| 96 | + // Clean up |
| 97 | + if (fs.existsSync(testScriptPath)) { |
| 98 | + fs.unlinkSync(testScriptPath); |
| 99 | + } |
| 100 | + console.error('❌ Failed to test database connection:', error.message); |
| 101 | + return false; |
| 102 | + } |
| 103 | + } catch (error) { |
| 104 | + console.error('❌ Failed to check database:', error.message); |
| 105 | + return false; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// Function to check IPFS configuration |
| 110 | +async function checkIpfs() { |
| 111 | + console.log('\n📋 Checking IPFS configuration...'); |
| 112 | + |
| 113 | + // Check if .env file exists and has IPFS configuration |
| 114 | + const envPath = path.join(__dirname, '../.env'); |
| 115 | + if (!fs.existsSync(envPath)) { |
| 116 | + console.error('❌ .env file not found. Please run npm run setup first.'); |
| 117 | + return false; |
| 118 | + } |
| 119 | + |
| 120 | + const envContent = fs.readFileSync(envPath, 'utf8'); |
| 121 | + const hasIpfsProjectId = envContent.includes('IPFS_PROJECT_ID=') && !envContent.includes('IPFS_PROJECT_ID=""'); |
| 122 | + const hasIpfsProjectSecret = envContent.includes('IPFS_PROJECT_SECRET=') && !envContent.includes('IPFS_PROJECT_SECRET=""'); |
| 123 | + |
| 124 | + if (hasIpfsProjectId && hasIpfsProjectSecret) { |
| 125 | + console.log('✅ IPFS configuration found'); |
| 126 | + return true; |
| 127 | + } else { |
| 128 | + console.error('❌ IPFS configuration incomplete. Please run npm run setup:ipfs'); |
| 129 | + return false; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// Function to check authentication configuration |
| 134 | +async function checkAuth() { |
| 135 | + console.log('\n📋 Checking authentication configuration...'); |
| 136 | + |
| 137 | + // Check if .env file exists and has authentication configuration |
| 138 | + const envPath = path.join(__dirname, '../.env'); |
| 139 | + if (!fs.existsSync(envPath)) { |
| 140 | + console.error('❌ .env file not found. Please run npm run setup first.'); |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + const envContent = fs.readFileSync(envPath, 'utf8'); |
| 145 | + const hasIndieAuthClientId = envContent.includes('INDIE_AUTH_CLIENT_ID=') && !envContent.includes('INDIE_AUTH_CLIENT_ID=""'); |
| 146 | + const hasIndieAuthClientSecret = envContent.includes('INDIE_AUTH_CLIENT_SECRET=') && !envContent.includes('INDIE_AUTH_CLIENT_SECRET=""'); |
| 147 | + const hasNextAuthSecret = envContent.includes('NEXTAUTH_SECRET=') && !envContent.includes('NEXTAUTH_SECRET=""'); |
| 148 | + |
| 149 | + if (hasIndieAuthClientId && hasIndieAuthClientSecret && hasNextAuthSecret) { |
| 150 | + console.log('✅ Authentication configuration found'); |
| 151 | + return true; |
| 152 | + } else { |
| 153 | + console.error('❌ Authentication configuration incomplete. Please run npm run setup:auth'); |
| 154 | + return false; |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// Function to check if the application builds successfully |
| 159 | +async function checkBuild() { |
| 160 | + console.log('\n📋 Checking if the application builds successfully...'); |
| 161 | + |
| 162 | + try { |
| 163 | + console.log('Building the application (this may take a moment)...'); |
| 164 | + await runCommand('npm run build'); |
| 165 | + console.log('✅ Application builds successfully'); |
| 166 | + return true; |
| 167 | + } catch (error) { |
| 168 | + console.error('❌ Application build failed:', error.message); |
| 169 | + return false; |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// Function to check security |
| 174 | +async function checkSecurity() { |
| 175 | + console.log('\n📋 Checking security...'); |
| 176 | + |
| 177 | + try { |
| 178 | + console.log('Running security audit...'); |
| 179 | + const { stdout } = await runCommand('npm audit --json'); |
| 180 | + |
| 181 | + try { |
| 182 | + const auditResult = JSON.parse(stdout); |
| 183 | + const vulnerabilities = auditResult.vulnerabilities || {}; |
| 184 | + const totalVulnerabilities = Object.values(vulnerabilities).reduce((sum, severity) => sum + severity, 0); |
| 185 | + |
| 186 | + if (totalVulnerabilities === 0) { |
| 187 | + console.log('✅ No security vulnerabilities found'); |
| 188 | + return true; |
| 189 | + } else { |
| 190 | + console.error(`❌ Found ${totalVulnerabilities} security vulnerabilities. Please run npm audit fix`); |
| 191 | + return false; |
| 192 | + } |
| 193 | + } catch (parseError) { |
| 194 | + console.error('❌ Failed to parse audit results:', parseError.message); |
| 195 | + return false; |
| 196 | + } |
| 197 | + } catch (error) { |
| 198 | + console.error('❌ Security audit failed:', error.message); |
| 199 | + return false; |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +// Main function |
| 204 | +async function runPreLaunchCheck() { |
| 205 | + console.log('🚀 BasedNet Pre-Launch Checklist'); |
| 206 | + console.log('=============================='); |
| 207 | + |
| 208 | + let allChecksPass = true; |
| 209 | + |
| 210 | + // Check environment variables |
| 211 | + const envCheck = await checkEnvironment(); |
| 212 | + allChecksPass = allChecksPass && envCheck; |
| 213 | + |
| 214 | + // Check database connection |
| 215 | + const dbCheck = await checkDatabase(); |
| 216 | + allChecksPass = allChecksPass && dbCheck; |
| 217 | + |
| 218 | + // Check IPFS configuration |
| 219 | + const ipfsCheck = await checkIpfs(); |
| 220 | + allChecksPass = allChecksPass && ipfsCheck; |
| 221 | + |
| 222 | + // Check authentication configuration |
| 223 | + const authCheck = await checkAuth(); |
| 224 | + allChecksPass = allChecksPass && authCheck; |
| 225 | + |
| 226 | + // Check if the application builds successfully |
| 227 | + const buildCheck = await checkBuild(); |
| 228 | + allChecksPass = allChecksPass && buildCheck; |
| 229 | + |
| 230 | + // Check security |
| 231 | + const securityCheck = await checkSecurity(); |
| 232 | + allChecksPass = allChecksPass && securityCheck; |
| 233 | + |
| 234 | + // Summary |
| 235 | + console.log('\n📋 Pre-Launch Checklist Summary'); |
| 236 | + console.log('-----------------------------'); |
| 237 | + console.log(`Environment Variables: ${envCheck ? '✅' : '❌'}`); |
| 238 | + console.log(`Database Connection: ${dbCheck ? '✅' : '❌'}`); |
| 239 | + console.log(`IPFS Configuration: ${ipfsCheck ? '✅' : '❌'}`); |
| 240 | + console.log(`Authentication Configuration: ${authCheck ? '✅' : '❌'}`); |
| 241 | + console.log(`Application Build: ${buildCheck ? '✅' : '❌'}`); |
| 242 | + console.log(`Security Check: ${securityCheck ? '✅' : '❌'}`); |
| 243 | + |
| 244 | + if (allChecksPass) { |
| 245 | + console.log('\n🎉 All checks passed! BasedNet is ready for launch.'); |
| 246 | + console.log('\nNext steps:'); |
| 247 | + console.log('1. Deploy to production: npm run deploy'); |
| 248 | + console.log('2. Set up your custom domain'); |
| 249 | + console.log('3. Configure monitoring and analytics'); |
| 250 | + } else { |
| 251 | + console.log('\n⚠️ Some checks failed. Please address the issues before launching.'); |
| 252 | + } |
| 253 | + |
| 254 | + rl.close(); |
| 255 | +} |
| 256 | + |
| 257 | +// Run the pre-launch check |
| 258 | +runPreLaunchCheck(); |
0 commit comments