Files
nodeagent/scripts/run-portable.cmd
T
jkooe 5b69d6cec9 refactor: 项目结构与命名规范化(重写前的最后一次常规提交)
## 改名
`scripts/run-temp.cmd` → `scripts/run-portable.cmd`
理由:它是**正式功能**(免安装、免管理员的便携启动方案),"temp" 易被误认成临时文件。
程序内文案同步:TEMPORARY launcher → PORTABLE launcher;
打包产物 README 的"【临时试用,不装也行】" → "【免安装试用,不装也行】"。

## 移动
`PRD.md` → `docs/PRD.md`
理由:文档统一放 docs/(README/CHANGELOG 按惯例留在根)。

## 同步的引用(共 11 处)
- scripts/pack.mjs:产物文件名校验 + 生成 README 的 3 处指引文案
- scripts/run-portable.cmd:自身用法注释 6 处
- README.md:目录树 + 文档链接
- docs/DEVELOPMENT.md:配套文档链接(`../PRD.md` → `PRD.md`,现已同目录)+ 目录树

## 验证
- 全仓 `run-temp` 残留 **0**;`./PRD.md`/`../PRD.md` 旧路径残留 **0**
- 7 包构建通过;单测 273 全绿;ps1-encoding 24 全绿
- `pack.mjs` 实跑通过,`run-portable.cmd` 已正确进入 Windows 产物
- `.cmd` 保持 ASCII + CRLF(94 行);PRD.md 无内部相对链接需调整
2026-10-10 13:39:42 +08:00

95 lines
4.3 KiB
Batchfile

@echo off
REM ============================================================================
REM nodeagent - PORTABLE launcher (no install, no admin, no scheduled task)
REM
REM Purpose: bring the agent up for a quick test WITHOUT running install.cmd.
REM The agent is zero-config: on first run it creates %USERPROFILE%\.nodeagent\
REM (agent.json with a random PSK + self-signed cert) and starts listening.
REM
REM Usage:
REM run-portable.cmd start in foreground (Ctrl+C to stop)
REM run-portable.cmd start same as above
REM run-portable.cmd background start hidden, then print status
REM run-portable.cmd stop stop the background instance
REM run-portable.cmd status show whether it is running
REM
REM IMPORTANT: the agent takes NO command-line arguments (it does not parse
REM argv at all). The listen port comes from %USERPROFILE%\.nodeagent\
REM agent.json -- the "<port>" below is used ONLY by this script's own status
REM check. To change the port, edit agent.json, not the command line.
REM
REM Notes:
REM * Pure ASCII on purpose (see install.cmd) so it survives the CMD code page.
REM * No admin needed. Nothing is written outside this folder and %USERPROFILE%.
REM * "background" still dies with the parent CMD window; use it for a test
REM session, not as a permanent daemon. For that, run install.cmd once.
REM ============================================================================
setlocal EnableExtensions
set "HERE=%~dp0"
set "NODE=%HERE%node.exe"
set "AGENT=%HERE%agent.mjs"
set "PORT=%~2"
if not defined PORT set "PORT=8765"
if not exist "%NODE%" (
echo [x] node.exe not found in "%HERE%"
exit /b 1
)
if not exist "%AGENT%" (
echo [x] agent.mjs not found in "%HERE%"
exit /b 1
)
if /i "%~1"=="stop" goto :STOP
if /i "%~1"=="status" goto :STATUS
if /i "%~1"=="background" goto :BACKGROUND
goto :FOREGROUND
REM ---------------------------------------------------------------------------
:FOREGROUND
echo ==========================================================
echo nodeagent (temporary, foreground)
echo ==========================================================
echo folder : "%HERE%"
echo port : %PORT% (for this status check only; agent reads agent.json)
echo stop : Ctrl+C
echo.
"%NODE%" "%AGENT%"
echo.
echo [i] agent exited (code %errorlevel%)
pause
exit /b 0
REM ---------------------------------------------------------------------------
:BACKGROUND
echo [*] starting agent in background...
start "nodeagent-temp" /min cmd /c ""%NODE%" "%AGENT%""
rem give it a moment to bind the port and generate cert/config
timeout /t 3 /nobreak >nul
goto :STATUS
REM ---------------------------------------------------------------------------
:STATUS
echo [*] checking status...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference='SilentlyContinue';" ^
"$p = Get-CimInstance Win32_Process -Filter \"Name='node.exe'\" -ErrorAction SilentlyContinue | Where-Object { $_.CommandLine -like '*agent.mjs*' };" ^
"if ($p) { Write-Host (' running : PID ' + (($p | ForEach-Object { $_.ProcessId }) -join ', ')) } else { Write-Host ' running : no' };" ^
"$l = Get-NetTCPConnection -LocalPort %PORT% -State Listen -ErrorAction SilentlyContinue;" ^
"if ($l) { Write-Host (' listening: port %PORT%') } else { Write-Host ' listening: no' };" ^
"$cfg = Join-Path $env:USERPROFILE '.nodeagent\agent.json';" ^
"if (Test-Path $cfg) { Write-Host (' config : ' + $cfg); try { (Get-Content $cfg -Raw | ConvertFrom-Json) | ForEach-Object { Write-Host (' node_id : ' + $_.node_id); Write-Host (' key : ' + $_.key) } } catch {} } else { Write-Host ' config : (not yet created)' };"
echo.
echo If the connect line above is empty, run: run-portable.cmd status
pause
exit /b 0
REM ---------------------------------------------------------------------------
:STOP
echo [*] stopping temporary agent...
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference='SilentlyContinue';" ^
"Get-CimInstance Win32_Process -Filter \"Name='node.exe'\" | Where-Object { $_.CommandLine -like '*agent.mjs*' } | ForEach-Object { Stop-Process -Id $_.ProcessId -Force; Write-Host (' killed PID ' + $_.ProcessId) };"
echo [i] done
exit /b 0