Functions/Install-NodePackageRequiredModules.ps1
<#
.SYNOPSIS This function installs the required Node modules as specified in the Node package details. .DESCRIPTION This function installs the required Node modules as specified in the Node package details. #> function Install-NodePackageRequiredModules { [CmdletBinding(PositionalBinding=$false)] [OutputType([PSCustomObject])] param ( # The name of the Node package. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$name, # The Node package details in JSON string form. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [String]$details, # The path to the folder containing the Node package. [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()] [ValidateScript({ Test-Path -Path $_ })] [String]$nodePackagePath, # Select whether to write information about the installation. [Parameter(Mandatory=$false)] [Switch]$writeInformation, # Select whether to return information about the installation. [Parameter(Mandatory=$false)] [Switch]$returnInformation ) # Retrieve the original location $originalLocation = Get-Location # Install the required modules try { # Change location to the Node package Set-Location -Path $nodePackagePath # Write the package details to a file $details | Out-File -Filepath "package.json" -Encoding "utf8" # Install the packages # Capture success as well as error output Write-Information "Installing required Node modules in '$($nodePackagePath)'." $installOutput = (cmd /c npm install '2>&1') | Out-String $exitCode = $LastExitCode # Output installation information if ($writeInformation) { if ($exitCode -ne 0) { Write-Error "Finished installing Node modules, exited with code $($exitCode), installation output:`r`n$($installOutput)" } else { Write-Information "Finished installing Node modules, installation output:`r`n$($installOutput)" } } # Return the installation information if ($returnInformation) { return [PSCustomObject]@{ ExitCode = $exitCode Output = $installOutput } } } catch { Write-Error "Exception occurred on line $($_.InvocationInfo.ScriptLineNumber): `r`n$($_.Exception.Message)" } finally { # Set the location back to the original location Set-Location -Path $originalLocation } } |