172 lines
5.6 KiB
PowerShell
172 lines
5.6 KiB
PowerShell
# Script-level parameters and cmdlet binding to support -Verbose and common parameters
|
|
[CmdletBinding()]
|
|
param (
|
|
[switch]$Quiet
|
|
)
|
|
|
|
function Write-LogMessage {
|
|
param (
|
|
[string]$message,
|
|
[ValidateSet('Info','Warning')] [string]$Level = 'Info'
|
|
)
|
|
|
|
if ($Quiet) { return }
|
|
|
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
|
if ($Level -eq 'Warning') {
|
|
Write-Host "[$timestamp] WARNING: $message"
|
|
}
|
|
else {
|
|
Write-Host "[$timestamp] $message"
|
|
}
|
|
}
|
|
|
|
function Save-ElxInternals {
|
|
param(
|
|
[ValidateScript({ Test-Path $_ -PathType Leaf })][Parameter(Mandatory)]$ModulePath
|
|
)
|
|
|
|
if (-not $Quiet) {
|
|
Write-Host "Switching to ELX Tools directory..."
|
|
}
|
|
|
|
$extractElxToolsScriptPath = Join-Path $PSScriptRoot "../tools/extract-elx/"
|
|
Push-Location -StackName ElxTools $extractElxToolsScriptPath
|
|
|
|
if (-not $Quiet) {
|
|
Write-Host "Executing ELX extraction..."
|
|
}
|
|
|
|
& node "extract-elx.js" $ModulePath --clean
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Error "ELX extraction failed with exit code $LASTEXITCODE."
|
|
exit 1
|
|
}
|
|
|
|
if (-not $Quiet) {
|
|
Write-Host "ELX extraction completed successfully!" -ForegroundColor Green
|
|
Write-Host "Switching back to original directory..."
|
|
}
|
|
|
|
Pop-Location -StackName ElxTools
|
|
}
|
|
|
|
function New-EnvFileContent {
|
|
param(
|
|
[ValidateNotNullOrEmpty()][Parameter(Mandatory)][string]$ModuleTag
|
|
)
|
|
|
|
$envFileScriptPath = Join-Path $PSScriptRoot "../"
|
|
# Load existing .env if present
|
|
$envPath = Join-Path $envFileScriptPath '.env'
|
|
|
|
if (-not (Test-Path $envPath)) {
|
|
Write-Error "No existing .env file found!"
|
|
exit 1
|
|
}
|
|
|
|
$envValues = @{}
|
|
$envLines = Get-Content -Raw -Path $envPath -ErrorAction Stop
|
|
Write-Host $envLines
|
|
foreach ($line in $envLines -split "`n") {
|
|
$l = $line.Trim()
|
|
if ($l -eq '' -or $l.StartsWith('#') -or $l.StartsWith(';')) { continue }
|
|
if ($l -match '=') {
|
|
$parts = $l -split '=', 2
|
|
$name = $parts[0].Trim()
|
|
$value = $parts[1].Trim().Trim("'").Trim('"')
|
|
$envValues[$name] = $value
|
|
}
|
|
if ($name -eq 'ELIXFORMS_MODULE_TAG') {
|
|
$envValues[$name] = $ModuleTag
|
|
}
|
|
}
|
|
$out = foreach ($name in $settingNames) { "$name='$($envValues[$name])'" }
|
|
$out -join "`n"
|
|
}
|
|
|
|
function Export-ElxModule {
|
|
param(
|
|
[ValidateNotNullOrEmpty()][Parameter(Mandatory)][string]$ModuleTag,
|
|
[ValidateScript({ Test-Path $_ })][Parameter(Mandatory)]$ModulePath
|
|
)
|
|
|
|
if (-not $Quiet) {
|
|
Write-Host "Switching to Selenium directory..."
|
|
}
|
|
|
|
$seleniumToolsScriptPath = Join-Path $PSScriptRoot "../tools/selenium/"
|
|
Push-Location -StackName Selenium $seleniumToolsScriptPath
|
|
|
|
if (-not $Quiet) {
|
|
Write-Host "Executing module export for '$ModuleTag'..."
|
|
}
|
|
|
|
#HACK: il tool prende in ingresso un env file
|
|
$tmpEnvFile = [System.IO.Path]::GetTempFileName()
|
|
|
|
New-EnvFileContent -ModuleTag $ModuleTag | Set-Content -LiteralPath $tmpEnvFile -Encoding UTF8
|
|
|
|
& npx env-cmd --file "$tmpEnvFile" -- npm run export
|
|
|
|
Remove-Item -LiteralPath $tmpEnvFile -Force -ErrorAction SilentlyContinue
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
Write-Error "Module export for '$ModuleTag' failed with exit code $LASTEXITCODE."
|
|
exit 1
|
|
}
|
|
|
|
if (-not $Quiet) {
|
|
Write-Host "Module export for '$ModuleTag' completed successfully!" -ForegroundColor Green
|
|
Write-Host "Switching back to original directory..."
|
|
}
|
|
|
|
Pop-Location -StackName Selenium
|
|
}
|
|
|
|
function Convert-UrlToElixAutocompleteFormat {
|
|
param (
|
|
[Parameter(Mandatory)][ValidateNotNullOrEmpty()][uri]$AbsoluteUrl
|
|
)
|
|
|
|
Write-LogMessage "Converting URL '$($AbsoluteUrl.Host)' to elixForms WS call format..."
|
|
$domain = $AbsoluteUrl.Host -replace '-', '--' -replace '\.', '-'
|
|
$path = $AbsoluteUrl.AbsolutePath.TrimStart('/') -replace '-', '--' -replace '/', '-'
|
|
$url = "/eF/services/api/remote/$domain/$path/v1"
|
|
|
|
# Preserve query parameter order using an ordered hashtable and URL-decode values
|
|
$queryParams = [ordered]@{}
|
|
$rawQuery = $AbsoluteUrl.Query.TrimStart('?')
|
|
if ($rawQuery) {
|
|
$rawQuery.Split('&') | ForEach-Object {
|
|
if ($_ -ne '') {
|
|
$pair = $_ -split '=', 2
|
|
$key = $pair[0]
|
|
$value = if ($pair.Count -gt 1) { $pair[1] } else { '' }
|
|
|
|
if ($queryParams.Contains($key)) {
|
|
# Append duplicate values with a comma, preserving existing empty-string handling
|
|
if ($queryParams[$key] -eq '') {
|
|
$queryParams[$key] = $value
|
|
}
|
|
else {
|
|
$queryParams[$key] = "{0},{1}" -f $queryParams[$key], $value
|
|
}
|
|
}
|
|
else {
|
|
$queryParams[$key] = $value
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
[PSCustomObject]@{
|
|
URL = $url
|
|
QueryParams = $queryParams
|
|
QueryString = ($queryParams.Keys | ForEach-Object { "$_=$($queryParams[$_])" }) -Join "&"
|
|
} | ConvertTo-Json -Depth 2 -EscapeHandling None
|
|
}
|
|
|
|
Export-ModuleMember -Function Convert-UrlToElixAutocompleteFormat
|
|
Export-ModuleMember -Function Save-ElxInternals
|
|
Export-ModuleMember -Function Export-ElxModule |