hooks/prepend-profile.ps1

# Read tool input from stdin
$json = [Console]::In.ReadToEnd() | ConvertFrom-Json
$command = $json.tool_input.command

# Prepend profile sourcing unless already present, and wrap the command in a
# dot-sourced script block so all pipeline output (objects and strings) is piped
# through a formatter. `. { }` runs in the current scope so variables, functions,
# and location changes persist across tool invocations. Objects are converted via
# Out-String so they appear as text in the captured output, just like a terminal.
if (-not $command.StartsWith('. $env:PWRCLAUDE_PROFILE')) {
  $command = '. $env:PWRCLAUDE_PROFILE; . { ' + $command + ' } | ForEach-Object { if ($_ -isnot [string]) { $_ | Out-String -Stream | Where-Object { $_ } } else { $_ } }'
}

@{
  hookSpecificOutput = @{
    hookEventName      = 'PreToolUse'
    permissionDecision = 'allow'
    updatedInput       = @{ command = $command }
  }
} | ConvertTo-Json -Depth 10 -Compress