UNPKG

@kya-os/cli

Version:

CLI for MCP-I setup and management

197 lines 7.73 kB
import { existsSync, readFileSync } from 'fs'; import { join } from 'path'; export function detectPlatform(projectRoot = process.cwd()) { const info = { platform: 'unknown', hasVercel: false, hasDocker: false, packageManager: 'npm' }; // Check package.json const packageJsonPath = join(projectRoot, 'package.json'); if (existsSync(packageJsonPath)) { try { const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); // Detect Next.js if (packageJson.dependencies?.next || packageJson.devDependencies?.next) { info.platform = 'nextjs'; info.framework = 'nextjs'; info.version = packageJson.dependencies?.next || packageJson.devDependencies?.next; } // Check for other frameworks if (packageJson.dependencies?.express) { info.framework = 'express'; if (info.platform === 'unknown') info.platform = 'node'; } if (packageJson.dependencies?.fastify) { info.framework = 'fastify'; if (info.platform === 'unknown') info.platform = 'node'; } } catch (error) { console.error('Error reading package.json:', error); } } // Check for Vercel if (existsSync(join(projectRoot, 'vercel.json')) || existsSync(join(projectRoot, '.vercel'))) { info.hasVercel = true; if (info.platform === 'nextjs') { info.platform = 'vercel'; // Next.js on Vercel } } // Check for Docker if (existsSync(join(projectRoot, 'Dockerfile')) || existsSync(join(projectRoot, 'docker-compose.yml'))) { info.hasDocker = true; if (info.platform === 'unknown') info.platform = 'docker'; } // Check for Lambda if (existsSync(join(projectRoot, 'serverless.yml')) || existsSync(join(projectRoot, 'template.yaml')) || existsSync(join(projectRoot, 'sam-template.yaml'))) { info.platform = 'lambda'; } // Check for Cloudflare Workers const wranglerToml = join(projectRoot, 'wrangler.toml'); const wranglerJson = join(projectRoot, 'wrangler.json'); if (existsSync(wranglerToml) || existsSync(wranglerJson)) { // Check if it's Cloudflare Pages or Workers try { const configPath = existsSync(wranglerToml) ? wranglerToml : wranglerJson; const configContent = readFileSync(configPath, 'utf-8'); // Cloudflare Pages typically has pages_build_output_dir if (configContent.includes('pages_build_output_dir') || configContent.includes('"pages"')) { info.platform = 'cloudflare-pages'; } else { info.platform = 'cloudflare'; } info.framework = 'cloudflare-workers'; } catch (error) { // Default to cloudflare if we can't read the config info.platform = 'cloudflare'; info.framework = 'cloudflare-workers'; } } // Also check for @cloudflare/workers-types in package.json if (existsSync(packageJsonPath) && info.platform === 'unknown') { try { const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); if (packageJson.dependencies?.['@cloudflare/workers-types'] || packageJson.devDependencies?.['@cloudflare/workers-types'] || packageJson.devDependencies?.wrangler) { info.platform = 'cloudflare'; info.framework = 'cloudflare-workers'; } } catch (error) { // Ignore parsing errors } } // Detect package manager if (existsSync(join(projectRoot, 'pnpm-lock.yaml'))) { info.packageManager = 'pnpm'; } else if (existsSync(join(projectRoot, 'yarn.lock'))) { info.packageManager = 'yarn'; } // Default to Node.js if we found a package.json but nothing else if (info.platform === 'unknown' && existsSync(packageJsonPath)) { info.platform = 'node'; } return info; } export function getPlatformSpecificConfig(platform) { const configs = { nextjs: { storage: 'memory', transport: 'fetch', envFile: '.env.local', instructions: [ 'Add environment variables to .env.local for local development', 'For production, add variables to Vercel Dashboard', 'Import with: import "@kya-os/mcp-i/auto"' ] }, vercel: { storage: 'memory', transport: 'fetch', envFile: '.env.local', instructions: [ 'Add environment variables to .env.local for local development', 'Add the same variables to Vercel Dashboard → Settings → Environment Variables', 'Import with: import "@kya-os/mcp-i/auto"' ] }, lambda: { storage: 'memory', transport: 'fetch', envFile: '.env', instructions: [ 'Add environment variables to your Lambda function configuration', 'Or use AWS Systems Manager Parameter Store', 'Import with: import "@kya-os/mcp-i/auto"' ] }, docker: { storage: 'file', transport: 'fetch', envFile: '.env', instructions: [ 'Add environment variables to docker-compose.yml or Dockerfile', 'Mount a volume for persistent identity storage', 'Import with: import "@kya-os/mcp-i/auto"' ] }, node: { storage: 'file', transport: 'fetch', envFile: '.env', instructions: [ 'Add environment variables to .env file', 'Make sure to add .env to .gitignore', 'Import with: import "@kya-os/mcp-i/auto"' ] }, cloudflare: { storage: 'kv', transport: 'fetch', envFile: '.dev.vars', package: '@kya-os/mcp-i-cloudflare', instructions: [ 'For local development: Add environment variables to .dev.vars', 'For production: Add secrets using "wrangler secret put AGENT_PRIVATE_KEY"', 'Create KV namespace: wrangler kv:namespace create "MCP_IDENTITY"', 'Add KV binding to wrangler.toml', 'Import with: import "@kya-os/mcp-i-cloudflare"' ] }, 'cloudflare-pages': { storage: 'kv', transport: 'fetch', envFile: '.dev.vars', package: '@kya-os/mcp-i-cloudflare', instructions: [ 'For local development: Add environment variables to .dev.vars', 'For production: Add environment variables in Cloudflare Pages Dashboard', 'Create KV namespace in Cloudflare Dashboard', 'Add KV binding in Pages settings', 'Import with: import "@kya-os/mcp-i-cloudflare"' ] }, unknown: { storage: 'auto', transport: 'fetch', envFile: '.env', instructions: [ 'Add environment variables based on your deployment platform', 'Import with: import "@kya-os/mcp-i/auto"' ] } }; return configs[platform]; } //# sourceMappingURL=platform-detector.js.map