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 72 73 74 75 76 77 78 79 80 81 82 83 84 | import * as dotenv from 'dotenv';
import { DotEnvCaster } from 'dotenv-caster';
import fs from 'fs';
import { BackendConfig, EnvConfig, FrontendConfig } from './types';
export const dotenvLoader = (): EnvConfig => {
dotenv.config();
const caster = new DotEnvCaster();
const viteApiUrl = caster.castString(process.env.VITE_API_URL);
const viteClientUrl = caster.castString(process.env.VITE_CLIENT_URL);
const viteIdenteapotSalt = caster.castString(process.env.VITE_IDENTEAPOT_SALT);
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);
const dbCredentialsSslRejectUnauthorized = caster.castBoolean(
process.env.DATABASE_CREDENTIALS_SSL_REJECT_UNAUTHORIZED
);
const betterAuthUrl = caster.castString(process.env.BETTER_AUTH_URL);
const betterAuthSecret = caster.castString(process.env.BETTER_AUTH_SECRET);
const discordClientId = caster.castString(process.env.DISCORD_CLIENT_ID);
const discordClientSecret = caster.castString(process.env.DISCORD_CLIENT_SECRET);
const googleClientId = caster.castString(process.env.GOOGLE_CLIENT_ID);
const googleClientSecret = caster.castString(process.env.GOOGLE_CLIENT_SECRET);
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_CLIENT_URL: viteClientUrl,
VITE_IDENTEAPOT_SALT: viteIdenteapotSalt ? '*****' : '',
POSTGRES_USER: postgresUser,
POSTGRES_PASSWORD: postgresPassword ? '*****' : '',
POSTGRES_DB: postgresDb,
POSTGRES_PORT: postgresPort,
DATABASE_CREDENTIALS_SSL_REJECT_UNAUTHORIZED: dbCredentialsSslRejectUnauthorized,
BETTER_AUTH_URL: betterAuthUrl,
BETTER_AUTH_SECRET: betterAuthSecret ? '*****' : '',
DISCORD_CLIENT_ID: discordClientId,
DISCORD_CLIENT_SECRET: discordClientSecret ? '*****' : '',
GOOGLE_CLIENT_ID: googleClientId,
GOOGLE_CLIENT_SECRET: googleClientSecret ? '*****' : '',
});
const frontendConfig: FrontendConfig = {
viteApiUrl,
viteClientUrl,
viteIdenteapotSalt,
};
const backendConfig: BackendConfig = {
postgresUser,
postgresPassword,
postgresDb,
postgresPort,
dbCredentialsSslRejectUnauthorized,
betterAuthUrl,
betterAuthSecret,
discordClientId,
discordClientSecret,
googleClientId,
googleClientSecret,
};
const envConfig = {
frontendConfig,
backendConfig,
};
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}`);
};
|