#!/bin/bash
source "$ROOT_PATH"/scripts/common.sh
set -eo pipefail

# Function to handle errors
handle_error() {
    echo_red_bg "Error: $1"
    exit 1
}

if [ ! "${IS_MULTI_SERVICE_REPO}" = "true" ]; then  # mono service repo:
  echo_red "IS_MULTI_SERVICE_REPO var is not equal to true. I assume this is mono service repo"
else
  mv package.json old.json || handle_error "failed to create backup of package.json"
  jq -s '.[0] * .[1]' old.json  "$LAMBDA_PATH/package.json" > package.json && cat package.json || handle_error "failed to merge package json from $LAMBDA_PATH to root dir"
fi

echo_red "run npm ci && npm run build:lambda"
npm ci && npm run build:lambda || handle_error "failed to run npm ci and npm run build:lambda"

echo_green "build succeed"