diff --git a/lib/read.ps1 b/lib/read.ps1 index 938283c..c95b712 100644 --- a/lib/read.ps1 +++ b/lib/read.ps1 @@ -45,14 +45,20 @@ foreach ($arg in $argList) { # **NOTE** Don't include special characters of DOS in $command when $getRes is True. # [string] $cmdPath = $Env:ComSpec # [string] $psPath = 'powershell.exe' -function execWithTTY ($command, $getRes = $False) { +function execWithTTY ($command, $getRes = $False, $errorThrow = $False) { if ($getRes) { $res = (cmd.exe /C "CON powershell.exe -Command -" - if ($LastExitCode -ne 0) { exit $LastExitCode } + if ($LastExitCode -ne 0) { + if ($errorThrow) { throw $LastExitCode } + else { exit $LastExitCode } + } } } @@ -67,10 +73,12 @@ if ($options.display) { if ($options.displayOnly) { return "''" } if (-not $options.keyIn -and $options.hideEchoBack -and $options.mask -eq '*') { - $inputTTY = execWithTTY ('$text = Read-Host -AsSecureString;' + - '$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($text);' + - '[Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)') $True - return '''' + $inputTTY + '''' + try { + $inputTTY = execWithTTY ('$text = Read-Host -AsSecureString;' + + '$bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($text);' + + '[Runtime.InteropServices.Marshal]::PtrToStringAuto($bstr)') $True $True + return '''' + $inputTTY + '''' + } catch {} # ignore } if ($options.keyIn) { $reqSize = 1 } diff --git a/package.json b/package.json index 9a4d4ea..dc51539 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "readline-sync", - "version": "1.2.20", + "version": "1.2.21", "title": "readlineSync", "description": "Synchronous Readline for interactively running to have a conversation with the user via a console(TTY).", "keywords": [