Skip to content

Instantly share code, notes, and snippets.

@Zorgatone
Last active April 10, 2021 09:20
Show Gist options
  • Save Zorgatone/d1e6da5984c740cf3fe51214969df304 to your computer and use it in GitHub Desktop.
Save Zorgatone/d1e6da5984c740cf3fe51214969df304 to your computer and use it in GitHub Desktop.
react-native-clean function for use in .zshrc
react-native-clean() {
if [ ! -f "react-native.config.js" ] && [ ! -f "app.json" ]; then
echo >&2 "Probably not a react-native project root folder!"
return 1
else
echo "Detected react-native project root folder..."
fi
if [ -x "command -v watchman" ]; then
watchman watch-del-all
fi
if [[ -d "$TMPDIR"react-* ]]; then
rm -rf "$TMPDIR"react-*
fi
if [[ -d /tmp/react-* ]]; then
rm -rf /tmp/react-*
fi
if [[ -d "$TMPDIR"metro-* ]]; then
rm -rf "$TMPDIR"metro-*
fi
if [[ -d /tmp/metro-* ]]; then
rm -rf /tmp/metro-*
fi
# Pass option to clean gradle cache (next build will be very slow!)
if [[ "$1" = "--gradle-cache" || "$1" = "-gc" || "$2" = "--gradle-cache" || "$2" = "-gc" ]]; then
if [[ -d "$HOME"/.gradle/caches ]]; then
rm -rf "$HOME"/.gradle/caches
fi
fi
if [[ -d node_modules ]]; then
rm -rf node_modules
fi
if [ -x "$(command -v npm)" ]; then
echo "Cleaning node_modules..."
npm cache clean --force
if [[ "$1" = "--use-yarn" || "$1" = "-uy" || "$2" = "--use-yarn" || "$2" = "-uy" ]]; then
echo "Reinstalling node_modules using yarn..."
if [ -x "$(command -v yarn)" ]; then
yarn
elif npx --version >/dev/null 2>&1; then
npx yarn
else
echo >&2 "Could not find npx executable!"
return 1
fi
else
echo "Reinstalling node_modules using npm..."
npm ci
fi
else
echo >&2 "Could not find node_modules or npm command!"
fi
if [ -d "ios" ]; then
echo "Cleaning iOS..."
cd ios
if [ -x "$(command -v pod)" ]; then
echo "Reinstalling Pods..."
rm -rf Pods && pod install --repo-update #&& pod update
else
echo "Command pod not found"
fi
if [ -x "$(command -v xcodebuild)" ]; then
echo "Cleaning Xcode build..."
if [ -z "$(xcodebuild clean)" ]; then
echo "Xcode build cleaned correctly with xcodebuild"
else
rm -rf build && echo "Removed Xcode build folder correctly"
fi
else
echo "Command xcodebuild not found"
fi
cd ..
else
echo >&2 "Could not find ios folder!"
fi
if [ -d "android" ] && [ -f "android/gradlew" ]; then
echo "Cleaning Android..."
cd android
./gradlew clean
cd ..
if npx --version >/dev/null 2>&1; then
npx jetify
else
echo >&2 "Could not find npx executable!"
return 1
fi
else
echo >&2 "Could not find android folder or gradlew executable!"
fi
if [ -d "macos" ]; then
echo "Cleaning macOS..."
cd macos
if [ -x "$(command -v pod)" ]; then
echo "Reinstalling Pods..."
rm -rf Pods && pod install --repo-update #&& pod update
else
echo "Command pod not found"
fi
if [ -x "$(command -v xcodebuild)" ]; then
echo "Cleaning Xcode build..."
if [ -z "$(xcodebuild clean)" ]; then
echo "Xcode build cleaned correctly with xcodebuild"
else
rm -rf build && echo "Removed Xcode build folder correctly"
fi
else
echo "Command xcodebuild not found"
fi
cd ..
else
echo >&2 "Could not find macos folder!"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment