From ab292c0bed1d23bbc9889137a4f1ad4ba78736aa Mon Sep 17 00:00:00 2001 From: euyu Date: Thu, 23 Apr 2026 20:44:49 +0800 Subject: [PATCH] fix: detect Windows shell from environment --- src/helpers/os-detect.ts | 47 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/helpers/os-detect.ts b/src/helpers/os-detect.ts index a33b5041..ecb9c314 100644 --- a/src/helpers/os-detect.ts +++ b/src/helpers/os-detect.ts @@ -2,13 +2,54 @@ import os from 'os'; import path from 'path'; import i18n from './i18n'; +function trimExecutableExtension(shell: string) { + return shell.replace(/\.(exe|cmd)$/i, '').toLowerCase(); +} + +function shellNameFromPath(shellPath: string) { + return trimExecutableExtension( + path.win32.basename(shellPath) || path.posix.basename(shellPath) + ); +} + +function normalizeWindowsShell(shellName: string) { + if (shellName === 'pwsh' || shellName === 'powershell') { + return 'powershell'; + } + + if (shellName === 'cmd') { + return 'cmd'; + } + + if (shellName === 'bash' || shellName === 'zsh' || shellName === 'fish') { + return shellName; + } +} + +function detectWindowsShell(env: NodeJS.ProcessEnv) { + const shellFromEnv = env.SHELL + ? normalizeWindowsShell(shellNameFromPath(env.SHELL)) + : undefined; + if (shellFromEnv) { + return shellFromEnv; + } + + const comSpecShell = env.ComSpec + ? normalizeWindowsShell(shellNameFromPath(env.ComSpec)) + : undefined; + if (comSpecShell) { + return comSpecShell; + } + + return 'powershell'; +} + export function detectShell() { try { - // Detect if we're running on win32 and assume powershell if (os.platform() === 'win32') { - return 'powershell'; + return detectWindowsShell(process.env); } - // otherwise return current shell; default to bash + return path.basename(os.userInfo().shell ?? 'bash'); } catch (err: unknown) { if (err instanceof Error) {