build1.ps1

function Build-ModuleLocally {
    [CmdletBinding()]
    param ()

    $ModuleRoot = Get-Location
    $psd1Path = Get-ChildItem -Path $ModuleRoot -Filter *.psd1 | Select-Object -First 1

    if (-not $psd1Path) {
        Write-Error "❌ No .psd1 file found in current directory '$ModuleRoot'."
        return
    }

    $psd1FullPath = $psd1Path.FullName
    $psm1FullPath = [System.IO.Path]::ChangeExtension($psd1FullPath, ".psm1")
    $ModuleName = $psd1Path.BaseName
    $TempPrefix = "_temp"

    $PublicPath = Join-Path $ModuleRoot 'Public'
    $PrivatePath = Join-Path $ModuleRoot 'Private'

    if (-not (Test-Path $PublicPath)) {
        Write-Warning "⚠️ Public folder not found at $PublicPath"
    }

    if (-not (Test-Path $PrivatePath)) {
        Write-Warning "⚠️ Private folder not found at $PrivatePath"
    }

    # Rebuild PSM1
    Write-Host "🔨 Rebuilding $($psm1FullPath)..."
    "# Auto-generated on $(Get-Date)" | Set-Content $psm1FullPath

    if (Test-Path $PrivatePath) {
        Get-ChildItem -Path $PrivatePath -Filter *.ps1 | ForEach-Object {
            Add-Content -Path $psm1FullPath -Value "`n# Private: $($_.Name)`n$(Get-Content $_ -Raw)"
        }
    }

    if (Test-Path $PublicPath) {
        Get-ChildItem -Path $PublicPath -Filter *.ps1 | ForEach-Object {
            Add-Content -Path $psm1FullPath -Value "`n# Public: $($_.Name)`n$(Get-Content $_ -Raw)"
        }
    }

    # Update FunctionsToExport in PSD1
    $exportedFunctions = @()
    if (Test-Path $PublicPath) {
        $exportedFunctions = Get-ChildItem "$PublicPath\*.ps1" | Select-Object -ExpandProperty BaseName
    }

    $replacement = ($exportedFunctions | ForEach-Object { "'$_'" }) -join ",`n "

    (Get-Content $psd1FullPath) -replace `
        "(?s)(FunctionsToExport\s*=\s*@\()[^\)]*(\))", `
        "`$1`n $replacement`n $2" |
        Set-Content $psd1FullPath

    # Remove existing temp import
    $moduleToRemove = Get-Module -Name $ModuleName -All | Where-Object { $_.Name -eq "$TempPrefix$ModuleName" }
    if ($moduleToRemove) {
        Remove-Module $moduleToRemove -Force
    }

    # Import with _temp prefix
    Write-Host "📦 Importing module '$ModuleName' with prefix '$TempPrefix'..."
    Import-Module -Name $psd1FullPath -Force -Prefix $TempPrefix

    Write-Host "✅ Module '$ModuleName' is now available in session with prefix '_temp'"
}