UNPKG

gitarsenal-cli

Version:

CLI tool for creating Modal sandboxes with GitHub repositories

162 lines (141 loc) • 5.44 kB
#!/usr/bin/env node const fs = require('fs'); const path = require('path'); const inquirer = require('inquirer'); const chalk = require('chalk'); const os = require('os'); async function setupE2B() { console.log(chalk.blue('šŸ”§ E2B Sandbox Setup')); console.log(chalk.gray('This script will help you set up the E2B API key for GitArsenal.')); console.log(chalk.gray('You can get your API key by signing up at https://e2b.dev')); console.log(); // Check if E2B_API_KEY is already set in environment const existingKey = process.env.E2B_API_KEY; if (existingKey) { console.log(chalk.green('āœ… E2B API key is already set in your environment.')); console.log(chalk.gray(`Key: ${existingKey.substring(0, 4)}...${existingKey.substring(existingKey.length - 4)}`)); const { updateKey } = await inquirer.prompt([ { type: 'confirm', name: 'updateKey', message: 'Do you want to update your E2B API key?', default: false } ]); if (!updateKey) { console.log(chalk.green('āœ… Using existing E2B API key.')); return; } } // Prompt for E2B API key const { apiKey } = await inquirer.prompt([ { type: 'password', name: 'apiKey', message: 'Enter your E2B API key:', mask: '*', validate: (input) => { if (!input.trim()) { return 'API key is required'; } return true; } } ]); // Determine where to store the API key const { storeLocation } = await inquirer.prompt([ { type: 'list', name: 'storeLocation', message: 'Where would you like to store the API key?', choices: [ { name: 'Environment variable (this session only)', value: 'env' }, { name: '.env file in current directory', value: 'local' }, { name: '.env file in home directory', value: 'home' } ], default: 'env' } ]); // Store the API key based on user's choice switch (storeLocation) { case 'env': process.env.E2B_API_KEY = apiKey; console.log(chalk.green('āœ… E2B API key set for this session.')); console.log(chalk.yellow('āš ļø This setting will be lost when you close your terminal.')); console.log(chalk.gray('To make it permanent, add the following to your shell profile:')); console.log(chalk.cyan(`export E2B_API_KEY="${apiKey}"`)); break; case 'local': try { const envFile = path.join(process.cwd(), '.env'); let content = ''; if (fs.existsSync(envFile)) { content = fs.readFileSync(envFile, 'utf8'); // Replace existing E2B_API_KEY if it exists if (content.match(/^E2B_API_KEY=.*/m)) { content = content.replace(/^E2B_API_KEY=.*/m, `E2B_API_KEY="${apiKey}"`); } else { content += `\nE2B_API_KEY="${apiKey}"\n`; } } else { content = `E2B_API_KEY="${apiKey}"\n`; } fs.writeFileSync(envFile, content); console.log(chalk.green(`āœ… E2B API key saved to ${envFile}`)); } catch (error) { console.error(chalk.red(`āŒ Failed to save API key to .env file: ${error.message}`)); } break; case 'home': try { const homeDir = os.homedir(); const envFile = path.join(homeDir, '.env'); let content = ''; if (fs.existsSync(envFile)) { content = fs.readFileSync(envFile, 'utf8'); // Replace existing E2B_API_KEY if it exists if (content.match(/^E2B_API_KEY=.*/m)) { content = content.replace(/^E2B_API_KEY=.*/m, `E2B_API_KEY="${apiKey}"`); } else { content += `\nE2B_API_KEY="${apiKey}"\n`; } } else { content = `E2B_API_KEY="${apiKey}"\n`; } fs.writeFileSync(envFile, content); console.log(chalk.green(`āœ… E2B API key saved to ${envFile}`)); } catch (error) { console.error(chalk.red(`āŒ Failed to save API key to home .env file: ${error.message}`)); } break; } // Also store in GitArsenal credentials try { const gitarsenalDir = path.join(os.homedir(), '.gitarsenal'); if (!fs.existsSync(gitarsenalDir)) { fs.mkdirSync(gitarsenalDir, { recursive: true }); } const credentialsFile = path.join(gitarsenalDir, 'credentials.json'); let credentials = {}; if (fs.existsSync(credentialsFile)) { try { credentials = JSON.parse(fs.readFileSync(credentialsFile, 'utf8')); } catch (error) { console.log(chalk.yellow(`āš ļø Could not read existing credentials file: ${error.message}`)); } } credentials['e2b'] = apiKey; fs.writeFileSync(credentialsFile, JSON.stringify(credentials, null, 2)); console.log(chalk.green('āœ… E2B API key also saved to GitArsenal credentials.')); } catch (error) { console.error(chalk.red(`āŒ Failed to save API key to GitArsenal credentials: ${error.message}`)); } console.log(chalk.green('\nšŸŽ‰ E2B setup complete! You can now use the E2B sandbox provider.')); console.log(chalk.cyan('To use E2B sandbox, run:')); console.log(chalk.white(' gitarsenal --sandbox-provider e2b --repo <repository-url>')); } // Run the setup setupE2B().catch(error => { console.error(chalk.red(`āŒ Error: ${error.message}`)); process.exit(1); });