Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions src/helpers/os-detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down