Use existing environment variables as defaults for the generator command.

This commit is contained in:
giancarlo
2024-06-05 20:02:29 +07:00
parent a3f339aaf6
commit aba9076805
3 changed files with 100 additions and 65 deletions

View File

@@ -0,0 +1,40 @@
import { readFileSync } from 'node:fs';
export namespace generator {
export function loadAllEnvironmentVariables(basePath: string) {
const sharedEnv = loadEnvironmentVariables(basePath + '/.env');
const productionEnv = loadEnvironmentVariables(
basePath + '/.env.production',
);
return {
...sharedEnv,
...productionEnv,
};
}
export function loadEnvironmentVariables(filePath: string) {
const file = readFileSync(filePath, 'utf-8');
const vars = file.split('\n').filter((line) => line.trim() !== '');
const env: Record<string, string> = {};
for (const line of vars) {
const isComment = line.startsWith('#');
if (isComment) {
continue;
}
const [key, value] = line.split('=');
if (!key) {
continue;
}
env[key] = value ?? '';
}
return env;
}
}