RN Command Tools (Mac Only)

為了在開發過程中節省時間,把一些常用的Command集成了.sh檔

安裝

可把底下代碼另存為ReactNative.sh,按以下Structure存放

再Run以下Command:

cd ReactNative.sh存放位置
chmod+x ReactNative.sh

以rrn command運行 RN Command Tools

編輯/Users/你的用戶名/.zshrc

#加入
alias rrn="ReactNative.sh存放位置/ReactNative.sh"

Double Click 開啟即可

RN Command Tools代碼

*Open Editor 預設使用Sublime Text 3

#!/bin/bash

mydir="$(dirname "$BASH_SOURCE")"
projectPath=$mydir"/Projects/"

main(){
echo "



┌────────────────────────────────────────────────────────────────────────────┐
│                                                                            │
│  Welcome To React Native                                                   │
│                                                                            │
│  Action:                                                                   │
│  -----------------------------------------------------------------------   │
│  1) Select Project                                                         │
│  2) Create Project                                                         │
│  3) Clear iOS Unused Simulator(s)                                          │
│  e) Exit                                                                   │
│  -----------------------------------------------------------------------   │
│                                                                            │
└────────────────────────────────────────────────────────────────────────────┘

Current Directory: $mydir                                                 
";

read -p "Please Select(1-3)[Default 1]: " mainOption;
if [ "$mainOption" == 1 ];  then
    selectProject
elif [ "$mainOption" == 2 ]; then
  createProject
elif [ "$mainOption" == 3 ]; then
    echo "Clear iOS Unused Simulator(s)"
    xcrun simctl delete unavailable
    echo "[Success]"
    main
elif [ "$mainOption" == "e" ]; then
echo "Exit"
exit 0;
else
    selectProject
fi
}



createProject(){
#Move to Project Path
cd $projectPath

echo "
┌────────────────────────────────────────────────────────────────────────────┐ 
│                                                                            │ 
│  Create React Native Project                                               │ 
│                                                                            │
└────────────────────────────────────────────────────────────────────────────┘
  $projectPath                                        
 ";
newProject

}

newProject(){

read -p  "Please enter New Project Name: (e: Back to MainMenu):" projectName
if [ "$projectName" == "e" ];  then
    echo "Exit Create New Project"
main
else
v=0
array=($(ls -d */))
size=$((${#array[@]}-1));
for (( i=0; i<${#array[@]}; i++ ));
do
if [ "$projectName"/ == ${array[i]} ];  then
((v++))
fi
done

if [ "$v" == 0 ]; then
react-native init $projectName
cd $projectPath$projectName"/ios"
pod init
main
else
    echo "Project Exist, Please Rename Your NEW Project."
newProject
fi

fi
}


selectProject(){

#Move to Project Path
cd $projectPath

echo "
┌────────────────────────────────────────────────────────────────────────────┐ 
│                                                                            │ 
│  Please Select Reacty Native Project:                                      │ 
│                                                                            │ 
└────────────────────────────────────────────────────────────────────────────┘
------------------------------------------------------------------------------
 ";

array=($(ls -d */))
size=$((${#array[@]}-1));
for (( i=0; i<${#array[@]}; i++ ));
do
 echo $(($i+1))") " ${array[i]}; 
done

echo "------------------------------------------------------------------------------";

read -p "Please Choose the Project (1-${#array[@]}): " projectCount
projectName=${array[$((projectCount-1))]%?}

echo "Opening Project:"
thisProject=$projectPath${array[$((projectCount-1))]}
echo $thisProject
cd $thisProject

subMenu $thisProject
}




subMenu(){
    cd $1
echo "



Current Project: $1

┌────────────────────────────────────────────────────────────────────────────┐
│                                                                            │
│  Action:                                                                   │
│  -----------------------------------------------------------------------   │
│   1) Open Editor (sublime)                                                 │
│   2) Open Editor (VSCode)                                                  |
│   3) Open Folder                                                           │
│   4) Run On iOS                                                            │
│   5) Run On Android                                                        │
│   6) Run On Android (Device)                                               │
│   7) Run On iOS (Release)                                                  │
│   8) Run On Android (Release)                                              │
│   9) Custom Command                                                        │
│  10) Install Plugin                                                        │
│  11) Batch Install Plugin                                                  │
│  12) Uninstall Plugin                                                      │
│  13) Reinstall Plugin                                                      │
│  14) Generating a signing key (Android)                                    │
|  15) Format Project                                                        │
│  16) Initialize Project                                                    │
|  17) Open iOS Project                                                      |
|  18) Open Android Project                                                  |
|  19) Generating Android APK                                                |
|  20) Check Version                                                         |
|  21) Upgrade React Native                                                  |
│   e) Back to Main Menu                                                     │
│  -----------------------------------------------------------------------   │
│                                                                            │
└────────────────────────────────────────────────────────────────────────────┘

