UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

172 lines (171 loc) 6.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addAliasBlockToShellConfig = addAliasBlockToShellConfig; exports.addAliasToShellConfig = addAliasToShellConfig; exports.addEnvVarToShellConfig = addEnvVarToShellConfig; exports.checkAliasInFile = checkAliasInFile; exports.checkEnvVarInFile = checkEnvVarInFile; exports.detectShellConfigs = detectShellConfigs; exports.getPreferredShellConfig = getPreferredShellConfig; /** * Shell configuration utilities for CLI commands * Handles detection and modification of shell config files (.zshrc, .bashrc, etc.) */ const fs_1 = require("fs"); const os_1 = require("os"); const path_1 = require("path"); /** * Known shell configuration files in order of preference */ const SHELL_CONFIG_FILES = [ { file: '.zshrc', shell: 'zsh' }, { file: '.bashrc', shell: 'bash' }, { file: '.bash_profile', shell: 'bash' }, { file: '.profile', shell: 'sh' }, ]; /** * Add multiple aliases to a shell config file as a block * @param configPath - Full path to the shell config file * @param aliases - Array of alias definitions * @param blockComment - Comment header for the block * @returns true if successful, false otherwise */ function addAliasBlockToShellConfig(configPath, aliases, blockComment = 'Claude Code shortcuts - Added by lenne.tech CLI') { try { const lines = [`\n# ${blockComment}`]; for (const { alias, command } of aliases) { lines.push(`alias ${alias}='${command}'`); } lines.push(''); (0, fs_1.appendFileSync)(configPath, lines.join('\n'), 'utf-8'); return true; } catch (_a) { return false; } } /** * Add an alias to a shell config file * @param configPath - Full path to the shell config file * @param alias - Alias name * @param command - Command the alias expands to * @param comment - Optional comment to add before the alias * @returns true if successful, false otherwise */ function addAliasToShellConfig(configPath, alias, command, comment = 'Added by lenne.tech CLI') { try { const lineToAdd = `\n# ${comment}\nalias ${alias}='${command}'\n`; (0, fs_1.appendFileSync)(configPath, lineToAdd, 'utf-8'); return true; } catch (_a) { return false; } } /** * Add an environment variable export to a shell config file * @param configPath - Full path to the shell config file * @param envName - Name of the environment variable * @param envValue - Value to set * @param comment - Optional comment to add before the export * @returns true if successful, false otherwise */ function addEnvVarToShellConfig(configPath, envName, envValue, comment = 'Added by lenne.tech CLI') { try { const lineToAdd = `\n# ${comment}\nexport ${envName}=${envValue}\n`; (0, fs_1.appendFileSync)(configPath, lineToAdd, 'utf-8'); return true; } catch (_a) { return false; } } /** * Check if an alias is already defined in a shell config file * @param configPath - Full path to the shell config file * @param alias - Alias name to check * @returns true if the alias exists in the file */ function checkAliasInFile(configPath, alias) { try { if (!(0, fs_1.existsSync)(configPath)) { return false; } const content = (0, fs_1.readFileSync)(configPath, 'utf-8'); // Check for alias definition (with single or double quotes) const patterns = [`alias ${alias}=`, `alias ${alias} =`]; return patterns.some((p) => content.includes(p)); } catch (_a) { return false; } } /** * Check if an environment variable is already set in a shell config file * Checks for common export formats (with/without quotes) * @param configPath - Full path to the shell config file * @param envName - Name of the environment variable * @param envValue - Expected value * @returns true if the export exists in the file */ function checkEnvVarInFile(configPath, envName, envValue) { try { if (!(0, fs_1.existsSync)(configPath)) { return false; } const content = (0, fs_1.readFileSync)(configPath, 'utf-8'); // Check for existing export (with or without quotes) const patterns = [ `export ${envName}=${envValue}`, `export ${envName}="${envValue}"`, `export ${envName}='${envValue}'`, ]; return patterns.some((p) => content.includes(p)); } catch (_a) { return false; } } /** * Detect available shell configuration files for the current user * Prioritizes the current shell's config file * @returns Array of shell config file info, sorted by preference */ function detectShellConfigs() { const home = (0, os_1.homedir)(); const configs = []; // Check current shell from environment const currentShell = process.env.SHELL ? (0, path_1.basename)(process.env.SHELL) : null; for (const { file, shell } of SHELL_CONFIG_FILES) { const fullPath = (0, path_1.join)(home, file); const fileExists = (0, fs_1.existsSync)(fullPath); // Prioritize current shell's config if (fileExists || (currentShell && currentShell === shell)) { configs.push({ exists: fileExists, path: fullPath, shell, }); } } // If no configs found, suggest creating .zshrc or .bashrc based on current shell if (configs.length === 0) { const defaultShell = currentShell || 'zsh'; const defaultFile = defaultShell === 'bash' ? '.bashrc' : '.zshrc'; configs.push({ exists: false, path: (0, path_1.join)(home, defaultFile), shell: defaultShell, }); } return configs; } /** * Get the preferred shell config file for the current user * Returns the first existing config, or suggests a default * @returns The preferred shell config file info, or null if none found */ function getPreferredShellConfig() { const configs = detectShellConfigs(); return configs.find((c) => c.exists) || configs[0] || null; }