* Execute echo $0 command and extract the shell name from the output * For Windows shells, use appropriate commands
(shellPath = null)
| 90 | * For Windows shells, use appropriate commands |
| 91 | */ |
| 92 | async function getShellFromCommand(shellPath = null) { |
| 93 | try { |
| 94 | let command = 'echo $0'; |
| 95 | |
| 96 | // Use different commands for Windows shells |
| 97 | if (shellPath === 'cmd' || shellPath === 'cmd.exe') { |
| 98 | command = 'echo %0'; |
| 99 | } else if (shellPath === 'pwsh' || shellPath === 'powershell') { |
| 100 | command = 'Write-Host $MyInvocation.MyCommand.Name'; |
| 101 | } |
| 102 | |
| 103 | const result = await executeCommand(command, 2000); |
| 104 | |
| 105 | // Extract shell name from the result |
| 106 | if (result.content && result.content[0] && result.content[0].text) { |
| 107 | const output = result.content[0].text; |
| 108 | // Look for the shell name in the output, handling both PID line and actual output |
| 109 | const lines = output.split('\n').filter(line => line.trim() !== ''); |
| 110 | |
| 111 | // Find the line that contains the actual shell output (not the PID line, Command started, or Initial output) |
| 112 | for (const line of lines) { |
| 113 | if (!line.includes('PID') && |
| 114 | !line.includes('Command started') && |
| 115 | !line.includes('Initial output:') && |
| 116 | line.trim() !== '') { |
| 117 | return line.trim(); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | throw new Error('Could not extract shell name from command output'); |
| 123 | } catch (error) { |
| 124 | console.error('Error executing shell command:', error); |
| 125 | throw error; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Setup function to prepare the test environment |
no test coverage detected