UNPKG

gitset

Version:

Enhanced git init with user configuration management

162 lines 5.19 kB
import { GitCommandExecutor } from '../utils/git-command-executor.js'; import { PLATFORM_CONFIG } from '../config/constants.js'; /** * Detects Git installation and retrieves Git configuration * Uses optimized command execution with caching for better performance */ export class GitDetector { /** * Check if Git is installed on the system */ async checkInstallation() { try { const versionResult = await GitCommandExecutor.commands.getVersion(); if (versionResult.success) { const version = this.parseGitVersion(versionResult.stdout); const pathResult = await this.getGitPath(); return { installed: true, version, installationPath: pathResult.success ? pathResult.stdout.trim() : undefined, canAutoInstall: true }; } return { installed: false, canAutoInstall: await this.canAutoInstall() }; } catch (error) { return { installed: false, canAutoInstall: await this.canAutoInstall() }; } } /** * Get global Git configuration (cached for performance) */ async getGlobalConfig() { try { const [nameResult, emailResult] = await Promise.all([ GitCommandExecutor.commands.getGlobalUserName(), GitCommandExecutor.commands.getGlobalUserEmail() ]); return { name: nameResult.success ? nameResult.stdout.trim() : '', email: emailResult.success ? emailResult.stdout.trim() : '', source: 'global' }; } catch (error) { return { name: '', email: '', source: 'global' }; } } /** * Detect current platform information */ detectPlatform() { const platform = process.platform; const arch = process.arch; // Validate platform is supported const isSupported = PLATFORM_CONFIG.supported.includes(platform); return { platform, arch, hasGit: false, // Will be determined by checkInstallation gitVersion: undefined, supported: isSupported }; } /** * Get Git installation path */ async getGitPath() { const platform = process.platform; const command = platform === 'win32' ? 'where' : 'which'; try { // Use spawn directly for non-git commands const result = await this.executeSystemCommand(command, ['git']); return { success: result.success, stdout: result.stdout }; } catch (error) { return { success: false, stdout: '' }; } } /** * Execute system command (not Git command) */ async executeSystemCommand(command, args) { const { spawn } = await import('cross-spawn'); return new Promise((resolve) => { const child = spawn(command, args); let stdout = ''; child.stdout?.on('data', (data) => { stdout += data.toString(); }); child.on('close', (code) => { resolve({ success: code === 0, stdout }); }); child.on('error', () => { resolve({ success: false, stdout: '' }); }); }); } /** * Parse Git version from version string */ parseGitVersion(versionString) { const pattern = PLATFORM_CONFIG.gitVersionPatterns.default; const match = versionString.match(pattern); return match ? match[1] : 'unknown'; } /** * Check if Git can be automatically installed on this platform */ async canAutoInstall() { const platform = process.platform; // Check if platform is supported if (!PLATFORM_CONFIG.supported.includes(platform)) { return false; } const packageManagers = PLATFORM_CONFIG.packageManagers[platform]; // Check if any package manager is available for (const manager of packageManagers) { if (await this.commandExists(manager)) { return true; } } return false; // Manual installation option always available } /** * Check if a command exists on the system */ async commandExists(command) { try { const platform = process.platform; const checkCommand = platform === 'win32' ? 'where' : 'which'; const result = await this.executeSystemCommand(checkCommand, [command]); return result.success; } catch (error) { return false; } } } //# sourceMappingURL=git-detector.js.map