Source/Test-DotEnv.ps1
|
<# .SYNOPSIS Tests if a .env file exists and is well-formed. .DESCRIPTION Checks if the specified .env file exists in the file system, and that ConvertFrom-DotEnv can convert its content without throwing. .PARAMETER Path The path to the .env file. If not specified, checks .env in the current directory. .EXAMPLE Test-DotEnv -Path ".env" .EXAMPLE ".env", ".env.local" | Test-DotEnv .INPUTS [System.String] The .env file path. Accepts pipeline input by value or by property name. .OUTPUTS [System.Boolean] True if the .env file exists and every line is well-formed, otherwise False. #> function Test-DotEnv { [OutputType([bool])] [CmdletBinding()] param( [Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)] [string] $Path = ".env" ) process { if (-not (Test-Path -Path $Path)) { return $false } try { ConvertFrom-DotEnv -Text (Get-Content -Path $Path -Raw) | Out-Null return $true } catch { return $false } } } |