From b81b63a4b9b7b4d09bdef5a2d1d63d2877a876a7 Mon Sep 17 00:00:00 2001 From: Pier-Paolo Mammi Date: Thu, 28 May 2026 12:28:55 +0200 Subject: [PATCH] move node install to setup environment script --- scripts/extract-elx-module.psm1 | 12 --- scripts/setup-environment.ps1 | 151 +++++++++++++++++++------------- 2 files changed, 90 insertions(+), 73 deletions(-) diff --git a/scripts/extract-elx-module.psm1 b/scripts/extract-elx-module.psm1 index 3ed65e3..92af8e5 100644 --- a/scripts/extract-elx-module.psm1 +++ b/scripts/extract-elx-module.psm1 @@ -34,18 +34,6 @@ function Save-ElxInternals { Push-Location -StackName ElxTools $extractElxToolsScriptPath if (-not $Quiet) { - Write-Host "Installing Node.js dependencies for the ELX Tools..." - } - - # Install Node.js dependencies for the ELX Tools - & npm install - if ($LASTEXITCODE -ne 0) { - Write-Error "npm install failed with exit code $LASTEXITCODE." - exit 1 - } - - if (-not $Quiet) { - Write-Host "Node.js dependencies installed successfully!" -ForegroundColor Cyan Write-Host "Executing ELX extraction..." } diff --git a/scripts/setup-environment.ps1 b/scripts/setup-environment.ps1 index 37b6569..ea7b603 100644 --- a/scripts/setup-environment.ps1 +++ b/scripts/setup-environment.ps1 @@ -1,3 +1,87 @@ +function Initialize-EnvironmentSettings { + Write-Host "Initializing settings..." + + # Load and validate .env.template (INI-style key=value list) + $parentDir = Split-Path -Parent $PSScriptRoot + $templatePath = Join-Path $parentDir '.env.template' + if (-not (Test-Path $templatePath)) { + Write-Error "Template file '$templatePath' not found." + exit 1 + } + + $templateLines = Get-Content -Raw -Path $templatePath -ErrorAction Stop + $settingNames = @() + foreach ($line in $templateLines -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() + if ($name -ne '') { $settingNames += $name } + } + } + if ($settingNames.Count -eq 0) { + Write-Error "No settings found in template file '$templatePath'." + exit 1 + } + + # Load existing .env if present + $envPath = Join-Path $parentDir '.env' + $envValues = @{} + if (Test-Path $envPath) { + $envLines = Get-Content -Raw -Path $envPath -ErrorAction Stop + 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 + } + } + } + + # Ensure all template settings are present; prompt user for missing/empty + foreach ($name in $settingNames) { + if (-not $envValues.ContainsKey($name) -or [string]::IsNullOrWhiteSpace($envValues[$name])) { + do { + Write-Host "$name value is missing!" -ForegroundColor Yellow + $settingValue = Read-Host "Enter value for $name" + } while ([string]::IsNullOrWhiteSpace($settingValue)) + $envValues[$name] = $settingValue + } + else { + Write-Host "$name value already set to '$($envValues[$name])'" -ForegroundColor DarkGray + } + } + + # Recreate ../.env with the required settings and values + $out = foreach ($name in $settingNames) { "$name='$($envValues[$name])'" } + $out | Set-Content -Path $envPath -Encoding UTF8 + + Write-Host "Settings preparation complete" +} + +function Initialize-NodeTools { + Write-Host "Installing Node.js dependencies for the ELX Tools..." + + $extractElxToolsScriptPath = Join-Path $PSScriptRoot "../tools/extract-elx/" + + Push-Location -StackName ElxTools $extractElxToolsScriptPath + + # Install Node.js dependencies for the ELX Tools + & npm install + if ($LASTEXITCODE -ne 0) { + Write-Error "npm install failed with exit code $LASTEXITCODE." + exit 1 + } + + Pop-Location -StackName ElxTools + + Write-Host "Node.js dependencies installed successfully!" +} + # If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { # #"No Administrative rights, it will display a popup window asking user for Admin rights" # $arguments = "-NoProfile -ExecutionPolicy Bypass -Command & '" + $myinvocation.mycommand.definition + "'" @@ -23,71 +107,16 @@ # Import-Module -Force $envSetupModulePath Write-Host "Preparing environment..." -ForegroundColor Cyan +Write-Host -Write-Host "Preparing settings..." -ForegroundColor Gray +Initialize-EnvironmentSettings -# Load and validate .env.template (INI-style key=value list) -$parentDir = Split-Path -Parent $PSScriptRoot -$templatePath = Join-Path $parentDir '.env.template' -if (-not (Test-Path $templatePath)) { - Write-Error "Template file '$templatePath' not found." - exit 1 -} +Write-Host -$templateLines = Get-Content -Raw -Path $templatePath -ErrorAction Stop -$settingNames = @() -foreach ($line in $templateLines -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() - if ($name -ne '') { $settingNames += $name } - } -} -if ($settingNames.Count -eq 0) { - Write-Error "No settings found in template file '$templatePath'." - exit 1 -} +Initialize-NodeTools -# Load existing .env if present -$envPath = Join-Path $parentDir '.env' -$envValues = @{} -if (Test-Path $envPath) { - $envLines = Get-Content -Raw -Path $envPath -ErrorAction Stop - 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 - } - } -} - -# Ensure all template settings are present; prompt user for missing/empty -foreach ($name in $settingNames) { - if (-not $envValues.ContainsKey($name) -or [string]::IsNullOrWhiteSpace($envValues[$name])) { - do { - Write-Host "$name value is missing!" -ForegroundColor Yellow - $input = Read-Host "Enter value for $name" - } while ([string]::IsNullOrWhiteSpace($input)) - $envValues[$name] = $input - } - else { - Write-Host "$name value already set to '$($envValues[$name])'" -ForegroundColor DarkGray - } -} - -# Recreate ../.env with the required settings and values -$out = foreach ($name in $settingNames) { "$name='$($envValues[$name])'" } -$out | Set-Content -Path $envPath -Encoding UTF8 - -Write-Host "Settings preparation complete" -ForegroundColor Gray - -Write-Host "Environment preparation complete!" -ForegroundColor Cyan +Write-Host +Write-Host "Environment preparation complete!" -ForegroundColor Green # do { # Write-Host "To complete the intallations you need to restart your computer" -ForegroundColor Cyan