#!/usr/bin/env bash
set -eo pipefail

# 1. CHECK IF APPLICABLE 
if [ ! -f "package.json" ] || [ ! -f ".node_version" ]; then
   exit 100
fi

# 2. GET ARGS
plan_path=$2

# 3. GET CONFIGURATION
version=14.17.1
if [[ -f .node_version ]]; then
    version=$(< .node_version tr -d '[:space:] | node-')
fi

# 4. DECLARE DEPENDENCIES
cat >> "${plan_path}" <<EOL
# Buildpack provides this dependency
#
# NOTE: The dependency is provided during the 'build' process.
#
[[provides]]
name = "nodejs"

# Buildpack requires this dependency
#
# NOTE: Everything aside from 'name' is simply additional information that the providing buildpack can use to resolve
#       the dependency.
#
[[requires]]
name = "nodejs"
version = "${version}"
EOL
