UNPKG

@kya-os/cli

Version:

CLI for KYA-OS MCP-I setup and management

222 lines • 8.58 kB
import chalk from "chalk"; import { exec } from "child_process"; import { promisify } from "util"; import { EnvManager } from "../utils/env-manager.js"; import { detectPlatform } from "../utils/platform-detector.js"; import { showSuccess, showError } from "../utils/prompts.js"; import { cliEffects } from "../effects/cli-integration.js"; const execAsync = promisify(exec); export async function env(subcommand) { const envManager = new EnvManager(); switch (subcommand) { case "show": await showEnv(envManager); break; case "copy": await copyEnv(envManager); break; case "verify": await verifyEnv(envManager); break; } } async function showEnv(envManager) { console.log(chalk.cyan("\nšŸ” MCP-I Environment Variables\n")); // Try to get from multiple sources let vars = envManager.getFromProcess(); if (!vars.MCP_IDENTITY_AGENT_DID) { // Try reading from files const files = [".env.local", ".env", ".env.production"]; for (const file of files) { const fileVars = envManager.readEnvFile(file); if (fileVars.MCP_IDENTITY_AGENT_DID) { vars = fileVars; console.log(chalk.gray(`(from ${file})\n`)); break; } } } else { console.log(chalk.gray("(from process.env)\n")); } if (!vars.MCP_IDENTITY_AGENT_DID) { showError("No MCP-I environment variables found"); console.log("Run the following to initialize:"); console.log(chalk.cyan(" npx kya-os init")); return; } // Display variables with binary matrix effect const varsDisplay = envManager.formatForDisplay(vars, true); await cliEffects.showCustomEffect("binarypath", varsDisplay, { config: { duration: 3000, streamCount: 8, binaryColors: ["008800", "00ff00"], revealColor: "00ff00", streamDelay: 50, }, }); // Platform-specific instructions const platform = detectPlatform(); console.log(chalk.bold("\nšŸ“‹ Platform-specific usage:\n")); if (platform.platform === "vercel" || platform.platform === "nextjs") { console.log("For Vercel deployment:"); console.log("1. Copy these variables: " + chalk.cyan("npx kya-os env copy")); console.log("2. Go to Vercel Dashboard → Settings → Environment Variables"); console.log("3. Add each variable for Production/Preview/Development as needed"); } else if (platform.platform === "lambda") { console.log("For AWS Lambda:"); console.log("1. Add to your Lambda function configuration"); console.log("2. Or use AWS Systems Manager Parameter Store"); console.log("3. Or add to your serverless.yml/SAM template"); } else if (platform.platform === "docker") { console.log("For Docker:"); console.log("1. Add to docker-compose.yml under environment:"); console.log("2. Or use --env-file with docker run"); console.log("3. Or build args in Dockerfile"); } else { console.log("Add these to your deployment platform's environment configuration."); } } async function copyEnv(envManager) { console.log(chalk.cyan("\nšŸ“‹ Copying MCP-I Environment Variables\n")); // Get variables let vars = envManager.getFromProcess(); if (!vars.MCP_IDENTITY_AGENT_DID) { const files = [".env.local", ".env"]; for (const file of files) { const fileVars = envManager.readEnvFile(file); if (fileVars.MCP_IDENTITY_AGENT_DID) { vars = fileVars; break; } } } if (!vars.MCP_IDENTITY_AGENT_DID) { showError("No MCP-I environment variables found"); return; } // Format for clipboard (no masking) const formatted = envManager.formatForClipboard(vars); // Try to copy to clipboard try { // Detect OS and use appropriate command const platform = process.platform; let command; if (platform === "darwin") { command = "pbcopy"; } else if (platform === "win32") { command = "clip"; } else { // Linux - try xclip first, then xsel try { await execAsync("which xclip"); command = "xclip -selection clipboard"; } catch { try { await execAsync("which xsel"); command = "xsel --clipboard --input"; } catch { throw new Error("No clipboard utility found. Install xclip or xsel."); } } } // Copy to clipboard const proc = exec(command); if (proc.stdin) { proc.stdin.write(formatted); proc.stdin.end(); } await new Promise((resolve, reject) => { proc.on("exit", (code) => { if (code === 0) resolve(null); else reject(new Error(`Clipboard command failed with code ${code}`)); }); proc.on("error", reject); }); showSuccess("Environment variables copied to clipboard!"); console.log("\nNext steps:"); console.log("1. Paste into your deployment platform's environment configuration"); console.log("2. Make sure to save/apply the changes"); console.log("\n" + chalk.yellow("āš ļø Keep these values secure - they contain your private key!")); } catch (error) { showError(`Failed to copy to clipboard: ${error.message}`); console.log("\nManual copy:\n"); console.log(chalk.gray("─".repeat(60))); console.log(formatted); console.log(chalk.gray("─".repeat(60))); } } async function verifyEnv(envManager) { console.log(chalk.cyan("\nāœ… Verifying MCP-I Environment\n")); // Check process.env const processVars = envManager.getFromProcess(); const { valid: processValid, missing: processMissing } = envManager.validateVariables(processVars); console.log(chalk.bold("Process Environment:")); if (processValid) { console.log(` āœ“ ${chalk.green("All variables present in process.env")}`); } else { console.log(` āœ— ${chalk.red("Missing from process.env:")} ${processMissing.join(", ")}`); } // Check files console.log(chalk.bold("\nEnvironment Files:")); const files = [".env", ".env.local", ".env.production"]; let foundValidFile = false; for (const file of files) { const fileVars = envManager.readEnvFile(file); if (fileVars.MCP_IDENTITY_AGENT_DID) { const { valid, missing } = envManager.validateVariables(fileVars); if (valid) { console.log(` āœ“ ${chalk.green(file)} - All variables present`); foundValidFile = true; } else { console.log(` āš ļø ${chalk.yellow(file)} - Missing: ${missing.join(", ")}`); } } } if (!foundValidFile && !processValid) { console.log(` āœ— ${chalk.red("No valid environment configuration found")}`); } // Platform-specific checks const platform = detectPlatform(); console.log(chalk.bold("\nPlatform-Specific:")); if (platform.platform === "vercel" || platform.platform === "nextjs") { if (process.env.VERCEL) { console.log(` āœ“ ${chalk.green("Running on Vercel")}`); if (processValid) { console.log(` āœ“ ${chalk.green("Environment variables configured in Vercel")}`); } else { console.log(` āœ— ${chalk.red("Environment variables not found - add to Vercel Dashboard")}`); } } else { console.log(` ā„¹ļø ${chalk.blue("Local development - using .env.local")}`); } } // Summary console.log(chalk.bold("\nSummary:")); if (processValid || foundValidFile) { showSuccess("Environment configuration is valid!"); } else { showError("Environment configuration issues found"); console.log("\nTo fix:"); console.log("1. Run: " + chalk.cyan("npx kya-os init")); console.log("2. Or manually create .env file with required variables"); } } //# sourceMappingURL=env.js.map