|
| 1 | +import { GluegunCommand } from 'gluegun'; |
| 2 | + |
| 3 | +import { ExtendedGluegunToolbox } from '../../interfaces/extended-gluegun-toolbox'; |
| 4 | +import { |
| 5 | + addAliasBlockToShellConfig, |
| 6 | + checkAliasInFile, |
| 7 | + getPreferredShellConfig, |
| 8 | + ShellAlias, |
| 9 | +} from '../../lib/shell-config'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Claude Code shortcuts (shell aliases) |
| 13 | + * See: https://docs.lennetech.app/claude-code/installation |
| 14 | + */ |
| 15 | +const CLAUDE_SHORTCUTS: ShellAlias[] = [ |
| 16 | + { |
| 17 | + alias: 'c', |
| 18 | + command: 'claude --dangerously-skip-permissions', |
| 19 | + description: 'Start new Claude Code session', |
| 20 | + }, |
| 21 | + { |
| 22 | + alias: 'cc', |
| 23 | + command: 'claude --dangerously-skip-permissions --continue', |
| 24 | + description: 'Continue last session', |
| 25 | + }, |
| 26 | + { |
| 27 | + alias: 'cr', |
| 28 | + command: 'claude --dangerously-skip-permissions --resume', |
| 29 | + description: 'Select and resume previous session', |
| 30 | + }, |
| 31 | +]; |
| 32 | + |
| 33 | +/** |
| 34 | + * Install Claude Code shell shortcuts |
| 35 | + */ |
| 36 | +const ShortcutsCommand: GluegunCommand = { |
| 37 | + alias: ['s'], |
| 38 | + description: 'Install shell shortcuts', |
| 39 | + name: 'shortcuts', |
| 40 | + run: async (toolbox: ExtendedGluegunToolbox) => { |
| 41 | + const { |
| 42 | + print: { error, info, success }, |
| 43 | + } = toolbox; |
| 44 | + |
| 45 | + // Get preferred shell config |
| 46 | + const shellConfig = getPreferredShellConfig(); |
| 47 | + |
| 48 | + if (!shellConfig) { |
| 49 | + error('Could not detect shell configuration file.'); |
| 50 | + info('Supported shells: zsh, bash'); |
| 51 | + |
| 52 | + if (!toolbox.parameters.options.fromGluegunMenu) { |
| 53 | + process.exit(1); |
| 54 | + } |
| 55 | + return 'shortcuts: no shell config found'; |
| 56 | + } |
| 57 | + |
| 58 | + info(`Shell: ${shellConfig.shell}`); |
| 59 | + info(`Config: ${shellConfig.path}`); |
| 60 | + info(''); |
| 61 | + |
| 62 | + // Check which shortcuts are already installed |
| 63 | + const existingAliases: ShellAlias[] = []; |
| 64 | + const missingAliases: ShellAlias[] = []; |
| 65 | + |
| 66 | + for (const shortcut of CLAUDE_SHORTCUTS) { |
| 67 | + if (checkAliasInFile(shellConfig.path, shortcut.alias)) { |
| 68 | + existingAliases.push(shortcut); |
| 69 | + } else { |
| 70 | + missingAliases.push(shortcut); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // Show status |
| 75 | + if (existingAliases.length > 0) { |
| 76 | + info('Already installed:'); |
| 77 | + for (const { alias, description } of existingAliases) { |
| 78 | + info(` ${alias} - ${description}`); |
| 79 | + } |
| 80 | + info(''); |
| 81 | + } |
| 82 | + |
| 83 | + if (missingAliases.length === 0) { |
| 84 | + success('All Claude Code shortcuts are already installed!'); |
| 85 | + info(''); |
| 86 | + info('Available shortcuts:'); |
| 87 | + for (const { alias, command, description } of CLAUDE_SHORTCUTS) { |
| 88 | + info(` ${alias} - ${description}`); |
| 89 | + info(` ${command}`); |
| 90 | + } |
| 91 | + |
| 92 | + if (!toolbox.parameters.options.fromGluegunMenu) { |
| 93 | + process.exit(0); |
| 94 | + } |
| 95 | + return 'shortcuts: already installed'; |
| 96 | + } |
| 97 | + |
| 98 | + // Add missing aliases |
| 99 | + const added = addAliasBlockToShellConfig(shellConfig.path, missingAliases); |
| 100 | + |
| 101 | + if (added) { |
| 102 | + info(''); |
| 103 | + success(`Added ${missingAliases.length} shortcut${missingAliases.length > 1 ? 's' : ''} to ${shellConfig.path}`); |
| 104 | + info(''); |
| 105 | + info(`Run: source ${shellConfig.path}`); |
| 106 | + info('Or restart your terminal to apply changes.'); |
| 107 | + } else { |
| 108 | + error(`Failed to write to ${shellConfig.path}`); |
| 109 | + info(''); |
| 110 | + info('To add manually, add these lines to your shell config:'); |
| 111 | + info(''); |
| 112 | + for (const { alias, command } of missingAliases) { |
| 113 | + info(`alias ${alias}='${command}'`); |
| 114 | + } |
| 115 | + |
| 116 | + if (!toolbox.parameters.options.fromGluegunMenu) { |
| 117 | + process.exit(1); |
| 118 | + } |
| 119 | + return 'shortcuts: write failed'; |
| 120 | + } |
| 121 | + |
| 122 | + if (!toolbox.parameters.options.fromGluegunMenu) { |
| 123 | + process.exit(0); |
| 124 | + } |
| 125 | + return `shortcuts: ${missingAliases.length} added`; |
| 126 | + }, |
| 127 | +}; |
| 128 | + |
| 129 | +export default ShortcutsCommand; |
0 commit comments