learning_ai_common_plat/packages/config/src/product-identity.ts
saravanakumardb1 65bf79203b feat(config): add @bytelyst/config package
- Zod-based baseEnvSchema (PORT, HOST, NODE_ENV, CORS_ORIGIN, SERVICE_NAME, COSMOS_*)
- loadConfig() with extension support for service-specific fields
- loadProductIdentity() reads product.json or falls back to env vars
- getProductId() convenience function
- Replaces 5 duplicated product-config.ts files across LysnrAI
- Peer dep: zod >=3.20.0
2026-02-12 11:19:49 -08:00

76 lines
1.9 KiB
TypeScript

/**
* Product identity — reads from a product.json file or falls back to env vars.
* Eliminates the need for hardcoded product-config.ts files in every service.
*/
import { readFileSync } from "node:fs";
import { resolve } from "node:path";
export interface ProductIdentity {
productId: string;
displayName: string;
licensePrefix: string;
configDirName: string;
envVarPrefix: string;
bundleIdSuffix: string;
packageName: string;
}
let _cached: ProductIdentity | null = null;
/**
* Load product identity from a JSON file or environment variables.
*
* @param jsonPath - Path to product.json (optional, tries common locations)
* @returns Product identity object
*/
export function loadProductIdentity(jsonPath?: string): ProductIdentity {
if (_cached) return _cached;
// Try loading from file
const paths = jsonPath
? [jsonPath]
: [
resolve("shared/product.json"),
resolve("../shared/product.json"),
resolve("../../shared/product.json"),
];
for (const p of paths) {
try {
const raw = readFileSync(p, "utf-8");
_cached = JSON.parse(raw) as ProductIdentity;
return _cached;
} catch {
// Try next path
}
}
// Fallback to env vars / defaults
_cached = {
productId: process.env.PRODUCT_ID || "lysnrai",
displayName: process.env.DISPLAY_NAME || "LysnrAI",
licensePrefix: process.env.LICENSE_PREFIX || "LYSNR",
configDirName: process.env.CONFIG_DIR_NAME || ".LysnrAI",
envVarPrefix: process.env.ENV_VAR_PREFIX || "LYSNR",
bundleIdSuffix: process.env.BUNDLE_ID_SUFFIX || "LysnrAI",
packageName: process.env.PACKAGE_NAME || "lysnrai",
};
return _cached;
}
/**
* Convenience: get just the product ID string.
*/
export function getProductId(): string {
return loadProductIdentity().productId;
}
/**
* Reset the cache (useful for testing).
* @internal
*/
export function _resetProductIdentity(): void {
_cached = null;
}