如何在Windows上实现VS Code开发环境一键自动化启动?
一键自动化搭建前后端开发环境方案(Windows)
一、PowerShell主脚本(start-dev-env.ps1)
这个脚本负责拉取代码、启动VS Code实例并触发项目内的自动化任务:
# 配置项目路径 $backendPath = "C:/myCompany/backend" $frontendPath = "C:/myCompany/frontend" # 替换成你的前端项目实际路径 # -------------------------- 处理后端项目 -------------------------- # 切换到后端目录并拉取最新代码 Set-Location $backendPath git pull # 启动VS Code并打开后端项目,同时触发预设的启动任务 code $backendPath --task "Start Backend Services" # 等待片刻,避免前端启动过快(可根据实际情况调整时长) Start-Sleep -Seconds 3 # -------------------------- 处理前端项目 -------------------------- # 切换到前端目录并拉取最新代码 Set-Location $frontendPath git pull # 启动VS Code并打开前端项目,同时触发预设的启动任务 code $frontendPath --task "Start Frontend Dev Server"
二、VS Code项目任务配置
需要在前后端项目的.vscode/tasks.json文件中定义对应的自动化任务,让脚本启动VS Code后自动执行命令。
后端项目(backend/.vscode/tasks.json)
这个任务会先启动docker-compose up,再拆分终端启动npm run start:dev:
{ "version": "2.0.0", "tasks": [ { "label": "Start Backend Services", "dependsOrder": "sequence", "dependsOn": ["Start Docker Compose", "Start Dev Server"], "runOptions": { "runOn": "folderOpen" } }, { "label": "Start Docker Compose", "type": "shell", "command": "docker-compose up", "args": [], "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "dedicated" }, "problemMatcher": [] }, { "label": "Start Dev Server", "type": "shell", "command": "npm run start:dev", "args": [], "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "dedicated", "split": "right" // 拆分终端到右侧 }, "problemMatcher": [] } ] }
前端项目(frontend/.vscode/tasks.json)
这个任务负责启动前端开发服务器:
{ "version": "2.0.0", "tasks": [ { "label": "Start Frontend Dev Server", "type": "shell", "command": "npm run start:dev", "args": [], "presentation": { "echo": true, "reveal": "always", "focus": true, "panel": "dedicated" }, "problemMatcher": [], "runOptions": { "runOn": "folderOpen" } } ] }
三、使用说明
- 将上述PowerShell脚本保存为
start-dev-env.ps1,放在任意你方便的位置(比如桌面)。 - 分别在前后端项目的
.vscode文件夹下创建对应的tasks.json文件,粘贴上面的配置内容。 - 右键点击
start-dev-env.ps1,选择“使用PowerShell运行”,即可一键完成所有操作:- 拉取前后端最新代码
- 启动两个VS Code实例
- 后端自动启动Docker服务和开发服务器(拆分终端)
- 前端自动启动开发服务器
注意事项
- 确保你的系统已经安装了
git、docker-compose、Node.js,并且这些命令能在PowerShell中直接执行。 - 如果
code命令无法识别,需要将VS Code的安装路径添加到系统环境变量的PATH中(默认路径一般是C:\Users\<你的用户名>\AppData\Local\Programs\Microsoft VS Code\bin)。 - 可以根据实际需求调整脚本中的等待时长(
Start-Sleep的秒数),或者修改任务中的终端拆分方向、是否聚焦等配置。
内容的提问来源于stack exchange,提问作者Dopynose
相关产品推荐
相关产品推荐

