fix: 创建Windows专用批处理脚本,解决bash脚本无法运行的问题

问题:Windows PowerShell不支持bash脚本,导致 pnpm run dev 报错 "invalid option name"。

解决方案:
1. 创建Windows批处理脚本文件:
   - scripts/dev.bat:开发环境启动(含端口清除逻辑)
   - scripts/build.bat:构建脚本(安装依赖+构建+打包)
   - scripts/start.bat:生产环境启动

2. 更新package.json:
   - 添加Windows专用命令:dev:win、build:win、start:win
   - 原命令添加fallback逻辑(bash失败时尝试bat)

3. Windows脚本特点:
   - 使用 @echo off 和Windows命令语法
   - 自动检查和清除端口占用(netstat + taskkill)
   - 支持环境变量 COZE_WORKSPACE_PATH 和 DEPLOY_RUN_PORT
   - 兼容PowerShell和命令提示符

Windows用户运行方式:
- 推荐:pnpm run dev:win
- 或直接:scripts\dev.bat

Coze-Commit-Type: user
Coze-User-ID: 3722323274763196
Coze-Conversation-ID: 9894087
This commit is contained in:
user9994793890
2026-07-08 14:03:32 +08:00
parent a5eaa68dca
commit c2dabee453
4 changed files with 72 additions and 4 deletions

View File

@@ -3,14 +3,17 @@
"version": "0.1.0",
"private": true,
"scripts": {
"build": "bash ./scripts/build.sh",
"dev": "bash ./scripts/dev.sh",
"build": "bash ./scripts/build.sh || scripts/build.bat",
"dev": "bash ./scripts/dev.sh || scripts/dev.bat",
"preinstall": "npx only-allow pnpm",
"lint": "eslint",
"lint:build": "eslint . --quiet",
"start": "bash ./scripts/start.sh",
"start": "bash ./scripts/start.sh || scripts/start.bat",
"ts-check": "tsc -p tsconfig.json",
"validate": "pnpm run --parallel '/^(ts-check|lint:build)$/'"
"validate": "pnpm run --parallel '/^(ts-check|lint:build)$/'",
"dev:win": "scripts/dev.bat",
"build:win": "scripts/build.bat",
"start:win": "scripts/start.bat"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.958.0",

18
scripts/build.bat Normal file
View File

@@ -0,0 +1,18 @@
@echo off
REM Windows构建脚本
REM 设置工作目录
if not defined COZE_WORKSPACE_PATH set COZE_WORKSPACE_PATH=%cd%
cd /d "%COZE_WORKSPACE_PATH%"
echo Installing dependencies...
pnpm install --prefer-frozen-lockfile --prefer-offline
echo Building the Next.js project...
pnpm next build
echo Bundling server with tsup...
pnpm tsup src/server.ts --format cjs --platform node --target node20 --outDir dist --no-splitting --no-minify
echo Build completed successfully!

32
scripts/dev.bat Normal file
View File

@@ -0,0 +1,32 @@
@echo off
REM Windows开发环境启动脚本
REM 设置端口
set PORT=5000
if not defined DEPLOY_RUN_PORT set DEPLOY_RUN_PORT=%PORT%
REM 检查并清除端口占用
echo Checking port %DEPLOY_RUN_PORT%...
netstat -ano | findstr :%DEPLOY_RUN_PORT% | findstr LISTENING >nul
if %errorlevel% equ 0 (
echo Port %DEPLOY_RUN_PORT% is in use, attempting to clear...
for /f "tokens=5" %%a in ('netstat -ano ^| findstr :%DEPLOY_RUN_PORT% ^| findstr LISTENING') do (
echo Killing process %%a on port %DEPLOY_RUN_PORT%
taskkill /PID %%a /F >nul 2>&1
)
timeout /t 2 /nobreak >nul
echo Port %DEPLOY_RUN_PORT% cleared.
) else (
echo Port %DEPLOY_RUN_PORT% is free.
)
REM 设置工作目录
if not defined COZE_WORKSPACE_PATH set COZE_WORKSPACE_PATH=%cd%
echo Starting HTTP service on port %DEPLOY_RUN_PORT% for dev...
echo Working directory: %COZE_WORKSPACE_PATH%
REM 启动开发服务器
cd /d "%COZE_WORKSPACE_PATH%"
set PORT=%DEPLOY_RUN_PORT%
pnpm tsx watch src/server.ts

15
scripts/start.bat Normal file
View File

@@ -0,0 +1,15 @@
@echo off
REM Windows生产环境启动脚本
REM 设置端口
set PORT=5000
if not defined DEPLOY_RUN_PORT set DEPLOY_RUN_PORT=%PORT%
REM 设置工作目录
if not defined COZE_WORKSPACE_PATH set COZE_WORKSPACE_PATH=%cd%
cd /d "%COZE_WORKSPACE_PATH%"
echo Starting HTTP service on port %DEPLOY_RUN_PORT% for deploy...
set PORT=%DEPLOY_RUN_PORT%
node dist/server.js