多项目终端环境初始化开发环境方案
多项目终端环境初始化开发环境方案
背景需求
在高峰期同时维护4个开发项目时,频繁切换环境存在以下痛点:
- 需手动进入不同目录,再启动前后端服务
- 多项目开发快速切换
- 主力开发机是MacBook/Mac Mini,需要优先支持macos
解决方案
通过AppleScript实现iTerm自动化环境切换,以个人的一个yutop项目为例:
tell application "iTerm"activate# Create new tabtell current windowcreate tab with default profileend tell# Split panetell current session of current windowsplit vertically with default profileend telltell first session of current tab of current windowsplit horizontally with default profileend tell# -- 后端服务tell first session of current tab of current windowwrite text "cd /opt/dir/yumore/yutop/top-go/"write text "go run main.go"end tell# -- 前端开发tell second session of current tab of current windowwrite text "cd /opt/dir/yumore/yutop/top-vue"write text "pnpm run dev"end tell# -- 版本控制lazygittell third session of current tab of current windowwrite text "cd /opt/dir/yumore/yutop/"write text "lazygit"end tell
end tell
以上内容写入文件yutop.scpt
执行方式
osascript yutop.scpt # 需提前在系统设置中授予iTerm自动化权限
后续扩展
- 封装为launch_project.sh批量管理运行多个.scpt文件
- 添加包装kill -9命令批量结束项目进程
当前方案已在实际开发中验证,切换效率提升90%