import { existsSync } from "fs";
import { join } from "path";
import { parseIonApiFile, type IonApiConfig } from "./ionapi.js";
export type Environment = "TST" | "PRD" | "TRN";
/**
* Get the current environment from environment variable or default
*/
export function getCurrentEnvironment(): Environment {
const env = process.env.BIRST_ENV?.toUpperCase();
if (env === "TST" || env === "PRD" || env === "TRN") {
return env;
}
// Default to TST
return "TST";
}
/**
* Get the path to the .ionapi file for an environment
*/
export function getIonApiPath(env: Environment): string {
// Check for environment variable override
const envPath = process.env.BIRST_IONAPI_PATH;
if (envPath && existsSync(envPath)) {
return envPath;
}
// Check credentials directory
const credentialsPath = join(process.cwd(), "credentials", `${env}.ionapi`);
if (existsSync(credentialsPath)) {
return credentialsPath;
}
// Check root directory
const rootPath = join(process.cwd(), `${env}.ionapi`);
if (existsSync(rootPath)) {
return rootPath;
}
throw new Error(
`ION API file not found for environment ${env}. ` +
`Looked in: ${credentialsPath}, ${rootPath}. ` +
`Set BIRST_IONAPI_PATH environment variable to specify a custom path.`
);
}
/**
* Load ION API configuration for the specified or current environment
*/
export function loadEnvironmentConfig(env?: Environment): IonApiConfig {
const targetEnv = env || getCurrentEnvironment();
const ionApiPath = getIonApiPath(targetEnv);
return parseIonApiFile(ionApiPath);
}
/**
* List available environments
*/
export function listAvailableEnvironments(): Environment[] {
const available: Environment[] = [];
const envs: Environment[] = ["TST", "PRD", "TRN"];
for (const env of envs) {
try {
getIonApiPath(env);
available.push(env);
} catch {
// Environment not available
}
}
return available;
}
/**
* Feature flags for optional tool categories
*/
export interface FeatureFlags {
/** Enable GenAI tools (requires Birst AI entitlement) */
enableGenAI: boolean;
/** Enable ICW query tools (requires application provisioning) */
enableICW: boolean;
}
/**
* Get feature flags from environment variables
*/
export function getFeatureFlags(): FeatureFlags {
return {
enableGenAI: process.env.BIRST_ENABLE_GENAI === "true",
enableICW: process.env.BIRST_ENABLE_ICW === "true",
};
}