enviroment-check.ps1 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Environment check - use local nodejs/node and python/x64 only
  2. [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
  3. $OutputEncoding = [System.Text.Encoding]::UTF8
  4. chcp 65001 | Out-Null
  5. $scriptRoot = $PSScriptRoot
  6. if (-not $scriptRoot) { $scriptRoot = Split-Path -Parent $MyInvocation.MyCommand.Path }
  7. if (-not $scriptRoot) { $scriptRoot = (Get-Location).Path }
  8. # 架构:根据 PROCESSOR_ARCHITECTURE
  9. $arch = if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { 'arm64' } else { 'x64' }
  10. # 从 config.js 读取路径(需先找到 node 来执行 get-node-paths.js)
  11. $nodeExeBootstrap = $null
  12. foreach ($tryNode in @(
  13. (Join-Path $scriptRoot ('nodejs\' + $arch + '\node\node.exe')),
  14. (Join-Path $scriptRoot 'nodejs\node\node.exe')
  15. )) {
  16. if (Test-Path $tryNode) { $nodeExeBootstrap = $tryNode; break }
  17. }
  18. $getPathsScript = Join-Path $scriptRoot 'configs\get-node-paths.js'
  19. if ($nodeExeBootstrap -and (Test-Path $getPathsScript)) {
  20. try {
  21. Push-Location $scriptRoot
  22. $cfgJson = & $nodeExeBootstrap $getPathsScript 2>$null
  23. if ($cfgJson) {
  24. $cfg = $cfgJson | ConvertFrom-Json
  25. $nodeDir = $cfg.nodeDir
  26. $npmCmd = $cfg.npmCmdPath
  27. $npmCliPath = $cfg.npmCliPath
  28. if ($cfg.pythonDir) { $pythonRoot = $cfg.pythonDir }
  29. if ($cfg.pythonVenvPath) { $venvPath = $cfg.pythonVenvPath }
  30. }
  31. } catch { }
  32. finally { Pop-Location }
  33. }
  34. if (-not $nodeDir) { $nodeDir = Join-Path $scriptRoot ('nodejs\' + $arch + '\node'); if (-not (Test-Path $nodeDir)) { $nodeDir = Join-Path $scriptRoot 'nodejs\node' } }
  35. if (-not $npmCliPath) { $npmCliPath = Join-Path $nodeDir 'node_modules\npm\bin\npm-cli.js' }
  36. if (-not $npmCmd) { $npmCmd = Join-Path $nodeDir 'npm.cmd' }
  37. $nodeExe = Join-Path $nodeDir 'node.exe'
  38. if (-not (Test-Path $nodeExe)) { $nodeExe = $null }
  39. if (-not (Test-Path $npmCmd)) { $npmCmd = $null }
  40. if (-not $pythonRoot) { $pythonRoot = Join-Path $scriptRoot ('python\' + $arch) }
  41. # Python 优先在 python/x64/py 下查找
  42. $pythonExe = $null
  43. foreach ($p in @(
  44. (Join-Path $pythonRoot 'py\python.exe'),
  45. (Join-Path $pythonRoot 'env\Scripts\python.exe'),
  46. (Join-Path $pythonRoot 'python.exe')
  47. )) {
  48. if (Test-Path $p) { $pythonExe = $p; break }
  49. }
  50. Write-Host ''
  51. Write-Host ('Checking development environment (local nodejs/node and python/' + $arch + ')...') -ForegroundColor Cyan
  52. Write-Host '================================' -ForegroundColor Cyan
  53. Write-Host ''
  54. Write-Host 'Checking Node.js (local nodejs/node)...' -ForegroundColor Yellow
  55. if (-not $nodeExe) {
  56. Write-Host ('[X] Local Node not found. Put Node in: ' + $nodeDir) -ForegroundColor Red
  57. exit 1
  58. }
  59. $nodeVersion = & $nodeExe --version 2>$null
  60. if ($nodeVersion) {
  61. Write-Host ('[OK] Node.js: ' + $nodeVersion) -ForegroundColor Green
  62. } else {
  63. Write-Host '[X] Local node.exe failed' -ForegroundColor Red
  64. exit 1
  65. }
  66. Write-Host ''
  67. Write-Host 'Checking npm (local)...' -ForegroundColor Yellow
  68. # 使用已验证的 nodeDir 构建 npm 路径,确保检查正确
  69. $npmCliPath = Join-Path $nodeDir 'node_modules\npm\bin\npm-cli.js'
  70. $npmCmd = Join-Path $nodeDir 'npm.cmd'
  71. if (Test-Path $npmCliPath) {
  72. # 已安装,跳过
  73. $npmVersion = & $npmCmd --version 2>$null
  74. if ($npmVersion) {
  75. Write-Host ('[OK] npm: ' + $npmVersion + ' (already installed)') -ForegroundColor Green
  76. } else {
  77. Write-Host '[OK] npm already installed' -ForegroundColor Green
  78. }
  79. } else {
  80. Write-Host '[X] npm not found in node_modules' -ForegroundColor Red
  81. $installNpmBat = Join-Path $nodeDir 'install-npm.bat'
  82. if (Test-Path $installNpmBat) {
  83. Write-Host 'Running install-npm.bat to install npm...' -ForegroundColor Yellow
  84. cmd /c "`"$installNpmBat`" /q"
  85. if ($LASTEXITCODE -ne 0) {
  86. Write-Host '[X] npm installation failed' -ForegroundColor Red
  87. exit 1
  88. }
  89. if (-not (Test-Path $npmCliPath)) {
  90. Write-Host '[X] npm-cli.js still missing after install. Check network or run install-npm.bat manually.' -ForegroundColor Red
  91. exit 1
  92. }
  93. Write-Host '[OK] npm installed' -ForegroundColor Green
  94. $generateScript = Join-Path $scriptRoot 'nodejs\dependences\generate-nodejs-dependencies.js'
  95. if (Test-Path $generateScript) {
  96. Write-Host 'Running generate-nodejs-dependencies.js...' -ForegroundColor Yellow
  97. & $nodeExe $generateScript
  98. if ($LASTEXITCODE -ne 0) {
  99. Write-Host '[WARN] generate-nodejs-dependencies.js failed (will retry later)' -ForegroundColor Yellow
  100. }
  101. }
  102. } else {
  103. Write-Host ('[X] Run ' + $installNpmBat + ' to install npm') -ForegroundColor Red
  104. exit 1
  105. }
  106. }
  107. # Use domestic npm registry for all subsequent npm operations
  108. if ($npmCmd) {
  109. Push-Location $scriptRoot
  110. & $npmCmd config set registry https://registry.npmmirror.com 2>$null
  111. Pop-Location
  112. }
  113. Write-Host ''
  114. Write-Host 'Checking Node.js dependencies (nodejs/node vs dependencies.txt)...' -ForegroundColor Yellow
  115. $nodeModulesPath = Join-Path $nodeDir 'node_modules'
  116. $depsTxt = Join-Path $scriptRoot ('nodejs\dependences\' + $arch + '\dependencies.txt')
  117. $nodeDependenciesScript = Join-Path $scriptRoot ('nodejs\dependences\' + $arch + '\nodejs-dependencies-install.js')
  118. # Ensure package.json deps (vite, react, etc.) are installed; updates dependencies.txt
  119. if (Test-Path $nodeDependenciesScript) {
  120. & $nodeExe $nodeDependenciesScript
  121. if ($LASTEXITCODE -ne 0) {
  122. Write-Host '[X] Node.js dependencies install failed' -ForegroundColor Red
  123. exit 1
  124. }
  125. }
  126. # npm install prunes extraneous packages; npm (not in package.json) gets removed. Reinstall if missing.
  127. if (-not (Test-Path $npmCliPath)) {
  128. $installNpmBat = Join-Path $nodeDir 'install-npm.bat'
  129. if (Test-Path $installNpmBat) {
  130. Write-Host 'npm was pruned by npm install. Reinstalling npm...' -ForegroundColor Yellow
  131. cmd /c "`"$installNpmBat`" /q"
  132. if ($LASTEXITCODE -ne 0 -or -not (Test-Path $npmCliPath)) {
  133. Write-Host '[X] npm reinstall failed' -ForegroundColor Red
  134. exit 1
  135. }
  136. Write-Host '[OK] npm restored' -ForegroundColor Green
  137. }
  138. }
  139. if (-not (Test-Path $nodeModulesPath)) {
  140. Write-Host '[X] nodejs/node/node_modules not found' -ForegroundColor Red
  141. exit 1
  142. }
  143. if (-not (Test-Path $depsTxt)) {
  144. $generateScript = Join-Path $scriptRoot 'nodejs\dependences\generate-nodejs-dependencies.js'
  145. if (Test-Path $generateScript) {
  146. Write-Host 'dependencies.txt not found. Running generate-nodejs-dependencies.js...' -ForegroundColor Yellow
  147. & $nodeExe $generateScript
  148. if ($LASTEXITCODE -ne 0 -and -not (Test-Path $depsTxt)) {
  149. Write-Host 'Generate failed (node_modules may be empty). Running nodejs-dependencies-install...' -ForegroundColor Yellow
  150. if (Test-Path $nodeDependenciesScript) {
  151. & $nodeExe $nodeDependenciesScript
  152. if ($LASTEXITCODE -ne 0) {
  153. Write-Host '[X] Failed to install dependencies and create dependencies.txt' -ForegroundColor Red
  154. exit 1
  155. }
  156. }
  157. if (-not (Test-Path $depsTxt)) {
  158. Write-Host '[X] dependencies.txt still not found: ' $depsTxt -ForegroundColor Red
  159. exit 1
  160. }
  161. }
  162. } else {
  163. Write-Host '[X] dependencies.txt not found: ' $depsTxt -ForegroundColor Red
  164. exit 1
  165. }
  166. }
  167. $requiredLines = Get-Content $depsTxt -Encoding UTF8 | Where-Object { $_.Trim() -and -not $_.Trim().StartsWith('#') }
  168. $missing = @()
  169. foreach ($line in $requiredLines) {
  170. $pkgSpec = $line.Trim().Split('==', 2)[0].Trim()
  171. if (-not $pkgSpec) { continue }
  172. if ($pkgSpec -match '^@([^/]+)/(.+)$') {
  173. $subPath = Join-Path $nodeModulesPath ('@' + $Matches[1])
  174. $pkgPath = Join-Path $subPath $Matches[2]
  175. } else {
  176. $pkgPath = Join-Path $nodeModulesPath $pkgSpec
  177. }
  178. if (-not (Test-Path $pkgPath)) { $missing += $pkgSpec }
  179. }
  180. if ($missing.Count -gt 0) {
  181. Write-Host ('[X] Missing ' + $missing.Count + ' package(s) in nodejs/node/node_modules (per dependencies.txt):') -ForegroundColor Red
  182. $missing | Select-Object -First 15 | ForEach-Object { Write-Host ' - ' $_ -ForegroundColor Red }
  183. if ($missing.Count -gt 15) { Write-Host (' ... and ' + ($missing.Count - 15) + ' more') -ForegroundColor Red }
  184. if (Test-Path $nodeDependenciesScript) {
  185. Write-Host 'Running nodejs-dependencies-install.js to install missing...' -ForegroundColor Yellow
  186. & $nodeExe $nodeDependenciesScript
  187. if ($LASTEXITCODE -ne 0) {
  188. Write-Host '[X] Node dependencies installation failed' -ForegroundColor Red
  189. exit 1
  190. }
  191. } else {
  192. exit 1
  193. }
  194. } else {
  195. Write-Host ('[OK] Node dependencies match dependencies.txt (' + $requiredLines.Count + ' packages)') -ForegroundColor Green
  196. }
  197. $generateScript = Join-Path $scriptRoot 'nodejs\dependences\generate-nodejs-dependencies.js'
  198. if (Test-Path $generateScript) {
  199. & $nodeExe $generateScript 2>$null
  200. }
  201. Write-Host ''
  202. Write-Host ('Checking Python (local python/' + $arch + ')...') -ForegroundColor Yellow
  203. if (-not $pythonExe) {
  204. Write-Host ('[X] Local Python not found. Put Python in: ' + $pythonRoot) -ForegroundColor Red
  205. exit 1
  206. }
  207. Write-Host '[OK] python found' -ForegroundColor Green
  208. Write-Host ''
  209. Write-Host 'Running python-enviroment-install.py (creates venv if missing, installs dependencies)...' -ForegroundColor Yellow
  210. if (-not $venvPath) { $venvPath = Join-Path $scriptRoot ('python\' + $arch + '\env') }
  211. $pythonDependenciesScript = Join-Path $scriptRoot ('python\' + $arch + '\python-enviroment-install.py')
  212. if (Test-Path $pythonDependenciesScript) {
  213. $env:PYTHON_VENV_PATH = $venvPath
  214. # 始终用本地 Python (py/python.exe) 运行,避免 venv 内脚本指向系统 Python (如 C:\programs\python)
  215. & $pythonExe $pythonDependenciesScript
  216. if ($LASTEXITCODE -ne 0) {
  217. Write-Host '[X] Python dependencies check/installation failed' -ForegroundColor Red
  218. exit 1
  219. }
  220. } else {
  221. Write-Host ('[X] Not found: ' + $pythonDependenciesScript) -ForegroundColor Red
  222. Write-Host '[WARN] Continuing without Python dependency check...' -ForegroundColor Yellow
  223. }
  224. Write-Host ''
  225. # Final verification: npm must exist for run_react to start Vite
  226. if (-not (Test-Path $npmCliPath)) {
  227. Write-Host '[X] npm-cli.js not found. Run: nodejs\node\install-npm.bat' -ForegroundColor Red
  228. exit 1
  229. }
  230. Write-Host '================================' -ForegroundColor Cyan
  231. Write-Host 'Environment check completed!' -ForegroundColor Green
  232. Write-Host 'All dependencies are ready. You can now start the project.' -ForegroundColor Green