";
read -p "Please Select(1-21)[Default 1]: " subOption; 
if [ "$subOption" == 1 ];  then
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime
sublime $1
subMenu $1
elif [ "$subOption" == 2 ];  then
#VS Code
VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* $1
subMenu $1
elif [ "$subOption" == 3 ];  then
open $1
subMenu $1
elif [ "$subOption" == 4 ];  then
react-native run-ios --simulator="iPhone 5s"
subMenu $1
elif [ "$subOption" == 5 ]; then
runAndroidEmu $1
elif [ "$subOption" == 6 ]; then
adb reverse tcp:8081 tcp:8081
react-native run-android
subMenu $1

elif [ "$subOption" == 7 ]; then
react-native bundle --entry-file index.ios.js --platform ios --dev false --bundle-output ios/main.jsbundle --assets-dest ios
react-native run-ios  --variant=release

elif [ "$subOption" == 8 ]; then
adb reverse tcp:8081 tcp:8081
react-native run-android  --variant=release
subMenu $1
elif [ "$subOption" == 9 ]; then
    runCommand $1

elif [ "$subOption" == 10 ]; then
installPlugin $1
elif [ "$subOption" == 11 ]; then
installPluginList $1
elif [ "$subOption" == 12 ]; then
read -p "what package you would like to uninstall? " packageName2
npm uninstall $packageName2
echo"

Uninstall Finish!
";
subMenu $1
elif [ "$subOption" == 13 ]; then
read -p "what package you would like to reinstall? " packageName3
npm uninstall $packageName3
npm install $packageName3 --save
react-native link
echo"
Reinstall Finish!
";
subMenu $1
elif [ "$subOption" == 14 ]; then
cd $thisProject"/android/app"
keytool -genkey -v -keystore my-release-key.keystore -alias my-key-alias -keyalg RSA -keysize 2048 -validity 10000
cd $thisProject
subMenu $1
elif [ "$subOption" == 15 ]; then
watchman watch-del-all
rm -rf node_modules && npm install
rm -fr $TMPDIR/react-*
subMenu $1
elif [ "$subOption" == 16 ]; then
npm init $1
subMenu $1
elif [ "$subOption" == 17 ]; then

if [ -f $thisProject'ios/'$projectName'.workspace' ]
then
    open $thisProject'ios/'$projectName'.workspace'
else
    open $thisProject'ios/'$projectName'.xcodeproj'
fi
    subMenu $1
elif [ "$subOption" == 18 ]; then

open -a /Applications/Android\ Studio.app $thisProject'android'
cd $thisProject
    subMenu $1
elif [ "$subOption" == 19 ]; then
    cd $thisProject"/android"
    ./gradlew assembleRelease
    open $thisProject"/android/app/build/outputs/apk/"
    cd $thisProject
    subMenu $1
elif [ "$subOption" == 20 ]; then
    react-native -v
    subMenu $1
elif [ "$subOption" == 21 ]; then
    react-native upgrade
    subMenu $1
elif [ "$subOption" == 'e' ]; then
    main
else
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/sublime
sublime $1
subMenu $1
fi
}

installPlugin(){
echo "

----------------------------------------------------------------------- 
"
read -p "what package you would like to install? (e: Back to SubMenu)" packageName1

if [ "$packageName1" == "e" ];  then
echo "[Exit Plugin install]"
subMenu    $1
else
yarn add $packageName1 --save
react-native link
echo"Install Finish!"
installPlugin $1
fi

}


runCommand(){
echo "

----------------------------------------------------------------------- 
Please enter command (e: Back to SubMenu):"
read yourCommand

if [ "$yourCommand" == "e" ];  then
echo "[Exit Custom Command]"
subMenu    $1
else
    $yourCommand
    runCommand $1
fi
}

runAndroidEmu(){

emuArray=($(emulator -list-avds */))
emuSize=$((${#array[@]}-1));
for (( i=0; i<${#emuArray[@]}; i++ ));
do
echo $(($i+1))") " ${emuArray[i]}; 
done
echo "------------------------------------------------------------------------------";

read -p "Please Choose the Emulator (1-${#array[@]})[Default 1]: " emuCount

echo "Opening Emulator:"

if [ "$emuCount" == "" ];  then
emuCount=1
echo ${emuArray[$((emuCount-1))]}
else
    echo ${emuArray[$((emuCount-1))]}
    fi
osascript -e 'tell app "Terminal"
    do script "'$ANDROID_HOME'/tools/emulator -avd '${emuArray[$((emuCount-1))]}'"
end tell'

adb reverse tcp:8081 tcp:8081
react-native run-android

subMenu    $1
}


installPluginList(){
echo "Reading "$1"plugin.txt"
    if [ -f $1"plugin.txt" ]
then
    exec 3<$1"plugin.txt"
for line in $(cat plugin.txt);

do
    echo -e "Install $line "
    yarn add $line --save
done <&3
react-native link
echo "[Install Finish]"
subMenu    $1
else
    echo $1"plugin.txt not found."
    subMenu    $1
fi

}

main

results matching ""

    No results matching ""