| 266 | } |
| 267 | |
| 268 | export function addDotEnvConfigCode(manifestData: string): string { |
| 269 | // add dotenv config after imports in project.ts file |
| 270 | let snippetCodeIndex = -1; |
| 271 | const manifestSections = manifestData.split('\n'); |
| 272 | for (let i = 0; i < manifestSections.length; i++) { |
| 273 | if (manifestSections[i].trim() === '') { |
| 274 | snippetCodeIndex = i + 1; |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | if (snippetCodeIndex === -1) { |
| 280 | snippetCodeIndex = 0; |
| 281 | } |
| 282 | |
| 283 | const envConfigCodeSnippet = ` |
| 284 | import * as dotenv from 'dotenv'; |
| 285 | import path from 'path'; |
| 286 | |
| 287 | const mode = process.env.NODE_ENV || 'production'; |
| 288 | |
| 289 | // Load the appropriate .env file |
| 290 | const dotenvPath = path.resolve(__dirname, \`.env\${mode !== 'production' ? \`.$\{mode}\` : ''}\`); |
| 291 | dotenv.config({ path: dotenvPath, quiet: true }); |
| 292 | `; |
| 293 | |
| 294 | // Inserting the env configuration code in project.ts |
| 295 | const updatedTsProject = `${ |
| 296 | manifestSections.slice(0, snippetCodeIndex).join('\n') + envConfigCodeSnippet |
| 297 | }\n${manifestSections.slice(snippetCodeIndex).join('\n')}`; |
| 298 | return updatedTsProject; |
| 299 | } |
| 300 | |
| 301 | export async function prepareEnv(projectPath: string, project: ProjectSpecBase): Promise<void> { |
| 302 | //load and write manifest(project.ts/project.yaml) |