|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Usage: yarn new-worktree feat/31-my-feature |
| 4 | + |
| 5 | +BRANCH=$1 |
| 6 | +MAIN_REPO=$(pwd) |
| 7 | +WORKTREE_DIR="../wt-$(echo $BRANCH | sed 's/\//-/g')" |
| 8 | + |
| 9 | +if [ -z "$BRANCH" ]; then |
| 10 | + echo "❌ Please provide a branch name" |
| 11 | + echo "Usage: yarn new-worktree feat/my-feature" |
| 12 | + exit 1 |
| 13 | +fi |
| 14 | + |
| 15 | +# Create the worktree and branch |
| 16 | +git worktree add -b $BRANCH $WORKTREE_DIR main |
| 17 | + |
| 18 | +# Copy .vscode if present |
| 19 | +if [ -d "$MAIN_REPO/.vscode" ]; then |
| 20 | + cp -r "$MAIN_REPO/.vscode" "$WORKTREE_DIR/.vscode" |
| 21 | +else |
| 22 | + echo "ℹ️ No .vscode directory found in main repo, skipping copy." |
| 23 | +fi |
| 24 | + |
| 25 | +# Copy all .env files |
| 26 | +for env_file in "$MAIN_REPO"/.env*; do |
| 27 | + [ -f "$env_file" ] && cp "$env_file" "$WORKTREE_DIR/$(basename $env_file)" |
| 28 | +done |
| 29 | + |
| 30 | +# Copy node_modules via hard links (fast, minimal disk usage) |
| 31 | +# Fall back to yarn install if node_modules doesn't exist in main repo |
| 32 | +if [ -d "$MAIN_REPO/node_modules" ]; then |
| 33 | + echo "📦 Hard-linking node_modules..." |
| 34 | + if ! cp -rl "$MAIN_REPO/node_modules" "$WORKTREE_DIR/node_modules"; then |
| 35 | + echo "⚠️ Hard-linking node_modules failed, running yarn install..." |
| 36 | + (cd "$WORKTREE_DIR" && yarn install) |
| 37 | + fi |
| 38 | +else |
| 39 | + echo "📦 node_modules not found in main repo, running yarn install..." |
| 40 | + (cd "$WORKTREE_DIR" && yarn install) |
| 41 | +fi |
| 42 | + |
| 43 | +# Open in VSCode |
| 44 | +if command -v code &> /dev/null; then |
| 45 | + code $WORKTREE_DIR |
| 46 | +else |
| 47 | + echo "⚠️ VS Code CLI not found. Open the worktree manually: $WORKTREE_DIR" |
| 48 | +fi |
| 49 | + |
| 50 | +echo "✅ Worktree ready at $WORKTREE_DIR on branch $BRANCH" |
| 51 | + |
0 commit comments