import { existsSync, readFileSync, writeFileSync } from 'node:fs' const keys = ['VITE_API_BASE_URL', 'VITE_USE_MOCK_AUTH', 'VITE_BASE_PATH'] function parseEnv(text) { const out = {} for (const line of text.split(/\r?\n/)) { const trimmed = line.trim() if (!trimmed || trimmed.startsWith('#')) continue const index = trimmed.indexOf('=') if (index <= 0) continue const key = trimmed.slice(0, index).trim() let value = trimmed.slice(index + 1) if ( (value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'")) ) { value = value.slice(1, -1) } out[key] = value } return out } const merged = {} if (existsSync('.env')) { Object.assign(merged, parseEnv(readFileSync('.env', 'utf8'))) } for (const key of keys) { const value = process.env[key] if (value) merged[key] = value } if (!merged.VITE_API_BASE_URL) { merged.VITE_API_BASE_URL = 'https://nademitex.montest.ir/server/api' } if (!merged.VITE_USE_MOCK_AUTH) { merged.VITE_USE_MOCK_AUTH = 'false' } if (!merged.VITE_BASE_PATH || merged.VITE_BASE_PATH === '/') { merged.VITE_BASE_PATH = '/' } else if (!merged.VITE_BASE_PATH.endsWith('/')) { merged.VITE_BASE_PATH = `${merged.VITE_BASE_PATH}/` } const lines = Object.entries(merged) .filter(([key]) => key.startsWith('VITE_')) .map(([key, value]) => `${key}=${value}`) writeFileSync('.env.production', `${lines.join('\n')}\n`) console.log('Vite build env:') for (const line of lines) console.log(` ${line}`)