## Script for testing npm package changes locally using a host app

echo "Welcome Developer, running this script will allow you to test the changes done on this package via an host app
        Entities involved
            1. Host app ( HA )
            2. Package app ( PA ) - NPM Package being developed"

# Step 1
## Get current working directory
pwd 
PATH_TO_PA=$(pwd)

echo "Please enter the file path to the Host App
      P.S - Please make sure that both entities are present in the same directory"
read PATH_TO_HA

# Step 2
echo "Installing PA as a local package in HA"
cd ${PATH_TO_HA}/
npm i ${PATH_TO_PA}
echo "PA is successfully linked to the HA"
## Already have proper path from root directory, so no need for "/"
cd $PATH_TO_PA


# Step 3
echo "Please enter the peer dependencies required in the PA
      P.S - Please make sure these dependencies are being used in the HA"
read line

PEER_DEPENDENCIES_PACKAGE_NAMES=(${line})

# Step 4
echo "We are now trying to link peer dependencies of the HA package to our local PA (using yarn)"

for PEER_DEPENDENCY in ${PEER_DEPENDENCIES_PACKAGE_NAMES[@]};
do 
    cd ${PATH_TO_HA}/
    echo "********Linking $PEER_DEPENDENCY ************"
    cd node_modules/$PEER_DEPENDENCY
    yarn unlink
    yarn link
    echo "Reverting to $PATH_TO_PA, linking this Peer Dependency with PA"
    cd $PATH_TO_PA
    yarn unlink $PEER_DEPENDENCY
    yarn link $PEER_DEPENDENCY
done

# Step 5
echo "All these Peer Dependencies are Linked Successfully, ${PEER_DEPENDENCIES_PACKAGE_NAMES[@]}
      Running the build command, npm run build"

npm run build

echo "Linking was Successful!!
      Please Run the build command everytime you try to test the changes done on PA"


# TODOS
# 1] Directly listing the peer dependencies from package.json of PA
# 2] Unlink script must be created for this

## Remove above readme file or shift its contents to other file 
## Remove the custom string from the date time formfield 