swap-domain.sh93 lines · main
| 1 | #!/usr/bin/env bash |
| 2 | # swap-domain.sh — replace one briven public domain with another across the |
| 3 | # entire repo. designed for the briven.tech → briven.tech cutover, but |
| 4 | # parameterised so it works for any rename. |
| 5 | # |
| 6 | # usage: |
| 7 | # ./scripts/swap-domain.sh # dry-run, briven.tech → briven.tech |
| 8 | # ./scripts/swap-domain.sh --apply # actually edit |
| 9 | # ./scripts/swap-domain.sh --from foo.dev --to bar.dev # different swap |
| 10 | # |
| 11 | # the dry-run prints every file that would change and the count of hits per |
| 12 | # file so you can sanity-check the surface before committing. it never |
| 13 | # touches node_modules, .git, .next, .turbo, dist, or any binary. |
| 14 | |
| 15 | set -euo pipefail |
| 16 | |
| 17 | FROM="briven.tech" |
| 18 | TO="briven.tech" |
| 19 | APPLY=0 |
| 20 | |
| 21 | while [[ $# -gt 0 ]]; do |
| 22 | case "$1" in |
| 23 | --from) FROM="$2"; shift 2 ;; |
| 24 | --to) TO="$2"; shift 2 ;; |
| 25 | --apply) APPLY=1; shift ;; |
| 26 | -h|--help) |
| 27 | sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//' |
| 28 | exit 0 |
| 29 | ;; |
| 30 | *) |
| 31 | echo "unknown flag: $1" >&2 |
| 32 | exit 2 |
| 33 | ;; |
| 34 | esac |
| 35 | done |
| 36 | |
| 37 | if [[ "$FROM" == "$TO" ]]; then |
| 38 | echo "from and to are the same — nothing to do" >&2 |
| 39 | exit 1 |
| 40 | fi |
| 41 | |
| 42 | repo_root="$(cd "$(dirname "$0")/.." && pwd)" |
| 43 | cd "$repo_root" |
| 44 | |
| 45 | # Collect matching files via NUL-delimited grep. bash strips embedded NULs |
| 46 | # from `$(...)`-captured strings, so we read into an array with `-d ''` |
| 47 | # instead of piping through a `$()` substitution. |
| 48 | files=() |
| 49 | while IFS= read -r -d '' f; do |
| 50 | files+=("$f") |
| 51 | done < <( |
| 52 | grep -rIl --null \ |
| 53 | --exclude-dir=node_modules \ |
| 54 | --exclude-dir=.git \ |
| 55 | --exclude-dir=.next \ |
| 56 | --exclude-dir=.turbo \ |
| 57 | --exclude-dir=dist \ |
| 58 | --exclude-dir=.pnpm-store \ |
| 59 | --exclude-dir=coverage \ |
| 60 | --exclude='pnpm-lock.yaml' \ |
| 61 | -- "$FROM" . || true |
| 62 | ) |
| 63 | |
| 64 | count=${#files[@]} |
| 65 | if [[ "$count" -eq 0 ]]; then |
| 66 | echo "no files contain '$FROM'" |
| 67 | exit 0 |
| 68 | fi |
| 69 | |
| 70 | echo "found '$FROM' in $count file(s):" |
| 71 | |
| 72 | if [[ "$APPLY" -eq 0 ]]; then |
| 73 | for f in "${files[@]}"; do |
| 74 | n=$(grep -c -- "$FROM" "$f" || true) |
| 75 | printf ' %4dx %s\n' "$n" "$f" |
| 76 | done |
| 77 | echo |
| 78 | echo "dry-run only. re-run with --apply to perform the swap." |
| 79 | exit 0 |
| 80 | fi |
| 81 | |
| 82 | # sed -i is non-portable: macOS/BSD requires an empty arg, GNU does not. |
| 83 | # Write through a per-file temp to sidestep the difference. |
| 84 | for f in "${files[@]}"; do |
| 85 | tmp="$f.swap.$$" |
| 86 | # Plain literal substitution — `.` in domain names matches any char in |
| 87 | # sed regex, but the only "briven.tech"-shaped strings in the repo are |
| 88 | # actual `briven.tech` URIs, so the false-positive surface is empty. |
| 89 | sed "s|$FROM|$TO|g" "$f" > "$tmp" |
| 90 | mv "$tmp" "$f" |
| 91 | done |
| 92 | |
| 93 | echo "rewrote $count file(s). review with: git diff" |