$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
$exe=""
$node_version="v16.20.1"
function ExistNodeJs {
$nodeExecutable = "node.exe"
$path = $env:path
$nodeExists = $path.TrimEnd(";+") -split ";+" | Where-Object { Test-Path (Join-Path $_ $nodeExecutable) }
if ($nodeExists) {
return $true
} else {
return $false
}
}
function CheckVersion () {
param (
[string]$nodePath
)
if ($nodePath -eq "") {
$nodePath = "node$exe"
}
$currentVersion = & $nodePath -v
if ([version]($currentVersion.Trim('v')) -ge [version]$node_version.Trim('v')) {
return $true
} else {
return $false
}
}
function CheckDepency {
if (-Not (Test-Path "$basedir/../ace_tools/lib/src" -PathType Container)) {
return
}
$bd = pwd
cd $basedir/../ace_tools
$missing=($(npm list 2>$null | Select-String 'UNMET DEPENDENCY' | Foreach {($_ -split '\s+',4)[3]}))
cd $bd
if ($missing) {
echo "Missing following node modules"
foreach ($m in $missing){
echo " $m"
}
choice /c yn /m "Do you want to install them:"
if ($LASTEXITCODE -eq 1) {
echo "executing npm install..."
cd $basedir/../ace_tools
npm install
cd $bd
$ret=$LASTEXITCODE
cd $bd
if ($ret -ne 0){
exit $ret
}
} else {
exit 0
}
}
}
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
$exe=".exe"
}
$ret=0
if (Test-Path "$basedir/node.exe") {
$ret = CheckVersion -nodePath $basedir/node$exe
if ($ret -eq $True) {
$Env:path = "$($Env:path):$basedir"
CheckDepency
& "$basedir/node$exe" "$basedir/../ace_tools/lib/ace_tools.js" $args
exit $LASTEXITCODE
} else {
Rename-item $basedir/node.exe -NewName node-old-version.exe
}
}
if (ExistNodeJs) {
$ret = CheckVersion
if ($ret -eq $True) {
CheckDepency
& "node$exe" "$basedir/../ace_tools/lib/ace_tools.js" $args
exit $LASTEXITCODE
} else {
Write-Host "The minimum version required for Node.js is $node_version, the current version is lower than the required version."
}
} else {
Write-Host "No Node.js detected, Node.js is a necessary condition for operation."
}
choice /c yn /m "Do you want to install it:"
if ($LASTEXITCODE -eq 1) {
$message = "start install Node.js(" + $node_version + ")"
Write-Host $message
$msiurl = "https://nodejs.org/dist/"+$node_version+"/node-"+$node_version+"-x64.msi"
$process = Start-Process "msiexec" -ArgumentList "/i $msiurl" -Wait -PassThru
if ($process.ExitCode -eq 0) {
Write-Host "Refreshing environment variables from system for terminal. Please wait..."
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
$ret = CheckVersion
if ($ret -eq $False) {
echo "The required version of nodejs has been installed, but the system still uses the lower version by default. Please uninstall or remove the lower version of nodejs from the path and rerun this program."
exit 1
}
CheckDepency
& "node$exe" "$basedir/../ace_tools/lib/ace_tools.js" $args
$ret=$LASTEXITCODE
} else {
Write-Host "You have cancelled the installation."
$ret=$LASTEXITCODE
}
} else {
$ret=$LASTEXITCODE
}
exit $ret