gitset
Version:
Enhanced git init with user configuration management
231 lines • 8.54 kB
JavaScript
import { spawn } from 'cross-spawn';
import ora from 'ora';
import chalk from 'chalk';
export class GitInstaller {
platform;
constructor() {
this.platform = process.platform;
}
async getAvailableInstallationMethods() {
const methods = [];
switch (this.platform) {
case 'darwin':
methods.push(...await this.getMacOSMethods());
break;
case 'linux':
methods.push(...await this.getLinuxMethods());
break;
case 'win32':
methods.push(...await this.getWindowsMethods());
break;
}
return methods.filter(method => method !== null);
}
async installGit(method) {
const spinner = ora(`Installing Git using ${method.name}...`).start();
try {
if (method.requiresElevation) {
spinner.text = `Installing Git using ${method.name} (elevated permissions required)...`;
}
const result = await this.executeInstallCommand(method.command);
if (result.success) {
spinner.succeed(chalk.green(`Git installed successfully using ${method.name}!`));
// Verify installation
const verifySpinner = ora('Verifying Git installation...').start();
const verified = await this.verifyInstallation();
if (verified) {
verifySpinner.succeed(chalk.green('Git installation verified!'));
return true;
}
else {
verifySpinner.fail(chalk.red('Git installation verification failed'));
return false;
}
}
else {
spinner.fail(chalk.red(`Git installation failed: ${result.stderr}`));
return false;
}
}
catch (error) {
spinner.fail(chalk.red(`Git installation error: ${error}`));
return false;
}
}
async getMacOSMethods() {
const methods = [];
// Check for Homebrew
if (await this.commandExists('brew')) {
methods.push({
name: 'Homebrew',
command: ['brew', 'install', 'git'],
description: 'Install Git using Homebrew package manager',
requiresElevation: false
});
}
// Check for Xcode Command Line Tools
if (await this.commandExists('xcode-select')) {
methods.push({
name: 'Xcode Command Line Tools',
command: ['xcode-select', '--install'],
description: 'Install Git via Xcode Command Line Tools',
requiresElevation: true
});
}
// Manual download option (always available)
methods.push({
name: 'Manual Download',
command: ['open', 'https://git-scm.com/download/mac'],
description: 'Download Git installer from official website',
requiresElevation: false
});
return methods;
}
async getLinuxMethods() {
const methods = [];
// APT (Ubuntu/Debian)
if (await this.commandExists('apt-get')) {
methods.push({
name: 'APT',
command: ['sudo', 'apt-get', 'update', '&&', 'sudo', 'apt-get', 'install', '-y', 'git'],
description: 'Install Git using APT package manager (Ubuntu/Debian)',
requiresElevation: true
});
}
// YUM (RHEL/CentOS)
if (await this.commandExists('yum')) {
methods.push({
name: 'YUM',
command: ['sudo', 'yum', 'install', '-y', 'git'],
description: 'Install Git using YUM package manager (RHEL/CentOS)',
requiresElevation: true
});
}
// DNF (Fedora)
if (await this.commandExists('dnf')) {
methods.push({
name: 'DNF',
command: ['sudo', 'dnf', 'install', '-y', 'git'],
description: 'Install Git using DNF package manager (Fedora)',
requiresElevation: true
});
}
// Pacman (Arch Linux)
if (await this.commandExists('pacman')) {
methods.push({
name: 'Pacman',
command: ['sudo', 'pacman', '-S', '--noconfirm', 'git'],
description: 'Install Git using Pacman package manager (Arch Linux)',
requiresElevation: true
});
}
// Zypper (openSUSE)
if (await this.commandExists('zypper')) {
methods.push({
name: 'Zypper',
command: ['sudo', 'zypper', 'install', '-y', 'git'],
description: 'Install Git using Zypper package manager (openSUSE)',
requiresElevation: true
});
}
// APK (Alpine Linux)
if (await this.commandExists('apk')) {
methods.push({
name: 'APK',
command: ['apk', 'add', 'git'],
description: 'Install Git using APK package manager (Alpine Linux)',
requiresElevation: false // no necessary "sudo" in Alpine
});
}
return methods;
}
async getWindowsMethods() {
const methods = [];
// Windows Package Manager
if (await this.commandExists('winget')) {
methods.push({
name: 'Windows Package Manager',
command: ['winget', 'install', '--id', 'Git.Git', '--exact'],
description: 'Install Git using Windows Package Manager',
requiresElevation: false
});
}
// Chocolatey
if (await this.commandExists('choco')) {
methods.push({
name: 'Chocolatey',
command: ['choco', 'install', 'git', '-y'],
description: 'Install Git using Chocolatey package manager',
requiresElevation: true
});
}
// Scoop
if (await this.commandExists('scoop')) {
methods.push({
name: 'Scoop',
command: ['scoop', 'install', 'git'],
description: 'Install Git using Scoop package manager',
requiresElevation: false
});
}
// Manual download option (always available)
methods.push({
name: 'Manual Download',
command: ['cmd', '/c', 'start', 'https://git-scm.com/download/windows'],
description: 'Download Git installer from official website',
requiresElevation: false
});
return methods;
}
async executeInstallCommand(command) {
return new Promise((resolve) => {
const [cmd, ...args] = command;
const child = spawn(cmd, args, { stdio: 'pipe' });
let stdout = '';
let stderr = '';
child.stdout?.on('data', (data) => {
stdout += data.toString();
});
child.stderr?.on('data', (data) => {
stderr += data.toString();
});
child.on('close', (code) => {
resolve({
success: code === 0,
stdout,
stderr
});
});
child.on('error', (error) => {
resolve({
success: false,
stdout: '',
stderr: error.message
});
});
});
}
async verifyInstallation() {
try {
const result = await this.executeInstallCommand(['git', '--version']);
return result.success && result.stdout.includes('git version');
}
catch {
return false;
}
}
async commandExists(command) {
return new Promise((resolve) => {
const platform = process.platform;
const checkCommand = platform === 'win32' ? 'where' : 'which';
const child = spawn(checkCommand, [command], { stdio: 'pipe' });
child.on('close', (code) => {
resolve(code === 0);
});
child.on('error', () => {
resolve(false);
});
});
}
}
//# sourceMappingURL=git-installer.js.map