Repository navigation
Expand file tree
/
Copy pathsetup.ps1
More file actions
88 lines (83 loc) · 3.94 KB
/
Copy pathsetup.ps1
File metadata and controls
88 lines (83 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
param([switch]$test, [switch]$portable)
$ErrorActionPreference = 'Continue'
$ProgressPreference = 'SilentlyContinue'
Set-Location $PSScriptRoot
[Environment]::CurrentDirectory = $PSScriptRoot
function Say($msg) { Write-Host $msg }
# 1. 找 node:优先系统安装,否则用/下载便携版
$nodeExe = $null
if (-not $portable) {
$cmd = Get-Command node -ErrorAction SilentlyContinue
if ($cmd) { $nodeExe = $cmd.Source }
}
if (-not $nodeExe) {
$nodeDir = Join-Path $PSScriptRoot '.node'
$nodeExe = Join-Path $nodeDir 'node.exe'
if (-not (Test-Path $nodeExe)) {
Say '================================================'
Say ' 第一次运行:正在下载运行环境(约 30MB)'
Say ' 只需要一次,请保持网络畅通,耐心等几分钟...'
Say '================================================'
$v = 'v22.20.0'; $n = "node-$v-win-x64"
$zip = Join-Path $env:TEMP 'lapian-node.zip'
$ok = $false
foreach ($base in @('https://registry.npmmirror.com/-/binary/node', 'https://nodejs.org/dist')) {
try { Invoke-WebRequest "$base/$v/$n.zip" -OutFile $zip -UseBasicParsing -ErrorAction Stop; $ok = $true; break } catch {}
}
if (-not $ok) {
Say ''
Say '运行环境下载失败。请检查网络后重新双击 run.bat。'
Say '也可以手动安装 Node.js 后再试:https://nodejs.org/zh-cn'
exit 1
}
$tmp = Join-Path $PSScriptRoot '.node-tmp'
Expand-Archive $zip -DestinationPath $tmp -Force
Move-Item (Join-Path $tmp $n) $nodeDir
Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $zip -Force -ErrorAction SilentlyContinue
}
}
$nodeHome = Split-Path $nodeExe
# 2. 安装依赖(用已验证可靠的 cmd 形态,并把 node 目录前置进 PATH)
if (-not (Test-Path (Join-Path $PSScriptRoot 'node_modules'))) {
Say '================================================'
Say ' 第一次运行:正在安装程序组件'
Say ' 只需要一次,大约一两分钟...'
Say '================================================'
$env:PATH = "$nodeHome;$env:PATH"
# npm 的进度日志走 stderr,在 cmd 层直接收进文件,避免上层管道把它当错误
cmd /c "npm install --no-audit --no-fund --registry=https://registry.npmmirror.com > install.log 2>&1"
if ($LASTEXITCODE -ne 0 -or -not (Test-Path (Join-Path $PSScriptRoot 'node_modules'))) {
Get-Content install.log -Tail 8 -ErrorAction SilentlyContinue | ForEach-Object { Say $_ }
Say ''
Say '组件安装失败。请检查网络后重新双击 run.bat。'
exit 1
}
}
# 3. 启动服务(直接 node 跑 vite,不经任何 shim)
Say '正在启动拉片笔记...'
$vite = Join-Path $PSScriptRoot 'node_modules/vite/bin/vite.js'
$serverLog = Join-Path $PSScriptRoot 'server.log'
$serverErrLog = Join-Path $PSScriptRoot 'server-err.log'
$proc = Start-Process $nodeExe -ArgumentList "`"$vite`" --port 5173" -WorkingDirectory $PSScriptRoot -WindowStyle Minimized -PassThru -RedirectStandardOutput $serverLog -RedirectStandardError $serverErrLog
# 4. 等服务就绪后开浏览器
$ready = $false
for ($i = 0; $i -lt 90; $i++) {
try { Invoke-WebRequest 'http://localhost:5173/' -UseBasicParsing -TimeoutSec 2 -ErrorAction Stop | Out-Null; $ready = $true; break } catch { Start-Sleep 1 }
if ($proc.HasExited) { break }
}
if (-not $ready) {
Say '启动失败或超时。最近的服务日志:'
Get-Content $serverErrLog -Tail 6 -ErrorAction SilentlyContinue | ForEach-Object { Say $_ }
Say '请重新双击 run.bat;若反复失败,把这个窗口截图反馈。'
exit 1
}
if (-not $test) { Start-Process 'http://localhost:5173/' }
Say ''
Say '================================================'
Say ' 拉片笔记已在浏览器打开:http://localhost:5173'
Say ' 有一个最小化的"node"服务窗口,'
Say ' 使用期间请不要关闭它;用完后关掉它即可退出。'
Say '================================================'
if (-not $test) { Read-Host '按回车键关闭本窗口(服务会继续运行)' }
exit 0