# Environment check - use local nodejs/node and python/x64 only [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 $OutputEncoding = [System.Text.Encoding]::UTF8 chcp 65001 | Out-Null $scriptRoot = $PSScriptRoot if (-not $scriptRoot) { $scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path } if (-not $scriptRoot) { $scriptRoot = (Get-Location).Path } $nodeDir = Join-Path $scriptRoot 'nodejs\node' $nodeExe = Join-Path $nodeDir 'node.exe' $npmCmd = Join-Path $nodeDir 'npm.cmd' if (-not (Test-Path $nodeExe)) { $nodeExe = $null } if (-not (Test-Path $npmCmd)) { $npmCmd = $null } $arch = if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { 'arm64' } else { 'x64' } $pythonRoot = Join-Path $scriptRoot ('python\' + $arch) # Python 优先在 python/x64/py 下查找 $pythonExe = $null foreach ($p in @( (Join-Path $pythonRoot 'py\python.exe'), (Join-Path $pythonRoot 'env\Scripts\python.exe'), (Join-Path $pythonRoot 'python.exe') )) { if (Test-Path $p) { $pythonExe = $p; break } } Write-Host '' Write-Host ('Checking development environment (local nodejs/node and python/' + $arch + ')...') -ForegroundColor Cyan Write-Host '================================' -ForegroundColor Cyan Write-Host '' Write-Host 'Checking Node.js (local nodejs/node)...' -ForegroundColor Yellow if (-not $nodeExe) { Write-Host ('[X] Local Node not found. Put Node in: ' + $nodeDir) -ForegroundColor Red exit 1 } $nodeVersion = & $nodeExe --version 2>$null if ($nodeVersion) { Write-Host ('[OK] Node.js: ' + $nodeVersion) -ForegroundColor Green } else { Write-Host '[X] Local node.exe failed' -ForegroundColor Red exit 1 } Write-Host '' Write-Host 'Checking npm (local)...' -ForegroundColor Yellow if (-not $npmCmd) { Write-Host ('[X] Local npm not found: ' + $nodeDir + '\npm.cmd') -ForegroundColor Red exit 1 } $npmVersion = & $npmCmd --version 2>$null if ($npmVersion) { Write-Host ('[OK] npm: ' + $npmVersion) -ForegroundColor Green } else { Write-Host '[WARN] npm version check skipped' -ForegroundColor Yellow } Write-Host '' Write-Host 'Checking Node.js dependencies (nodejs/node vs dependencies.txt)...' -ForegroundColor Yellow $nodeModulesPath = Join-Path $nodeDir 'node_modules' $depsTxt = Join-Path $scriptRoot ('nodejs\dependences\' + $arch + '\dependencies.txt') $nodeDependenciesScript = Join-Path $scriptRoot ('nodejs\dependences\' + $arch + '\nodejs-dependencies-install.js') if (-not (Test-Path $nodeModulesPath)) { Write-Host '[X] nodejs/node/node_modules not found' -ForegroundColor Red exit 1 } if (-not (Test-Path $depsTxt)) { Write-Host '[X] dependencies.txt not found: ' $depsTxt -ForegroundColor Red exit 1 } $requiredLines = Get-Content $depsTxt -Encoding UTF8 | Where-Object { $_.Trim() -and -not $_.Trim().StartsWith('#') } $missing = @() foreach ($line in $requiredLines) { $pkgSpec = $line.Trim().Split('==', 2)[0].Trim() if (-not $pkgSpec) { continue } if ($pkgSpec -match '^@([^/]+)/(.+)$') { $subPath = Join-Path $nodeModulesPath ('@' + $Matches[1]) $pkgPath = Join-Path $subPath $Matches[2] } else { $pkgPath = Join-Path $nodeModulesPath $pkgSpec } if (-not (Test-Path $pkgPath)) { $missing += $pkgSpec } } if ($missing.Count -gt 0) { Write-Host ('[X] Missing ' + $missing.Count + ' package(s) in nodejs/node/node_modules (per dependencies.txt):') -ForegroundColor Red $missing | Select-Object -First 15 | ForEach-Object { Write-Host ' - ' $_ -ForegroundColor Red } if ($missing.Count -gt 15) { Write-Host (' ... and ' + ($missing.Count - 15) + ' more') -ForegroundColor Red } if (Test-Path $nodeDependenciesScript) { Write-Host 'Running nodejs-dependencies-install.js to install missing...' -ForegroundColor Yellow & $nodeExe $nodeDependenciesScript if ($LASTEXITCODE -ne 0) { Write-Host '[X] Node dependencies installation failed' -ForegroundColor Red exit 1 } } else { exit 1 } } else { Write-Host ('[OK] Node dependencies match dependencies.txt (' + $requiredLines.Count + ' packages)') -ForegroundColor Green } $generateScript = Join-Path $scriptRoot 'nodejs\dependences\generate-nodejs-dependencies.js' if (Test-Path $generateScript) { & $nodeExe $generateScript 2>$null } Write-Host '' Write-Host ('Checking Python (local python/' + $arch + ')...') -ForegroundColor Yellow if (-not $pythonExe) { Write-Host ('[X] Local Python not found. Put Python in: ' + $pythonRoot) -ForegroundColor Red exit 1 } # Python --version 常输出到 stderr,需合并 stdout+stderr 再判断 $pythonVersion = (& $pythonExe --version 2>&1) | Out-String $pythonVersion = $pythonVersion.Trim() if (-not $pythonVersion) { $pythonVersion = $null } if ($pythonVersion -or (($LASTEXITCODE -eq 0) -and (Test-Path $pythonExe))) { if (-not $pythonVersion) { $pythonVersion = '(version ok)' } Write-Host ('[OK] python: ' + $pythonVersion) -ForegroundColor Green } else { Write-Host '[X] Local python failed' -ForegroundColor Red exit 1 } Write-Host '' Write-Host 'Checking pip...' -ForegroundColor Yellow $pipVersion = (& $pythonExe -m pip --version 2>&1) | Out-String $pipVersion = $pipVersion.Trim() if ($pipVersion) { Write-Host ('[OK] pip: ' + $pipVersion) -ForegroundColor Green } else { Write-Host '[X] pip not available' -ForegroundColor Red Write-Host 'Installing pip (ensurepip)...' -ForegroundColor Yellow & $pythonExe -m ensurepip --upgrade 2>$null if ($LASTEXITCODE -eq 0) { Write-Host '[OK] pip ready' -ForegroundColor Green } else { Write-Host '[WARN] ensurepip failed (embedded Python may not include it). Continuing; venv step will create env with pip.' -ForegroundColor Yellow } } Write-Host '' Write-Host 'Checking python virtual environment...' -ForegroundColor Yellow $venvPath = Join-Path $scriptRoot ('python\' + $arch + '\env') $venvPython = Join-Path $venvPath 'Scripts\python.exe' if (Test-Path $venvPython) { Write-Host ('[OK] python venv at: ' + $venvPath) -ForegroundColor Green } else { Write-Host '[X] python virtual environment not found' -ForegroundColor Yellow Write-Host 'Creating python virtual environment...' -ForegroundColor Yellow & $pythonExe -m venv $venvPath if ($LASTEXITCODE -ne 0) { Write-Host '[X] venv creation failed' -ForegroundColor Red Write-Host '[WARN] Continuing without venv...' -ForegroundColor Yellow } else { Write-Host '[OK] venv created' -ForegroundColor Green } } Write-Host '' Write-Host 'Checking python dependencies...' -ForegroundColor Yellow $pythonDependenciesScript = Join-Path $scriptRoot ('python\' + $arch + '\python-enviroment-install.py') if (Test-Path $pythonDependenciesScript) { $env:PYTHON_VENV_PATH = $venvPath # 始终用本地 Python (py/python.exe) 运行,避免 venv 内脚本指向系统 Python (如 C:\programs\python) & $pythonExe $pythonDependenciesScript if ($LASTEXITCODE -ne 0) { Write-Host '[X] Python dependencies check/installation failed' -ForegroundColor Red exit 1 } } else { Write-Host ('[X] Not found: ' + $pythonDependenciesScript) -ForegroundColor Red Write-Host '[WARN] Continuing without Python dependency check...' -ForegroundColor Yellow } Write-Host '' Write-Host '================================' -ForegroundColor Cyan Write-Host 'Environment check completed!' -ForegroundColor Green Write-Host 'All dependencies are ready. You can now start the project.' -ForegroundColor Green