swap-domain.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. set -euo pipefail
  15. FROM="briven.tech"
  16. TO="briven.tech"
  17. APPLY=0
  18. while [[ $# -gt 0 ]]; do
  19. case "$1" in
  20. --from) FROM="$2"; shift 2 ;;
  21. --to) TO="$2"; shift 2 ;;
  22. --apply) APPLY=1; shift ;;
  23. -h|--help)
  24. sed -n '2,13p' "$0" | sed 's/^# \{0,1\}//'
  25. exit 0
  26. ;;
  27. *)
  28. echo "unknown flag: $1" >&2
  29. exit 2
  30. ;;
  31. esac
  32. done
  33. if [[ "$FROM" == "$TO" ]]; then
  34. echo "from and to are the same — nothing to do" >&2
  35. exit 1
  36. fi
  37. repo_root="$(cd "$(dirname "$0")/.." && pwd)"
  38. cd "$repo_root"
  39. # Collect matching files via NUL-delimited grep. bash strips embedded NULs
  40. # from `$(...)`-captured strings, so we read into an array with `-d ''`
  41. # instead of piping through a `$()` substitution.
  42. files=()
  43. while IFS= read -r -d '' f; do
  44. files+=("$f")
  45. done < <(
  46. grep -rIl --null \
  47. --exclude-dir=node_modules \
  48. --exclude-dir=.git \
  49. --exclude-dir=.next \
  50. --exclude-dir=.turbo \
  51. --exclude-dir=dist \
  52. --exclude-dir=.pnpm-store \
  53. --exclude-dir=coverage \
  54. --exclude='pnpm-lock.yaml' \
  55. -- "$FROM" . || true
  56. )
  57. count=${#files[@]}
  58. if [[ "$count" -eq 0 ]]; then
  59. echo "no files contain '$FROM'"
  60. exit 0
  61. fi
  62. echo "found '$FROM' in $count file(s):"
  63. if [[ "$APPLY" -eq 0 ]]; then
  64. for f in "${files[@]}"; do
  65. n=$(grep -c -- "$FROM" "$f" || true)
  66. printf ' %4dx %s\n' "$n" "$f"
  67. done
  68. echo
  69. echo "dry-run only. re-run with --apply to perform the swap."
  70. exit 0
  71. fi
  72. # sed -i is non-portable: macOS/BSD requires an empty arg, GNU does not.
  73. # Write through a per-file temp to sidestep the difference.
  74. for f in "${files[@]}"; do
  75. tmp="$f.swap.$$"
  76. # Plain literal substitution — `.` in domain names matches any char in
  77. # sed regex, but the only "briven.tech"-shaped strings in the repo are
  78. # actual `briven.tech` URIs, so the false-positive surface is empty.
  79. sed "s|$FROM|$TO|g" "$f" > "$tmp"
  80. mv "$tmp" "$f"
  81. done
  82. echo "rewrote $count file(s). review with: git diff"