#!/usr/bin/env sh
dir=$(pwd)

# shellcheck disable=SC2129
printf "\n"
printf "|##########################################|\n"
printf "|            Write to gitignore            |\n"
printf "|##########################################|\n"
printf "\n"
printf "\n" >> "$dir/.gitignore"
printf "\n###Yarn 2" >> "$dir/.gitignore"
printf "\n.yarn/*" >> "$dir/.gitignore"
printf "\n!.yarn/releases" >> "$dir/.gitignore"
printf "\n!.yarn/plugins" >> "$dir/.gitignore"
printf "\n.pnp.*" >> "$dir/.gitignore"

# removes node_modules
printf "\n"
printf "|##########################################|\n"
printf "|            Delete node_modules           |\n"
printf "|##########################################|\n"
printf "\n"
rm -rf node_modules

# Upgrade Yarn to Berry (latest Yarn 2 version known to Yarn 1)
printf "\n"
printf "|##########################################|\n"
printf "|            Set version berry             |\n"
printf "|##########################################|\n"
printf "\n"
yarn set version berry

# (optional) Adds `yarn upgrade-interactive` command back
printf "\n"
printf "|##########################################|\n"
printf "|           Add interactive-tools          |\n"
printf "|##########################################|\n"
printf "\n"
yarn plugin import interactive-tools

# Cleans yarn cache
printf "\n"
printf "|##########################################|\n"
printf "|             Cleans yarn cache            |\n"
printf "|##########################################|\n"
printf "\n"
yarn cache clean

# Re-installs project using Yarn 2. This will also update your yarn.lock file, but will NOT sneakily upgrade the dependencies
printf "\n"
printf "|##########################################|\n"
printf "|               Yarn install               |\n"
printf "|##########################################|\n"
printf "\n"
yarn install