All files functions.ts

0% Statements 0/26
0% Branches 0/10
0% Functions 0/3
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71                                                                                                                                             
import * as dotenv from 'dotenv';
import { DotEnvCaster } from 'dotenv-caster';
import fs from 'fs';
import { BackendConfig, EnvConfig, FrontendConfig, NotifyConfig } from './types';
 
export const dotenvLoader = (): EnvConfig => {
  dotenv.config();
 
  const caster = new DotEnvCaster();
 
  const viteApiUrl = caster.castString(process.env.VITE_API_URL);
  const viteSentryDsn = caster.castString(process.env.VITE_SENTRY_DSN);
  const sentryAuthToken = caster.castString(process.env.SENTRY_AUTH_TOKEN);
  const sentryOrg = caster.castString(process.env.SENTRY_ORG);
  const sentryProject = caster.castString(process.env.SENTRY_PROJECT);
  const postgresUser = caster.castString(process.env.POSTGRES_USER);
  const postgresPassword = caster.castString(process.env.POSTGRES_PASSWORD);
  const postgresDb = caster.castString(process.env.POSTGRES_DB);
  const postgresPort = parseInt(caster.castString(process.env.POSTGRES_PORT), 10);
  if (Number.isNaN(postgresPort)) {
    throw new Error('POSTGRES_PORT has to be a number');
  }
 
  console.info('Environment variables loaded from .env file');
  console.table({
    VITE_API_URL: viteApiUrl,
    VITE_SENTRY_DSN: viteSentryDsn ? '*****' : '',
    SENTRY_AUTH_TOKEN: sentryAuthToken ? '*****' : '',
    SENTRY_ORG: sentryOrg,
    SENTRY_PROJECT: sentryProject,
    POSTGRES_USER: postgresUser,
    POSTGRES_PASSWORD: postgresPassword ? '*****' : '',
    POSTGRES_DB: postgresDb,
    POSTGRES_PORT: postgresPort,
  });
 
  const frontendConfig: FrontendConfig = {
    viteApiUrl,
    viteSentryDsn,
    sentryAuthToken,
    sentryOrg,
    sentryProject,
  };
  const backendConfig: BackendConfig = {
    postgresUser,
    postgresPassword,
    postgresDb,
    postgresPort,
  };
  const notifyConfig: NotifyConfig = {
    apiUrl: viteApiUrl,
  };
 
  const envConfig = {
    frontendConfig,
    backendConfig,
    notifyConfig,
  };
 
  return envConfig;
};
 
export const fileWriter = (path: string, data: string) => {
  fs.writeFile(path, data, (error) => {
    if (error) {
      throw new Error(`Failed to write ${path}: ${error.message}`);
    }
  });
  console.info(`Succeeded to write ${path}`);
};