|
| 1 | +import os from 'node:os'; |
| 2 | +import path from 'node:path'; |
| 3 | + |
| 4 | +export enum Shell { |
| 5 | + ZSH = 'zsh', |
| 6 | + BASH = 'bash', |
| 7 | + SH = 'sh', |
| 8 | + KSH = 'ksh', |
| 9 | + CSH = 'csh', |
| 10 | + FISH = 'fish', |
| 11 | +} |
| 12 | + |
| 13 | +export interface SystemInfo { |
| 14 | + os: 'Darwin' | 'Linux' | 'Windows_NT'; |
| 15 | + shell: Shell; |
| 16 | +} |
| 17 | + |
| 18 | +export const Utils = { |
| 19 | + getUser(): string { |
| 20 | + return os.userInfo().username; |
| 21 | + }, |
| 22 | + |
| 23 | + getSystemInfo() { |
| 24 | + return { |
| 25 | + os: os.type(), |
| 26 | + shell: this.getShell(), |
| 27 | + } |
| 28 | + }, |
| 29 | + |
| 30 | + isMacOS(): boolean { |
| 31 | + return os.platform() === 'darwin'; |
| 32 | + }, |
| 33 | + |
| 34 | + isLinux(): boolean { |
| 35 | + return os.platform() === 'linux'; |
| 36 | + }, |
| 37 | + |
| 38 | + getShell(): Shell | undefined { |
| 39 | + const shell = process.env.SHELL || ''; |
| 40 | + |
| 41 | + if (shell.endsWith('bash')) { |
| 42 | + return Shell.BASH |
| 43 | + } |
| 44 | + |
| 45 | + if (shell.endsWith('zsh')) { |
| 46 | + return Shell.ZSH |
| 47 | + } |
| 48 | + |
| 49 | + if (shell.endsWith('sh')) { |
| 50 | + return Shell.SH |
| 51 | + } |
| 52 | + |
| 53 | + if (shell.endsWith('csh')) { |
| 54 | + return Shell.CSH |
| 55 | + } |
| 56 | + |
| 57 | + if (shell.endsWith('ksh')) { |
| 58 | + return Shell.KSH |
| 59 | + } |
| 60 | + |
| 61 | + if (shell.endsWith('fish')) { |
| 62 | + return Shell.FISH |
| 63 | + } |
| 64 | + |
| 65 | + return undefined; |
| 66 | + }, |
| 67 | + |
| 68 | + |
| 69 | + getPrimaryShellRc(): string { |
| 70 | + return this.getShellRcFiles()[0]; |
| 71 | + }, |
| 72 | + |
| 73 | + getShellRcFiles(): string[] { |
| 74 | + const shell = process.env.SHELL || ''; |
| 75 | + const homeDir = os.homedir(); |
| 76 | + |
| 77 | + if (shell.endsWith('bash')) { |
| 78 | + // Linux typically uses .bashrc, macOS uses .bash_profile |
| 79 | + if (Utils.isLinux()) { |
| 80 | + return [ |
| 81 | + path.join(homeDir, '.bashrc'), |
| 82 | + path.join(homeDir, '.bash_profile'), |
| 83 | + path.join(homeDir, '.profile'), |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + return [ |
| 88 | + path.join(homeDir, '.bash_profile'), |
| 89 | + path.join(homeDir, '.bashrc'), |
| 90 | + path.join(homeDir, '.profile'), |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + if (shell.endsWith('zsh')) { |
| 95 | + return [ |
| 96 | + path.join(homeDir, '.zshrc'), |
| 97 | + path.join(homeDir, '.zprofile'), |
| 98 | + path.join(homeDir, '.zshenv'), |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + if (shell.endsWith('sh')) { |
| 103 | + return [ |
| 104 | + path.join(homeDir, '.profile'), |
| 105 | + ] |
| 106 | + } |
| 107 | + |
| 108 | + if (shell.endsWith('ksh')) { |
| 109 | + return [ |
| 110 | + path.join(homeDir, '.profile'), |
| 111 | + path.join(homeDir, '.kshrc'), |
| 112 | + ] |
| 113 | + } |
| 114 | + |
| 115 | + if (shell.endsWith('csh')) { |
| 116 | + return [ |
| 117 | + path.join(homeDir, '.cshrc'), |
| 118 | + path.join(homeDir, '.login'), |
| 119 | + path.join(homeDir, '.logout'), |
| 120 | + ] |
| 121 | + } |
| 122 | + |
| 123 | + if (shell.endsWith('fish')) { |
| 124 | + return [ |
| 125 | + path.join(homeDir, '.config/fish/config.fish'), |
| 126 | + ] |
| 127 | + } |
| 128 | + |
| 129 | + // Default to bash-style files |
| 130 | + return [ |
| 131 | + path.join(homeDir, '.bashrc'), |
| 132 | + path.join(homeDir, '.bash_profile'), |
| 133 | + path.join(homeDir, '.profile'), |
| 134 | + ]; |
| 135 | + }, |
| 136 | +}; |
| 137 | + |
| 138 | + |
| 139 | + |
0 commit comments