diff --git a/README.md b/README.md index 2ab4092..ea0265f 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,32 @@ yarn e2e:run yarn e2e:open ``` +## Git Worktrees + +The project includes scripts to manage [Git worktrees](https://git-scm.com/docs/git-worktree), allowing you to work on multiple branches simultaneously in separate directories without stashing or switching branches. + +### Create a new worktree + +``` +yarn new-worktree feat/my-feature +``` + +This will: +1. Create a new branch (`feat/my-feature`) and check it out in a sibling directory named `wt-feat-my-feature` +2. Copy `.vscode` settings and all `.env*` files from the main repo +3. Hard-link `node_modules` from the main repo (fast, minimal extra disk usage) — or run `yarn install` if none exist +4. Open the new worktree in a new VS Code window + +> If the new branch changes `package.json` dependencies, run `yarn install` inside the worktree directory to reconcile. + +### Remove a worktree + +``` +yarn remove-worktree feat/my-feature +``` + +This will remove the worktree directory and delete the local branch. The branch must be fully merged before deletion (uses `git branch -d`). + ## API Types Generation The project includes scripts for generating TypeScript types from OpenAPI specifications: diff --git a/package.json b/package.json index 63d219c..e42b156 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,9 @@ "generate:api-types:output": "node scripts/generate-api-types.mjs", "generate:api-types": "node scripts/generate-api-types.mjs src/app/services/feeds/types.ts", "generate:gbfs-validator-types:output": "npm exec -- openapi-typescript ./external_types/GbfsValidator.yaml -o $npm_config_output_path && eslint $npm_config_output_path --fix", - "generate:gbfs-validator-types": "npm run generate:gbfs-validator-types:output -- --output-file=src/app/services/feeds/gbfs-validator-types.ts" + "generate:gbfs-validator-types": "npm run generate:gbfs-validator-types:output -- --output-file=src/app/services/feeds/gbfs-validator-types.ts", + "new-worktree": "bash scripts/new-worktree.sh", + "remove-worktree": "bash scripts/remove-worktree.sh" }, "resolutions": { "tar": "^7.5.7" diff --git a/scripts/new-worktree.sh b/scripts/new-worktree.sh new file mode 100755 index 0000000..0d7d45a --- /dev/null +++ b/scripts/new-worktree.sh @@ -0,0 +1,51 @@ +#!/bin/bash + +# Usage: yarn new-worktree feat/31-my-feature + +BRANCH=$1 +MAIN_REPO=$(pwd) +WORKTREE_DIR="../wt-$(echo $BRANCH | sed 's/\//-/g')" + +if [ -z "$BRANCH" ]; then + echo "❌ Please provide a branch name" + echo "Usage: yarn new-worktree feat/my-feature" + exit 1 +fi + +# Create the worktree and branch +git worktree add -b $BRANCH $WORKTREE_DIR main + +# Copy .vscode if present +if [ -d "$MAIN_REPO/.vscode" ]; then + cp -r "$MAIN_REPO/.vscode" "$WORKTREE_DIR/.vscode" +else + echo "â„šī¸ No .vscode directory found in main repo, skipping copy." +fi + +# Copy all .env files +for env_file in "$MAIN_REPO"/.env*; do + [ -f "$env_file" ] && cp "$env_file" "$WORKTREE_DIR/$(basename $env_file)" +done + +# Copy node_modules via hard links (fast, minimal disk usage) +# Fall back to yarn install if node_modules doesn't exist in main repo +if [ -d "$MAIN_REPO/node_modules" ]; then + echo "đŸ“Ļ Hard-linking node_modules..." + if ! cp -rl "$MAIN_REPO/node_modules" "$WORKTREE_DIR/node_modules"; then + echo "âš ī¸ Hard-linking node_modules failed, running yarn install..." + (cd "$WORKTREE_DIR" && yarn install) + fi +else + echo "đŸ“Ļ node_modules not found in main repo, running yarn install..." + (cd "$WORKTREE_DIR" && yarn install) +fi + +# Open in VSCode +if command -v code &> /dev/null; then + code $WORKTREE_DIR +else + echo "âš ī¸ VS Code CLI not found. Open the worktree manually: $WORKTREE_DIR" +fi + +echo "✅ Worktree ready at $WORKTREE_DIR on branch $BRANCH" + diff --git a/scripts/remove-worktree.sh b/scripts/remove-worktree.sh new file mode 100755 index 0000000..4ecad8a --- /dev/null +++ b/scripts/remove-worktree.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +# Usage: yarn remove-worktree feat/31-my-feature + +BRANCH=$1 +WORKTREE_DIR="../wt-$(echo $BRANCH | sed 's/\//-/g')" + +if [ -z "$BRANCH" ]; then + echo "❌ Please provide a branch name" + echo "Usage: yarn remove-worktree feat/my-feature" + exit 1 +fi + +if [ ! -d "$WORKTREE_DIR" ]; then + echo "❌ Worktree directory $WORKTREE_DIR does not exist" + exit 1 +fi + +# Remove the worktree +git worktree remove "$WORKTREE_DIR" + +# Delete the branch +git branch -d $BRANCH + +echo "đŸ—‘ī¸ Worktree $WORKTREE_DIR removed and branch $BRANCH deleted" + \ No newline at end of file