/** * @fractary/core - Unified Configuration Loader * * Builds on yaml-config.ts to provide a unified configuration object * with integrated authentication support. */ import { CoreYamlConfig, ConfigLoadOptions, WorkConfig, RepoConfig, LogsConfig, FileConfig, DocsConfig, CodexConfig } from '../common/yaml-config'; import type { TokenProvider, GitHubAppConfig } from '../auth/types'; /** * Get the canonical env directory path (.fractary/env/) * * @param projectRoot Project root directory. If not provided, auto-detected. * @returns Absolute path to .fractary/env/ */ export declare function getEnvDir(projectRoot?: string): string; /** * Ensure .fractary/env/ directory exists, creating it if needed. * * @param projectRoot Project root directory. If not provided, auto-detected. * @returns Absolute path to the created/existing .fractary/env/ directory */ export declare function ensureEnvDir(projectRoot?: string): string; /** * Information about an env file found in the project. */ export interface EnvFileInfo { /** Human-readable name, e.g., "(default)", "test", "prod", "local" */ name: string; /** Relative file path, e.g., ".fractary/env/.env.test" */ file: string; /** Whether this file is in the standard (.fractary/env/) or legacy (root) location */ location: 'standard' | 'legacy'; /** Whether the file actually exists on disk */ exists: boolean; } /** * List all env files across .fractary/env/ (standard) and project root (legacy). * * Standard-location files take precedence over legacy when both exist * for the same base name (deduplication). * * @param projectRoot Project root directory. If not provided, auto-detected. * @returns Array of EnvFileInfo sorted by name */ export declare function listEnvFiles(projectRoot?: string): EnvFileInfo[]; /** * Resolve a specific env file path, checking .fractary/env/ first, then project root. * * @param fileName The env file name, e.g. ".env", ".env.test", ".env.local" * @param projectRoot Project root directory. If not provided, auto-detected. * @returns Object with path and location, or null if not found anywhere */ export declare function resolveEnvFile(fileName: string, projectRoot?: string): { path: string; location: 'standard' | 'legacy'; } | null; /** * Read a plugin's managed section from an env file. * * Section markers: * ``` * # ===== {pluginName} (managed) ===== * KEY=VALUE * # ===== end {pluginName} ===== * ``` * * @param filePath Absolute path to the env file * @param pluginName Plugin name, e.g. "fractary-core" * @returns Key-value pairs within the section, or null if section not found */ export declare function readManagedSection(filePath: string, pluginName: string): Record | null; /** * Write/update a plugin's managed section in an env file. * * Creates the file if it doesn't exist. Preserves all other sections and * content outside managed sections. * * If the plugin's section already exists, replaces content between markers. * Otherwise, appends a new section at the end. * * @param filePath Absolute path to the env file * @param pluginName Plugin name, e.g. "fractary-core" * @param entries Key-value pairs to write in the section */ export declare function writeManagedSection(filePath: string, pluginName: string, entries: Record): void; /** * Load environment variables from .env files with multi-environment support * * This function explicitly loads .env files - it must be called manually * rather than being a side effect of importing the module. * * ## Multi-Environment Support * * Set `FRACTARY_ENV` to load environment-specific .env files: * - `FRACTARY_ENV=staging` loads `.env.staging` * - `FRACTARY_ENV=production` loads `.env.production` * * Loading order (later files override earlier): * 1. `.env` - Base configuration (always loaded if exists) * 2. `.env.{FRACTARY_ENV}` - Environment-specific overrides * 3. `.env.local` - Local overrides (never committed, always loaded last) * * All files are optional. Missing files are silently skipped. * * ## File Locations * * When `.fractary/env/` directory exists (standard mode): * - Only `.fractary/env/` is checked * - Missing files are silently skipped (all files are optional) * - No fallback to project root or cwd * * When `.fractary/env/` does NOT exist (legacy mode): * 1. `/` — with deprecation warning * 2. `/` — if cwd differs from projectRoot * * @param options Loading options * @returns true if any .env file was loaded, false if no .env files found * * @example * ```typescript * import { loadEnv, loadConfig } from '@fractary/core'; * * // Load default .env * loadEnv(); * * // Or set environment before loading * process.env.FRACTARY_ENV = 'production'; * loadEnv({ force: true }); * * // From CLI: FRACTARY_ENV=production fractary-core-work issue-list * ``` */ export declare function loadEnv(options?: { cwd?: string; force?: boolean; }): boolean; /** * Get the currently loaded environment name * * @returns The value of FRACTARY_ENV when loadEnv was called, or undefined */ export declare function getCurrentEnv(): string | undefined; /** * Switch to a different environment mid-session * * This function allows changing environments during a Claude session, which is * useful for workflows like FABR where you move through phases that target * different environments: * * - **Local/Dev**: Writing code, running local tests * - **Test**: Deploying during evaluate phase * - **Prod**: Deploying during release phase * * ## What It Does * * 1. Sets `process.env.FRACTARY_ENV` to the new environment * 2. Reloads environment variables in order: `.env` → `.env.{newEnv}` → `.env.local` * 3. Updates `getCurrentEnv()` to return the new environment * * ## Important Notes * * - Variables from the previous environment that aren't overwritten will persist * - To start fresh, call `clearEnv()` before `switchEnv()` * - The config.yaml is NOT reloaded automatically; credentials are resolved * from process.env when API calls are made * * @param envName The environment to switch to (e.g., 'test', 'staging', 'prod') * @param options Optional settings * @returns true if the environment was switched successfully * * @example * ```typescript * import { switchEnv, getCurrentEnv } from '@fractary/core'; * * // FABR Workflow Example * * // Frame & Architect phases - local development * console.log(getCurrentEnv()); // undefined or 'dev' * * // Build phase - still local * // ... build and test locally ... * * // Evaluate phase - switch to test environment * switchEnv('test'); * console.log(getCurrentEnv()); // 'test' * // Now GITHUB_TOKEN, AWS_* etc. come from .env.test * // ... deploy to test, run integration tests ... * * // Release phase - switch to production * switchEnv('prod'); * console.log(getCurrentEnv()); // 'prod' * // Now credentials come from .env.prod * // ... deploy to production ... * ``` */ export declare function switchEnv(envName: string, options?: { cwd?: string; }): boolean; /** * Clear environment-specific variables and reset to base state * * This removes variables that were loaded from `.env.{FRACTARY_ENV}` files, * leaving only system environment variables and base `.env` values. * * Useful before `switchEnv()` if you want to ensure no variables from the * previous environment persist. * * ## State Reset Behavior * * After calling `clearEnv()`: * - `getCurrentEnv()` returns `undefined` * - `isEnvLoaded()` returns `false` * - The next `loadEnv()` or `switchEnv()` call will reload from files * * Note: This only clears the specified variables from `process.env`. * System environment variables (set outside Node.js) are not affected * and may still be present. * * @param variablesToClear Optional list of specific variables to clear. * If not provided, clears common Fractary variables. * * @example * ```typescript * // Clear before switching to ensure clean state * clearEnv(); * switchEnv('prod'); * * // Or clear specific variables * clearEnv(['GITHUB_TOKEN', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY']); * ``` */ export declare function clearEnv(variablesToClear?: string[]): void; /** * Check if environment variables have been loaded * * @returns true if loadEnv() has been called successfully */ export declare function isEnvLoaded(): boolean; /** * GitHub configuration extracted from yaml config */ export interface ExtractedGitHubConfig { /** Personal access token (from handlers.github.token or environment) */ token?: string; /** GitHub organization name */ organization?: string; /** GitHub project (owner/repo format) */ project?: string; /** GitHub App configuration */ app?: GitHubAppConfig; } /** * Loaded configuration with authentication * * Extends the raw YAML configuration with computed authentication. */ export interface LoadedConfig { /** Raw configuration version */ version: string; /** GitHub configuration extracted from work/repo handlers */ github?: ExtractedGitHubConfig; /** Token provider for GitHub authentication (if configured) */ tokenProvider?: TokenProvider; /** Work tracking configuration */ work?: WorkConfig; /** Repository management configuration */ repo?: RepoConfig; /** Logs management configuration */ logs?: LogsConfig; /** File storage configuration */ file?: FileConfig; /** Documentation management configuration */ docs?: DocsConfig; /** Codex configuration */ codex?: CodexConfig; /** Raw configuration (for advanced use cases) */ raw: CoreYamlConfig; } /** * Configuration loader options */ export interface LoadConfigOptions extends ConfigLoadOptions { /** Skip creating token provider (useful for non-GitHub operations) */ skipAuth?: boolean; /** Skip auto-loading .env file (default: false - .env is loaded automatically) */ skipEnvLoad?: boolean; } /** * Load unified configuration with authentication * * This function: * 1. Auto-loads .env files (unless skipEnvLoad is true) * 2. Loads and parses the YAML configuration * 3. Extracts GitHub configuration from handlers * 4. Creates a TokenProvider for authentication (if configured) * 5. Returns a unified config object * * Environment variables from .env files are loaded automatically by default, * so GITHUB_TOKEN and other secrets can be read from .env without manual sourcing. * * @param options Configuration loading options * @returns Loaded configuration with authentication * * @example * ```typescript * // .env is loaded automatically - GITHUB_TOKEN will be available * const config = await loadConfig(); * * if (config.tokenProvider) { * const token = await config.tokenProvider.getToken(); * // Use token for API calls * } * ``` */ export declare function loadConfig(options?: LoadConfigOptions): Promise; /** * Load configuration synchronously (without token provider) * * Useful for cases where you only need the configuration data * without authentication. * * @param options Configuration loading options * @returns Loaded configuration without token provider */ export declare function loadConfigSync(options?: ConfigLoadOptions): Omit; //# sourceMappingURL=loader.d.ts.map