Files
LinkPort/scripts/node-toolchain.ps1

34 lines
2.0 KiB
PowerShell

function Initialize-LinkPortNodeRuntime {
param([Parameter(Mandatory = $true)][string]$ProjectRoot)
$ProjectRoot = [IO.Path]::GetFullPath($ProjectRoot)
$nodeExecutable = $null
$selected = $false
if (![string]::IsNullOrWhiteSpace($env:LINKPORT_NODE)) {
$nodeExecutable = [Environment]::ExpandEnvironmentVariables($env:LINKPORT_NODE.Trim().Trim('"'))
if (![IO.Path]::IsPathRooted($nodeExecutable)) { $nodeExecutable = Join-Path $ProjectRoot $nodeExecutable }
$selected = $true
} elseif (Test-Path -LiteralPath (Join-Path $ProjectRoot 'tools/node/node.exe') -PathType Leaf) {
$nodeExecutable = Join-Path $ProjectRoot 'tools/node/node.exe'
$selected = $true
} else {
$command = Get-Command node.exe -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1
if ($command) { $nodeExecutable = $command.Source }
}
if (!$nodeExecutable -or !(Test-Path -LiteralPath $nodeExecutable -PathType Leaf)) {
throw 'Node.js was not found. Put the complete Node.js distribution in tools/node, use LINKPORT_NODE with a project-relative node.exe path, or install Node.js and npm on PATH.'
}
$nodeExecutable = [IO.Path]::GetFullPath($nodeExecutable)
$nodeDirectory = Split-Path $nodeExecutable -Parent
$npmExecutable = Join-Path $nodeDirectory 'npm.cmd'
if (!(Test-Path -LiteralPath $npmExecutable -PathType Leaf)) {
$npmCommand = if (!$selected) { Get-Command npm.cmd -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1 }
if (!$npmCommand -or !(Test-Path -LiteralPath $npmCommand.Source -PathType Leaf)) {
throw 'npm.cmd was not found beside Node.js. Copy the complete Node.js distribution, including npm.cmd and node_modules/npm, not only node.exe.'
}
$npmExecutable = $npmCommand.Source
}
$env:PATH = $nodeDirectory + [IO.Path]::PathSeparator + (Split-Path $npmExecutable -Parent) + [IO.Path]::PathSeparator + $env:PATH
return $nodeExecutable
}