UNPKG

@milkmaccya2/hostswitch

Version:

A simple CLI tool to manage and switch between multiple hosts file profiles for different development environments

112 lines 4.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HostSwitchService = void 0; const BackupManager_1 = require("./BackupManager"); const CurrentProfileManager_1 = require("./CurrentProfileManager"); const ProfileManager_1 = require("./ProfileManager"); class HostSwitchService { fileSystem; logger; config; permissionChecker; profileManager; currentProfileManager; backupManager; constructor(fileSystem, logger, config, permissionChecker) { this.fileSystem = fileSystem; this.logger = logger; this.config = config; this.permissionChecker = permissionChecker; this.ensureDirs(); this.profileManager = new ProfileManager_1.ProfileManager(fileSystem, config); this.currentProfileManager = new CurrentProfileManager_1.CurrentProfileManager(fileSystem, config); this.backupManager = new BackupManager_1.BackupManager(fileSystem, config); } ensureDirs() { this.fileSystem.ensureDirSync(this.config.configDir); this.fileSystem.ensureDirSync(this.config.profilesDir); this.fileSystem.ensureDirSync(this.config.backupDir); } getCurrentProfile() { return this.currentProfileManager.getCurrentProfile(); } getProfiles() { const currentProfile = this.getCurrentProfile(); return this.profileManager.getProfiles(currentProfile); } createProfile(name, fromCurrent = false) { return this.profileManager.createProfile(name, fromCurrent); } async switchProfile(name) { if (!this.profileManager.profileExists(name)) { return { success: false, message: `Profile '${name}' does not exist.`, }; } // 権限チェック - sudo必要かつsudoで実行されていない場合は自動sudo実行 const needsSudo = await this.permissionChecker.requiresSudo(this.config.hostsPath); if (needsSudo) { this.logger.info('Requesting administrative access...'); const sudoResult = await this.permissionChecker.rerunWithSudo(['switch', name]); return { success: sudoResult.success, message: sudoResult.message, requiresSudo: true, }; } return this.doSwitchProfile(name); } doSwitchProfile(name) { const currentProfile = this.getCurrentProfile(); const isModified = this.currentProfileManager.isHostsModified(); let backupPath; if (!currentProfile || isModified) { backupPath = this.backupManager.backupHosts(); if (isModified && currentProfile) { this.logger.warn('Current hosts file was modified outside of hostswitch.'); } } try { const profilePath = this.profileManager.getProfilePath(name); this.fileSystem.copySync(profilePath, this.config.hostsPath); this.currentProfileManager.setCurrentProfile(name); return { success: true, message: `Switched to profile '${name}'.`, backupPath, }; } catch (err) { const error = err; if (error.code === 'EACCES') { return { success: false, message: 'Permission denied. Run with sudo.', requiresSudo: true, }; } else { return { success: false, message: `Error switching profile: ${error.message}`, }; } } } deleteProfile(name) { const currentProfile = this.getCurrentProfile(); return this.profileManager.deleteProfile(name, currentProfile); } getProfileContent(name) { return this.profileManager.getProfileContent(name); } profileExists(name) { return this.profileManager.profileExists(name); } getProfilePath(name) { return this.profileManager.getProfilePath(name); } } exports.HostSwitchService = HostSwitchService; //# sourceMappingURL=HostSwitchService.js.map