param(
    [string]$publishPath
)

# Find compiled JavaScript file
$assetsPath = Join-Path -Path $publishPath -ChildPath "assets"
$jsFile = Get-ChildItem -Path $assetsPath -Filter "index-*.js"
if (-not $jsFile) {
    exit 1
}

# Find version string in the format: {COMMIT_DATE}.{REPOSITORY_NAME}.{BRANCH_NAME}+{COMMIT_ID}
# Might be in format: {CURRENT_DATE}.{REPOSITORY_NAME}
$content = Get-Content $jsFile.FullName -Raw
if (($content -match '"(\d{4}-\d{2}-\d{2}\.[^.]+\.[^+]+\+[a-fA-F0-9]+)"') -or ($content -match '"(\d{4}-\d{2}-\d{2}\.[a-zA-Z0-9_-]+)"')) {
    $version = $Matches[1]
    Write-Host "$version"
    exit 0
}

exit 1