#!/bin/bash

#
# Rush install runs into trouble when depending on packages that specify 'bin' script and this script does not
# exist at install/link time; rush tries to link the script to node_modules/.bin dir of the dependant package.
#
# However as is often the case, the script in package JSON's 'bin' dir is not immediately present after the
# repo is cloned. It is a result of build - and thus chicken-egg problem happens.
#
# We run into this problem with our catalog-export CLI tool; which is depended on by reference-workspace. In order
# to get out of the deadlock, this script is registered as preInstall hook. Its goal is to verify whether
# the catalog export's CLI entry point exists. If not, it will create an empty file and the rush install / link
# will work.
#
# The subsequent build / rebuild will then overwrite the dummy file and everything will be on track.
#

CURRENT_DIR=$(echo $(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P))
FILE="$CURRENT_DIR/../../tools/catalog-export/esm/index.js";
DIR=`dirname ${FILE}`

if [ ! -f ${FILE} ]; then
  mkdir -p ${DIR}
  cat >$FILE << EOF
#!/usr/bin/env node
console.error("You are calling dummy-garage bootstrapped version of catalog export; this was created by bootstrap-catalog-export preInstall hook - in order for rush link to work. Please run rush rebuild.");
EOF
fi
