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

# shellcheck disable=SC2129
printf "\n"
printf "|##########################################|"
printf "|            Write to gitignore            |"
printf "|##########################################|"
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 "|##########################################|"
printf "|            Delete node_modules           |"
printf "|##########################################|"
printf "\n"
rm -rf node_modules

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

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

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