forked from wangziqi/gongxue-base
46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
|
|
export const DEFAULT_DATABASE_URL = 'postgresql://postgres:postgres@127.0.0.1:54322/postgres';
|
|
export const DEFAULT_TENANT_ID = '00000000-0000-0000-0000-000000000001';
|
|
export const DEFAULT_TENANT_SLUG = 'master';
|
|
export const DEFAULT_TENANT_NAME = '升本刷题通主租户';
|
|
|
|
export function loadDotenv(cwd = process.cwd()) {
|
|
const envPath = path.resolve(cwd, '.env');
|
|
if (!fs.existsSync(envPath)) return;
|
|
|
|
const content = fs.readFileSync(envPath, 'utf8');
|
|
for (const line of content.split(/\r?\n/)) {
|
|
const trimmed = line.trim();
|
|
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
const idx = trimmed.indexOf('=');
|
|
if (idx === -1) continue;
|
|
const key = trimmed.slice(0, idx).trim();
|
|
const value = trimmed.slice(idx + 1).trim().replace(/^"|"$/g, '');
|
|
if (!process.env[key]) process.env[key] = value;
|
|
}
|
|
}
|
|
|
|
export function envString(key: string, fallback: string) {
|
|
return process.env[key] || fallback;
|
|
}
|
|
|
|
export function envNumber(key: string, fallback: number) {
|
|
const value = Number(process.env[key]);
|
|
return Number.isFinite(value) ? value : fallback;
|
|
}
|
|
|
|
export function envBoolean(key: string, fallback = false) {
|
|
const value = process.env[key];
|
|
if (value === undefined) return fallback;
|
|
return ['true', '1', 'yes', 'y', 'on'].includes(value.toLowerCase());
|
|
}
|
|
|
|
export function envList(key: string, fallback = '') {
|
|
return (process.env[key] || fallback)
|
|
.split(',')
|
|
.map(value => value.trim())
|
|
.filter(Boolean);
|
|
}
|