const { writeFile: writeF, existsSync: exists, mkdirSync: makeDirSync } = require('node:fs'); require('dotenv').config(); function getCliArg(name: string): string | undefined { const prefix = `--${name}`; const argv = process.argv; const idx = argv.findIndex((a) => a === prefix || a.startsWith(prefix + '=')); if (idx === -1) return undefined; const current = argv[idx]; if (current.includes('=')) return current.split('=')[1]; return argv[idx + 1]; } const environmentVar = getCliArg('environment'); function writeFileUsingFS(targetPath: string, environmentFileContent: string) { writeF(targetPath, environmentFileContent, function (err: NodeJS.ErrnoException | null) { if (err) { console.error(err); } if (environmentFileContent !== '') { console.log(`write ${environmentFileContent} to ${targetPath}`); } }); } // Providing path to the `environments` directory const envDir = 'apps/processpuzzle-testbed/src/environments'; // creates the `environments` directory if it does not exist if (!exists(envDir)) { makeDirSync(envDir); } //creates the `environment.prod.ts`, `environment.test.ts` and `environment.ts` file if it does not exist writeFileUsingFS(`${envDir}/environment.dev.ts`, ''); writeFileUsingFS(`${envDir}/environment.ci.ts`, ''); writeFileUsingFS(`${envDir}/environment.prod.ts`, ''); writeFileUsingFS(`${envDir}/environment.stage.ts`, ''); writeFileUsingFS(`${envDir}/environment.ts`, ''); // choose the correct targetPath based on the environment chosen let targetPat: string; switch (environmentVar) { case 'dev': targetPat = `${envDir}/environment.dev.ts`; break; case 'ci': targetPat = `${envDir}/environment.ci.ts`; break; case 'stage': targetPat = `${envDir}/environment.stage.ts`; break; case 'prod': targetPat = `${envDir}/environment.prod.ts`; break; default: targetPat = `${envDir}/environment.ts`; break; } //actual content to be compiled dynamically and pasted into respective environment files const fileContent = ` // This file was autogenerated by dynamically running setEnv.ts and using dotenv for managing API key secrecy import { EnvironmentVariables } from './environment-variables'; export const environment: EnvironmentVariables = { FIREBASE_API_KEY: '${process.env['FIREBASE_API_KEY']}', PIPELINE_STAGE: '${process.env['PIPELINE_STAGE']}' }; `; writeFileUsingFS(targetPat, fileContent); // appending data into the target file/* eslint:enable */ writeFileUsingFS(`${envDir}/environment.ts`, fileContent); // Emit runtime-env.json so main.ts can load it in non-Docker stages (dev/stage/prod). // In Docker (ci), docker-entrypoint.sh overwrites this file from container env vars at startup. // Values are stringified the same way as the TS env file above (missing env → literal "undefined"). const assetsDir = 'apps/processpuzzle-testbed/src/assets'; const runtimeEnvContent = JSON.stringify( { PIPELINE_STAGE: `${process.env['PIPELINE_STAGE']}`, FIREBASE_API_KEY: `${process.env['FIREBASE_API_KEY']}`, }, null, 2, ) + '\n'; writeFileUsingFS(`${assetsDir}/runtime-env.json`, runtimeEnvContent);