Skip to content

Instantly share code, notes, and snippets.

@bryanstearns
Last active November 21, 2022 21:34
Show Gist options
  • Save bryanstearns/b58840f1f90e1e7470023213196b29e0 to your computer and use it in GitHub Desktop.
Save bryanstearns/b58840f1f90e1e7470023213196b29e0 to your computer and use it in GitHub Desktop.
Clean rebuild script for React Native, handy when upgrading dependencies
#!/bin/bash
# Do clean rebuilds of both platforms after reinstalling fresh dependencies
# Useful during (eg) periodic upgrades of dependencies, to make sure everything
# still works at each step. Run one iOS and one Android simulator first.
# Keep this script outside your source tree (eg, /usr/local/bin/rn_rebuild)
set -x # echo each command
set -e # die on error (except when we "|| set exit 0")
# Extract our app's bundle ID.
BUNDLE_ID=`perl <android/app/build.gradle -n -e '/^\s+applicationId "(.*)"$/ && print $1'`
# Uninstall the iOS app
xcrun simctl terminate booted $BUNDLE_ID || set exit 0
xcrun simctl uninstall booted $BUNDLE_ID || set exit 0
# Uninstall the Android app
adb uninstall $BUNDLE_ID || set exit 0
# Kill the Metro bundler.
pkill -f "$(pwd)/node_modules/react-native/scripts/../cli.js start" || set exit 0
# Remove all installed dependencies and previous build results.
# (Might be better not to remove the two lockfiles here, to avoid seeing
# indirect soft-dependency version bumps because of new released, but :shrug:)
/bin/rm -rf node_modules ios/Pods ios/build android/build android/app/build \
ios/Podfile.lock yarn.lock
# Install all our dependencies afresh.
yarn
(cd ios; pod install)
# Build and test
react-native run-android
react-native run-ios
npm t
./node_modules/.bin/tsc --noEmit
./node_modules/.bin/tslint -p . --type-check
# Show any changed files (usually lockfiles if a soft dependency changed.)
CHANGES=`git status -s`
[[ -n $CHANGES ]] &&
(echo -e "\033[0;31mUh-oh, files changed!\033[0m\n$CHANGES"; exit 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment