|
1 | | -const { getProjectRoot, path } = require('./utils') |
| 1 | +const { getProjectRoot, path, getEnvContent } = require('../utils') |
2 | 2 | const { execSync } = require('child_process') |
| 3 | +const fs = require('fs') |
3 | 4 |
|
4 | 5 | const projectRoot = getProjectRoot() |
5 | 6 | const prismaPath = path.join(projectRoot, 'packages/prisma') |
| 7 | +const envPath = path.join(projectRoot, '.env') |
| 8 | + |
| 9 | +const getEnvValue = (content, key) => { |
| 10 | + const match = content.match(new RegExp(`^${key}=(.*)$`, 'm')) |
| 11 | + return match ? match[1] : null |
| 12 | +} |
6 | 13 |
|
7 | 14 | try { |
8 | | - execSync('npm run setup:env') |
| 15 | + execSync('npm run setup:env', { stdio: 'inherit' }) |
9 | 16 |
|
10 | 17 | // Check if Docker is running |
11 | 18 | try { |
|
15 | 22 | throw new Error('Docker is not running. Please start Docker and try again.') |
16 | 23 | } |
17 | 24 |
|
18 | | - console.log('Starting Docker containers...') |
19 | | - execSync('npm run docker:db', { stdio: 'inherit', cwd: projectRoot }) |
20 | | - execSync('npm run docker:studio', { stdio: 'inherit', cwd: projectRoot }) |
| 25 | + // Load .env and check DATABASE_HOST |
| 26 | + if (!fs.existsSync(envPath)) { |
| 27 | + throw new Error('.env file not found.') |
| 28 | + } |
| 29 | + |
| 30 | + const envContent = getEnvContent(envPath) |
| 31 | + const databaseHost = getEnvValue(envContent, 'DATABASE_HOST') |
| 32 | + |
| 33 | + if (!databaseHost) { |
| 34 | + throw new Error('DATABASE_HOST is not set in .env') |
| 35 | + } |
| 36 | + |
| 37 | + const isLocalDb = databaseHost === 'localhost:5432' || databaseHost === 'db:5432' |
| 38 | + |
| 39 | + if (isLocalDb) { |
| 40 | + console.log(`🟢 DATABASE_HOST is '${databaseHost}', starting local DB container...`) |
| 41 | + execSync('npm run docker-dev:db', { stdio: 'inherit', cwd: projectRoot }) |
| 42 | + } else { |
| 43 | + console.log(`🟡 DATABASE_HOST is '${databaseHost}', skipping local DB container.`) |
| 44 | + } |
| 45 | + |
| 46 | + console.log('Starting Prisma Studio container...') |
| 47 | + execSync('npm run docker-dev:studio', { stdio: 'inherit', cwd: projectRoot }) |
21 | 48 |
|
22 | 49 | console.log('Generating Prisma client...') |
23 | 50 | execSync('npx prisma generate', { stdio: 'inherit', cwd: prismaPath }) |
|
0 commit comments