File size: 8,963 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | /**
* Package manager detection for Claude CLI
*/
import { readFile } from 'fs/promises'
import memoize from 'lodash-es/memoize.js'
import { logForDebugging } from '../debug.js'
import { execFileNoThrow } from '../execFileNoThrow.js'
import { getPlatform } from '../platform.js'
export type PackageManager =
| 'homebrew'
| 'winget'
| 'pacman'
| 'deb'
| 'rpm'
| 'apk'
| 'mise'
| 'asdf'
| 'unknown'
/**
* Parses /etc/os-release to extract the distro ID and ID_LIKE fields.
* ID_LIKE identifies the distro family (e.g. Ubuntu has ID_LIKE=debian),
* letting us skip package manager execs on distros that can't have them.
* Returns null if the file is unreadable (pre-systemd or non-standard systems);
* callers fall through to the exec in that case as a conservative fallback.
*/
export const getOsRelease = memoize(
async (): Promise<{ id: string; idLike: string[] } | null> => {
try {
const content = await readFile('/etc/os-release', 'utf8')
const idMatch = content.match(/^ID=["']?(\S+?)["']?\s*$/m)
const idLikeMatch = content.match(/^ID_LIKE=["']?(.+?)["']?\s*$/m)
return {
id: idMatch?.[1] ?? '',
idLike: idLikeMatch?.[1]?.split(' ') ?? [],
}
} catch {
return null
}
},
)
function isDistroFamily(
osRelease: { id: string; idLike: string[] },
families: string[],
): boolean {
return (
families.includes(osRelease.id) ||
osRelease.idLike.some(like => families.includes(like))
)
}
/**
* Detects if the currently running Claude instance was installed via mise
* (a polyglot tool version manager) by checking if the executable path
* is within a mise installs directory.
*
* mise installs to: ~/.local/share/mise/installs/<tool>/<version>/
*/
export function detectMise(): boolean {
const execPath = process.execPath || process.argv[0] || ''
// Check if the executable is within a mise installs directory
if (/[/\\]mise[/\\]installs[/\\]/i.test(execPath)) {
logForDebugging(`Detected mise installation: ${execPath}`)
return true
}
return false
}
/**
* Detects if the currently running Claude instance was installed via asdf
* (another polyglot tool version manager) by checking if the executable path
* is within an asdf installs directory.
*
* asdf installs to: ~/.asdf/installs/<tool>/<version>/
*/
export function detectAsdf(): boolean {
const execPath = process.execPath || process.argv[0] || ''
// Check if the executable is within an asdf installs directory
if (/[/\\]\.?asdf[/\\]installs[/\\]/i.test(execPath)) {
logForDebugging(`Detected asdf installation: ${execPath}`)
return true
}
return false
}
/**
* Detects if the currently running Claude instance was installed via Homebrew
* by checking if the executable path is within a Homebrew Caskroom directory.
*
* Note: We specifically check for Caskroom because npm can also be installed via
* Homebrew, which would place npm global packages under the same Homebrew prefix
* (e.g., /opt/homebrew/lib/node_modules). We need to distinguish between:
* - Homebrew cask: /opt/homebrew/Caskroom/claude-code/...
* - npm-global (via Homebrew's npm): /opt/homebrew/lib/node_modules/@anthropic-ai/...
*/
export function detectHomebrew(): boolean {
const platform = getPlatform()
// Homebrew is only for macOS and Linux
if (platform !== 'macos' && platform !== 'linux' && platform !== 'wsl') {
return false
}
// Get the path of the currently running executable
const execPath = process.execPath || process.argv[0] || ''
// Check if the executable is within a Homebrew Caskroom directory
// This is specific to Homebrew cask installations
if (execPath.includes('/Caskroom/')) {
logForDebugging(`Detected Homebrew cask installation: ${execPath}`)
return true
}
return false
}
/**
* Detects if the currently running Claude instance was installed via winget
* by checking if the executable path is within a WinGet directory.
*
* Winget installs to:
* - User: %LOCALAPPDATA%\Microsoft\WinGet\Packages
* - System: C:\Program Files\WinGet\Packages
* And creates links at: %LOCALAPPDATA%\Microsoft\WinGet\Links\
*/
export function detectWinget(): boolean {
const platform = getPlatform()
// Winget is only for Windows
if (platform !== 'windows') {
return false
}
const execPath = process.execPath || process.argv[0] || ''
// Check for WinGet paths (handles both forward and backslashes)
const wingetPatterns = [
/Microsoft[/\\]WinGet[/\\]Packages/i,
/Microsoft[/\\]WinGet[/\\]Links/i,
]
for (const pattern of wingetPatterns) {
if (pattern.test(execPath)) {
logForDebugging(`Detected winget installation: ${execPath}`)
return true
}
}
return false
}
/**
* Detects if the currently running Claude instance was installed via pacman
* by querying pacman's database for file ownership.
*
* We gate on the Arch distro family before invoking pacman. On other distros
* like Ubuntu/Debian, 'pacman' in PATH may resolve to the pacman game
* (/usr/games/pacman) rather than the Arch package manager.
*/
export const detectPacman = memoize(async (): Promise<boolean> => {
const platform = getPlatform()
if (platform !== 'linux') {
return false
}
const osRelease = await getOsRelease()
if (osRelease && !isDistroFamily(osRelease, ['arch'])) {
return false
}
const execPath = process.execPath || process.argv[0] || ''
const result = await execFileNoThrow('pacman', ['-Qo', execPath], {
timeout: 5000,
useCwd: false,
})
if (result.code === 0 && result.stdout) {
logForDebugging(`Detected pacman installation: ${result.stdout.trim()}`)
return true
}
return false
})
/**
* Detects if the currently running Claude instance was installed via a .deb package
* by querying dpkg's database for file ownership.
*
* We use `dpkg -S <execPath>` to check if the executable is owned by a dpkg-managed package.
*/
export const detectDeb = memoize(async (): Promise<boolean> => {
const platform = getPlatform()
if (platform !== 'linux') {
return false
}
const osRelease = await getOsRelease()
if (osRelease && !isDistroFamily(osRelease, ['debian'])) {
return false
}
const execPath = process.execPath || process.argv[0] || ''
const result = await execFileNoThrow('dpkg', ['-S', execPath], {
timeout: 5000,
useCwd: false,
})
if (result.code === 0 && result.stdout) {
logForDebugging(`Detected deb installation: ${result.stdout.trim()}`)
return true
}
return false
})
/**
* Detects if the currently running Claude instance was installed via an RPM package
* by querying the RPM database for file ownership.
*
* We use `rpm -qf <execPath>` to check if the executable is owned by an RPM package.
*/
export const detectRpm = memoize(async (): Promise<boolean> => {
const platform = getPlatform()
if (platform !== 'linux') {
return false
}
const osRelease = await getOsRelease()
if (osRelease && !isDistroFamily(osRelease, ['fedora', 'rhel', 'suse'])) {
return false
}
const execPath = process.execPath || process.argv[0] || ''
const result = await execFileNoThrow('rpm', ['-qf', execPath], {
timeout: 5000,
useCwd: false,
})
if (result.code === 0 && result.stdout) {
logForDebugging(`Detected rpm installation: ${result.stdout.trim()}`)
return true
}
return false
})
/**
* Detects if the currently running Claude instance was installed via Alpine APK
* by querying apk's database for file ownership.
*
* We use `apk info --who-owns <execPath>` to check if the executable is owned
* by an apk-managed package.
*/
export const detectApk = memoize(async (): Promise<boolean> => {
const platform = getPlatform()
if (platform !== 'linux') {
return false
}
const osRelease = await getOsRelease()
if (osRelease && !isDistroFamily(osRelease, ['alpine'])) {
return false
}
const execPath = process.execPath || process.argv[0] || ''
const result = await execFileNoThrow(
'apk',
['info', '--who-owns', execPath],
{
timeout: 5000,
useCwd: false,
},
)
if (result.code === 0 && result.stdout) {
logForDebugging(`Detected apk installation: ${result.stdout.trim()}`)
return true
}
return false
})
/**
* Memoized function to detect which package manager installed Claude
* Returns 'unknown' if no package manager is detected
*/
export const getPackageManager = memoize(async (): Promise<PackageManager> => {
if (detectHomebrew()) {
return 'homebrew'
}
if (detectWinget()) {
return 'winget'
}
if (detectMise()) {
return 'mise'
}
if (detectAsdf()) {
return 'asdf'
}
if (await detectPacman()) {
return 'pacman'
}
if (await detectApk()) {
return 'apk'
}
if (await detectDeb()) {
return 'deb'
}
if (await detectRpm()) {
return 'rpm'
}
return 'unknown'
})
|