Tools/Setup.ps1
function Pode { param ( [Parameter(Mandatory=$true)] [ValidateSet('init', 'test', 'start', 'install', 'build')] [Alias('a')] [string] $Action, [switch] [Alias('d')] $Dev ) # default config file name and content $file = './package.json' $name = Split-Path -Leaf -Path $pwd $data = $null # default config data that's used to populate on init $map = @{ 'name' = $name; 'version' = '1.0.0'; 'description' = ''; 'main' = './server.ps1'; 'scripts' = @{ 'start' = './server.ps1'; 'install' = 'yarn install --force --ignore-scripts --modules-folder pode_modules'; "build" = 'psake'; 'test' = 'invoke-pester ./tests/*.ps1' }; 'author' = ''; 'license' = 'MIT'; } # check and load config if already exists if (Test-Path $file) { $data = (Get-Content $file | ConvertFrom-Json) } # quick check to see if the data is required if ($Action -ine 'init') { if ($null -eq $data) { Write-Host 'package.json file not found' -ForegroundColor Red return } else { $actionScript = $data.scripts.$Action if ([string]::IsNullOrWhiteSpace($actionScript) -and $Action -ieq 'start') { $actionScript = $data.main } if ([string]::IsNullOrWhiteSpace($actionScript) -and $Action -ine 'install') { Write-Host "package.json does not contain a script for the $($Action) action" -ForegroundColor Yellow return } } } else { if ($null -ne $data) { Write-Host 'package.json already exists' -ForegroundColor Yellow return } } switch ($Action.ToLowerInvariant()) { 'init' { $v = Read-Host -Prompt "name ($($map.name))" if (![string]::IsNullOrWhiteSpace($v)) { $map.name = $v } $v = Read-Host -Prompt "version ($($map.version))" if (![string]::IsNullOrWhiteSpace($v)) { $map.version = $v } $map.description = Read-Host -Prompt "description" $v = Read-Host -Prompt "entry point ($($map.main))" if (![string]::IsNullOrWhiteSpace($v)) { $map.main = $v; $map.scripts.start = $v } $map.author = Read-Host -Prompt "author" $v = Read-Host -Prompt "license ($($map.license))" if (![string]::IsNullOrWhiteSpace($v)) { $map.license = $v } $map | ConvertTo-Json -Depth 10 | Out-File -FilePath $file -Encoding utf8 -Force Write-Host 'Success, saved package.json' -ForegroundColor Green } 'test' { Invoke-PodePackageScript -ActionScript $actionScript } 'start' { Invoke-PodePackageScript -ActionScript $actionScript } 'install' { if ($Dev) { Install-PodeLocalModules -Modules $data.devModules } Install-PodeLocalModules -Modules $data.modules Invoke-PodePackageScript -ActionScript $actionScript } 'build' { Invoke-PodePackageScript -ActionScript $actionScript } } } function Invoke-PodePackageScript { param ( [Parameter()] [string] $ActionScript ) if ([string]::IsNullOrWhiteSpace($ActionScript)) { return } if (Test-IsPSCore) { pwsh.exe /c "$($ActionScript)" } else { powershell.exe /c "$($ActionScript)" } } function Install-PodeLocalModules { param ( [Parameter()] $Modules = $null ) if ($null -eq $Modules) { return } $psModules = './ps_modules' # download modules to ps_modules $Modules.psobject.properties.name | ForEach-Object { $_name = $_ $_version = $Modules.$_name try { # if version is latest, retrieve current if ($_version -ieq 'latest') { $_version = [string]((Find-Module $_name -ErrorAction Ignore).Version) } Write-Host "=> Downloading $($_name)@$($_version)... " -NoNewline -ForegroundColor Cyan # if the current version exists, do nothing if (!(Test-Path (Join-Path $psModules "$($_name)/$($_version)"))) { # remove other versions if (Test-Path (Join-Path $psModules "$($_name)")) { Remove-Item -Path (Join-Path $psModules "$($_name)") -Force -Recurse | Out-Null } # download the module Save-Module -Name $_name -RequiredVersion $_version -Path $psModules -Force -ErrorAction Stop | Out-Null } Write-Host 'Success' -ForegroundColor Green } catch { Write-Host 'Failed' -ForegroundColor Red throw "Module or version not found: $($_name)@$($_version)" } } } |