ktx/packages/context/src/core/config-reference.ts

37 lines
1,018 B
TypeScript
Raw Normal View History

2026-05-10 23:12:26 +02:00
import { readFileSync } from 'node:fs';
import { homedir } from 'node:os';
import { resolve } from 'node:path';
2026-05-10 23:51:24 +02:00
export function resolveKtxHomePath(path: string): string {
2026-05-10 23:12:26 +02:00
if (path === '~') {
return homedir();
}
if (path.startsWith('~/')) {
return resolve(homedir(), path.slice(2));
}
return resolve(path);
}
2026-05-10 23:51:24 +02:00
export function resolveKtxConfigReference(value: string | undefined, env: NodeJS.ProcessEnv): string | undefined {
2026-05-10 23:12:26 +02:00
if (!value) {
return undefined;
}
if (value.startsWith('env:')) {
const envName = value.slice('env:'.length).trim();
const envValue = env[envName];
return envValue && envValue.trim().length > 0 ? envValue.trim() : undefined;
}
if (value.startsWith('file:')) {
2026-05-10 23:51:24 +02:00
const filePath = resolveKtxHomePath(value.slice('file:'.length).trim());
2026-05-10 23:12:26 +02:00
const fileValue = readFileSync(filePath, 'utf8').trim();
return fileValue.length > 0 ? fileValue : undefined;
}
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : undefined;
}