import Conf from 'conf'; import axios from 'axios'; import chalk from 'chalk'; import fs from 'fs'; import path from 'path'; interface RigstateConfig { apiKey?: string; projectId?: string; apiUrl?: string; } const config = new Conf({ projectName: 'rigstate-cli', defaults: { apiUrl: 'https://app.rigstate.com', }, }); /** * Get the stored API key * @throws {Error} If no API key is found (user not logged in) */ export function getApiKey(): string { // 1. Check local manifest first (Folder Affinity) try { const cwd = process.cwd(); const manifestPaths = [ path.join(cwd, '.rigstate', 'identity.json'), path.join(cwd, '.rigstate') ]; for (const manifestPath of manifestPaths) { if (fs.existsSync(manifestPath) && fs.statSync(manifestPath).isFile()) { const content = fs.readFileSync(manifestPath, 'utf-8'); const manifest = JSON.parse(content); if (manifest.api_key) return manifest.api_key; } } } catch (e) { /* Fallback */ } // 2. Check environment variable if (process.env.RIGSTATE_API_KEY) { return process.env.RIGSTATE_API_KEY; } // 3. Fall back to global config const apiKey = config.get('apiKey'); if (!apiKey) { throw new Error( '❌ Not logged in. Please run "rigstate login " first.' ); } return apiKey; } /** * Set the API key */ export function setApiKey(key: string): void { config.set('apiKey', key); } /** * Get the default project ID (if set) */ export function getProjectId(): string | undefined { // 1. Check local manifest first (Folder Affinity) try { const cwd = process.cwd(); const manifestPaths = [ path.join(cwd, '.rigstate', 'identity.json'), path.join(cwd, '.rigstate') ]; for (const manifestPath of manifestPaths) { if (fs.existsSync(manifestPath) && fs.statSync(manifestPath).isFile()) { const content = fs.readFileSync(manifestPath, 'utf-8'); const manifest = JSON.parse(content); if (manifest.project_id) { console.log(chalk.dim(` [Auth] Context: Project ID ${manifest.project_id.substring(0, 8)}... (from ${path.basename(manifestPath)})`)); return manifest.project_id; } } } } catch (e: any) { console.log(chalk.red(` [Error] Failed to read context: ${e.message}`)); } // 2. Check environment variable if (process.env.RIGSTATE_PROJECT_ID) { console.log(chalk.dim(` [Auth] Context: Project ID ${process.env.RIGSTATE_PROJECT_ID.substring(0, 8)}... (from ENV)`)); return process.env.RIGSTATE_PROJECT_ID; } // 3. Fall back to global config const globalId = config.get('projectId'); return globalId; } /** * Set the default project ID */ export function setProjectId(projectId: string): void { config.set('projectId', projectId); } /** * Get the API URL (Synchronous) * Priority: Environment variable > Stored config > Production default */ export function getApiUrl(): string { let url = ''; // 1. Check local manifest first try { const cwd = process.cwd(); const manifestPaths = [ path.join(cwd, '.rigstate', 'identity.json'), path.join(cwd, '.rigstate') ]; for (const manifestPath of manifestPaths) { if (fs.existsSync(manifestPath) && fs.statSync(manifestPath).isFile()) { const content = fs.readFileSync(manifestPath, 'utf-8'); const manifest = JSON.parse(content); if (manifest.api_url) { url = manifest.api_url; break; } } } } catch (e) { /* Fallback */ } // 2. Check environment variable next if (!url && process.env.RIGSTATE_API_URL) { url = process.env.RIGSTATE_API_URL; } // 3. Check stored config if (!url) { url = config.get('apiUrl') as string; } // 4. Default to production if (!url) { url = 'https://app.rigstate.com'; } // Clean URL return url.replace(/\/+$/, ''); } /** * Set the API URL */ export function setApiUrl(url: string): void { config.set('apiUrl', url); } /** * Clear all config */ export function clearConfig(): void { config.clear(); } export { config };