#!/usr/bin/env bash

#
# install-yarn
#
# Copyright (C) 2022 Posit Software, PBC
#
# Unless you have received this program directly from Posit Software pursuant
# to the terms of a commercial license agreement with Posit Software, then
# this program is licensed to you under the terms of version 3 of the
# GNU Affero General Public License. This program is distributed WITHOUT
# ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
# MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
# AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
#
#

set -e

source "$(dirname "${BASH_SOURCE[0]}")/../tools/rstudio-tools.sh"
section "Installing yarn"

# determine node bin path then prepend to installation path
DEPS_DIR="$(pwd)"
NODE_BIN="${DEPS_DIR}/${NODE_SUBDIR}/bin"
INSTALL_PATH="${NODE_BIN}:${PATH}"

# try to ensure node is on the PATH
PATH="${NODE_BIN}:${PATH}"

# check for yarn installation
# TODO: do we want to allow yarn installs from other PATH locations?
if has-program yarn; then
  echo "yarn already installed at '$(which yarn)'"
else
  npm install -g yarn
fi
