UNPKG

seb-cli-tool

Version:

SEB CLI - Smart Embedded Board Configuration Tool - Cloud-First MCU Management

224 lines (186 loc) 6.92 kB
#!/usr/bin/env node const { spawn, execSync } = require('child_process'); const path = require('path'); const fs = require('fs'); const which = require('which'); console.log('🔧 Setting up SEB CLI for Windows...'); // Find Python executable for Windows function findPython() { try { // Try python first (Windows typically uses 'python') return which.sync('python'); } catch (e) { try { // Fallback to python3 return which.sync('python3'); } catch (e) { console.error('❌ Python not found. Please install Python 3.10+ from https://www.python.org/downloads/'); console.error('💡 Make sure to check "Add Python to PATH" during installation'); process.exit(1); } } } // Check Python version function checkPythonVersion(pythonPath) { try { const version = execSync(`"${pythonPath}" --version`, { encoding: 'utf8' }); const match = version.match(/Python (\d+)\.(\d+)/); if (match) { const major = parseInt(match[1]); const minor = parseInt(match[2]); if (major < 3 || (major === 3 && minor < 10)) { console.error('❌ Python 3.10+ is required. Found:', version.trim()); process.exit(1); } console.log(`✅ Found Python ${version.trim()}`); return true; } } catch (e) { console.error('❌ Could not check Python version:', e.message); process.exit(1); } } // Create virtual environment in user's home directory function createVirtualEnv(pythonPath) { const userHome = process.env.USERPROFILE || process.env.HOME; const sebHome = path.join(userHome, '.seb'); const venvPath = path.join(sebHome, 'venv'); // Create .seb directory if it doesn't exist if (!fs.existsSync(sebHome)) { fs.mkdirSync(sebHome, { recursive: true }); } if (fs.existsSync(venvPath)) { console.log('✅ Virtual environment already exists'); return venvPath; } console.log('🔧 Creating virtual environment...'); return new Promise((resolve, reject) => { const venvProcess = spawn(pythonPath, ['-m', 'venv', venvPath], { stdio: 'inherit', cwd: sebHome }); venvProcess.on('close', (code) => { if (code === 0) { console.log('✅ Virtual environment created'); resolve(venvPath); } else { console.error('❌ Failed to create virtual environment'); reject(new Error(`venv creation failed with code ${code}`)); } }); venvProcess.on('error', (error) => { console.error('❌ Error creating virtual environment:', error.message); reject(error); }); }); } // Get virtual environment Python path for Windows function getVenvPython(venvPath) { const pythonPath = path.join(venvPath, 'Scripts', 'python.exe'); if (fs.existsSync(pythonPath)) { return pythonPath; } throw new Error('Could not find Python in virtual environment'); } // Install Python dependencies in virtual environment function installPythonDeps(venvPython) { const packageDir = path.dirname(__dirname); const requirementsPath = path.join(packageDir, 'requirements.txt'); if (!fs.existsSync(requirementsPath)) { console.log('⚠️ No requirements.txt found, skipping Python dependency installation'); return Promise.resolve(); } console.log('📦 Installing Python dependencies...'); return new Promise((resolve, reject) => { const pipProcess = spawn(venvPython, ['-m', 'pip', 'install', '-r', requirementsPath], { stdio: 'inherit', cwd: packageDir }); pipProcess.on('close', (code) => { if (code === 0) { console.log('✅ Python dependencies installed successfully'); resolve(); } else { console.error('❌ Failed to install Python dependencies'); reject(new Error(`pip install failed with code ${code}`)); } }); pipProcess.on('error', (error) => { console.error('❌ Error running pip:', error.message); reject(error); }); }); } // Install the SEB package in development mode function installSebPackage(venvPython) { const packageDir = path.dirname(__dirname); const setupPath = path.join(packageDir, 'setup.py'); if (!fs.existsSync(setupPath)) { console.log('⚠️ No setup.py found, skipping package installation'); return Promise.resolve(); } console.log('🔧 Installing SEB package in development mode...'); return new Promise((resolve, reject) => { const installProcess = spawn(venvPython, ['-m', 'pip', 'install', '-e', '.'], { stdio: 'inherit', cwd: packageDir }); installProcess.on('close', (code) => { if (code === 0) { console.log('✅ SEB package installed successfully'); resolve(); } else { console.error('❌ Failed to install SEB package'); reject(new Error(`pip install -e failed with code ${code}`)); } }); installProcess.on('error', (error) => { console.error('❌ Error installing SEB package:', error.message); reject(error); }); }); } // Create a configuration file to help the CLI find the virtual environment function createConfigFile(venvPath) { const userHome = process.env.USERPROFILE || process.env.HOME; const sebHome = path.join(userHome, '.seb'); const configPath = path.join(sebHome, 'config.json'); const config = { venv_path: venvPath, python_path: path.join(venvPath, 'Scripts', 'python.exe'), created_at: new Date().toISOString(), platform: 'windows', analytics_enabled: true // Enable analytics by default }; fs.writeFileSync(configPath, JSON.stringify(config, null, 2)); console.log('✅ Configuration file created'); } // Main installation process async function main() { try { console.log('🔍 Checking Python installation...'); const pythonPath = findPython(); checkPythonVersion(pythonPath); console.log('🔧 Setting up virtual environment in user directory...'); const venvPath = await createVirtualEnv(pythonPath); const venvPython = getVenvPython(venvPath); console.log('📦 Installing dependencies...'); await installPythonDeps(venvPython); await installSebPackage(venvPython); console.log('⚙️ Creating configuration...'); createConfigFile(venvPath); console.log('🎉 SEB CLI setup complete!'); console.log(`💡 Virtual environment: ${venvPath}`); console.log('💡 You can now use: seb --help'); console.log('💡 If you encounter issues, try running: seb --offline-mode'); } catch (error) { console.error('❌ Setup failed:', error.message); console.log('💡 Try running: npm run setup-python'); process.exit(1); } } // Run if called directly if (require.main === module) { main(); } module.exports = { main, findPython, checkPythonVersion, createVirtualEnv, getVenvPython, installPythonDeps, installSebPackage };