UNPKG

@milkmaccya2/hostswitch

Version:

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

114 lines 4.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CliUserInterface = void 0; class CliUserInterface { logger; constructor(logger) { this.logger = logger; } showMessage(message, type = 'info') { switch (type) { case 'info': this.logger.info(message); break; case 'error': this.logger.error(message); break; case 'success': this.logger.success(message); break; case 'warning': this.logger.warning(message); break; } } async promptConfirm(_message) { throw new Error('Confirmation prompts are not supported in CLI mode. Use command line arguments instead.'); } async promptSelect(_message, _choices) { throw new Error('Selection prompts are not supported in CLI mode. Use command line arguments instead.'); } async promptInput(_message, _validator) { throw new Error('Input prompts are not supported in CLI mode. Use command line arguments instead.'); } async handleCommandResult(result) { if (result.requiresSudo) { await this.handleSudoRequired(result); return; } if (result.requiresConfirmation) { this.handleConfirmationRequired(); return; } this.handleResult(result); } async handleSudoRequired(result) { this.showMessage('This operation requires sudo privileges. Rerunning with sudo...', 'info'); if (this.isTestEnvironment()) { this.showMessage('(Skipped in test environment)', 'info'); return; } if (!result.sudoCommand) { this.showMessage('No sudo command provided', 'error'); return; } await this.executeSudo(); } isTestEnvironment() { return (process.env.NODE_ENV === 'test' || process.env.VITEST === 'true' || Boolean(process.env.npm_lifecycle_event?.includes('test'))); } async executeSudo() { try { const { execSync } = require('node:child_process'); const args = process.argv.slice(2).join(' '); execSync(`sudo ${process.argv[0]} ${process.argv[1]} ${args}`, { stdio: 'inherit' }); process.exit(0); } catch (_error) { this.showMessage('Failed to execute with sudo', 'error'); process.exit(1); } } handleConfirmationRequired() { this.showMessage('This operation requires confirmation. Add --force flag to proceed without confirmation.', 'warning'); } handleResult(result) { if (result.success) { if (result.data) { this.displayData(result.data); } if (result.message) { this.showMessage(result.message, 'success'); } } else { this.showMessage(result.message || 'Operation failed', 'error'); process.exit(1); } } displayData(data) { if (data && typeof data === 'object' && 'profiles' in data) { // List profiles command const profilesData = data; if (profilesData.profiles.length === 0) { this.showMessage('No profiles found', 'info'); } else { this.showMessage('Available profiles:', 'info'); profilesData.profiles.forEach((profile) => { const status = profile.isCurrent ? ' (current)' : ''; this.logger.info(` ${profile.name}${status}`); }); } } else if (data && typeof data === 'object' && 'content' in data) { // Show profile command const contentData = data; console.log(contentData.content); } } } exports.CliUserInterface = CliUserInterface; //# sourceMappingURL=CliUserInterface.js.map