|
|
@@ -76,16 +76,40 @@ Write-Host 'Checking npm (local)...' -ForegroundColor Yellow
|
|
|
# 使用已验证的 nodeDir 构建 npm 路径,确保检查正确
|
|
|
$npmCliPath = Join-Path $nodeDir 'node_modules\npm\bin\npm-cli.js'
|
|
|
$npmCmd = Join-Path $nodeDir 'npm.cmd'
|
|
|
-if (Test-Path $npmCliPath) {
|
|
|
- # 已安装,跳过
|
|
|
+if (-not (Test-Path $npmCmd)) { $npmCmd = $null }
|
|
|
+$nodeModulesPath = Join-Path $nodeDir 'node_modules'
|
|
|
+$npmBackupDir = Join-Path $scriptRoot ('nodejs\dependences\' + $arch + '\backup')
|
|
|
+$npmBackupNpmDir = Join-Path $npmBackupDir 'npm'
|
|
|
+New-Item -ItemType Directory -Force -Path $npmBackupDir | Out-Null
|
|
|
+
|
|
|
+$npmHealthy = $false
|
|
|
+if ((Test-Path $npmCliPath) -and $npmCmd) {
|
|
|
$npmVersion = & $npmCmd --version 2>$null
|
|
|
if ($npmVersion) {
|
|
|
+ $npmHealthy = $true
|
|
|
Write-Host ('[OK] npm: ' + $npmVersion + ' (already installed)') -ForegroundColor Green
|
|
|
- } else {
|
|
|
- Write-Host '[OK] npm already installed' -ForegroundColor Green
|
|
|
}
|
|
|
-} else {
|
|
|
- Write-Host '[X] npm not found in node_modules' -ForegroundColor Red
|
|
|
+}
|
|
|
+
|
|
|
+if (-not $npmHealthy) {
|
|
|
+ Write-Host '[WARN] npm missing or broken, trying backup restore first...' -ForegroundColor Yellow
|
|
|
+ if (Test-Path $npmBackupNpmDir) {
|
|
|
+ New-Item -ItemType Directory -Force -Path $nodeModulesPath | Out-Null
|
|
|
+ $nodeNpmDir = Join-Path $nodeModulesPath 'npm'
|
|
|
+ if (Test-Path $nodeNpmDir) { Remove-Item -LiteralPath $nodeNpmDir -Recurse -Force }
|
|
|
+ Copy-Item -LiteralPath $npmBackupNpmDir -Destination $nodeModulesPath -Recurse -Force
|
|
|
+ if ((Test-Path $npmCliPath) -and $npmCmd) {
|
|
|
+ $npmVersion = & $npmCmd --version 2>$null
|
|
|
+ if ($npmVersion) {
|
|
|
+ $npmHealthy = $true
|
|
|
+ Write-Host ('[OK] npm restored from backup: ' + $npmVersion) -ForegroundColor Green
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+if (-not $npmHealthy) {
|
|
|
+ Write-Host '[WARN] Backup restore failed, installing/downloading npm...' -ForegroundColor Yellow
|
|
|
$installNpmBat = Join-Path $nodeDir 'install-npm.bat'
|
|
|
if (Test-Path $installNpmBat) {
|
|
|
Write-Host 'Running install-npm.bat to install npm...' -ForegroundColor Yellow
|
|
|
@@ -98,7 +122,13 @@ if (Test-Path $npmCliPath) {
|
|
|
Write-Host '[X] npm-cli.js still missing after install. Check network or run install-npm.bat manually.' -ForegroundColor Red
|
|
|
exit 1
|
|
|
}
|
|
|
- Write-Host '[OK] npm installed' -ForegroundColor Green
|
|
|
+ $npmVersion = & $npmCmd --version 2>$null
|
|
|
+ if (-not $npmVersion) {
|
|
|
+ Write-Host '[X] npm installed but still unusable' -ForegroundColor Red
|
|
|
+ exit 1
|
|
|
+ }
|
|
|
+ $npmHealthy = $true
|
|
|
+ Write-Host ('[OK] npm installed: ' + $npmVersion) -ForegroundColor Green
|
|
|
$generateScript = Join-Path $scriptRoot 'nodejs\dependences\generate-nodejs-dependencies.js'
|
|
|
if (Test-Path $generateScript) {
|
|
|
Write-Host 'Running generate-nodejs-dependencies.js...' -ForegroundColor Yellow
|
|
|
@@ -113,6 +143,12 @@ if (Test-Path $npmCliPath) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+if ($npmHealthy -and (Test-Path (Join-Path $nodeModulesPath 'npm'))) {
|
|
|
+ if (Test-Path $npmBackupNpmDir) { Remove-Item -LiteralPath $npmBackupNpmDir -Recurse -Force }
|
|
|
+ Copy-Item -LiteralPath (Join-Path $nodeModulesPath 'npm') -Destination $npmBackupDir -Recurse -Force
|
|
|
+ Write-Host ('[OK] npm backup synced: ' + $npmBackupNpmDir) -ForegroundColor Green
|
|
|
+}
|
|
|
+
|
|
|
# Use domestic npm registry for all subsequent npm operations
|
|
|
if ($npmCmd) {
|
|
|
Push-Location $scriptRoot
|