| 71 | } |
| 72 | |
| 73 | const upsertSecret = (content: string, secret: string): string => { |
| 74 | const normalized = content.length > 0 ? content : '' |
| 75 | const lines = normalized.split(/\r?\n/) |
| 76 | let replaced = false |
| 77 | |
| 78 | const nextLines = lines.map((line) => { |
| 79 | if (replaced) { |
| 80 | return line |
| 81 | } |
| 82 | |
| 83 | const trimmed = line.trim() |
| 84 | if (!trimmed.startsWith('SECRET=') || trimmed.startsWith('#')) { |
| 85 | return line |
| 86 | } |
| 87 | |
| 88 | replaced = true |
| 89 | const commentIndex = line.indexOf('#') |
| 90 | const commentSuffix = commentIndex >= 0 ? line.slice(commentIndex).trimEnd() : '' |
| 91 | return commentSuffix ? `SECRET=${secret} ${commentSuffix}` : `SECRET=${secret}` |
| 92 | }) |
| 93 | |
| 94 | if (!replaced) { |
| 95 | if (nextLines.length > 0 && nextLines[nextLines.length - 1] !== '') { |
| 96 | nextLines.push(`SECRET=${secret}`) |
| 97 | } else if (nextLines.length === 0) { |
| 98 | nextLines.push(`SECRET=${secret}`) |
| 99 | } else { |
| 100 | nextLines[nextLines.length - 1] = `SECRET=${secret}` |
| 101 | nextLines.push('') |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return nextLines.join('\n') |
| 106 | } |
| 107 | |
| 108 | const ensureEnvFile = async (assumeYes: boolean): Promise<boolean> => { |
| 109 | const envPath = getProjectPath('.env') |