1#!/bin/bash
2# Copyright (c) Meta Platforms, Inc. and affiliates.
3#
4# This source code is licensed under the MIT license found in the
5# LICENSE file in the root directory of this source tree.
6
7# This script is used to source in Xcode the environment settings required to run properly.
8# The script first sources the base `.xcode.env` file.
9# Then it sources the `.xcode.env.local` file if present, to override some local config
10# Finally, it will execute the command passed i input if any.
11#
12# USAGE:
13# ./with-environment.sh command
14
15# Start with a default
16NODE_BINARY=$(command -v node)
17export NODE_BINARY
18
19# Override the default with the global environment
20ENV_PATH="$PODS_ROOT/../.xcode.env"
21if [ -f "$ENV_PATH" ]; then
22    source "$ENV_PATH"
23fi
24
25# Override the global with the local environment
26LOCAL_ENV_PATH="${ENV_PATH}.local"
27if [ -f "$LOCAL_ENV_PATH" ]; then
28    source "$LOCAL_ENV_PATH"
29fi
30
31# Check whether NODE_BINARY has been properly set, otherwise help the users with a meaningful error.
32if [ -n "$NODE_BINARY" ]; then
33    echo "Node found at: ${NODE_BINARY}"
34else
35    echo "[Warning] You need to configure your node path in the `'.xcode.env' file` environment. " \
36       "You can set it up quickly by running: " \
37       "echo 'export NODE_BINARY=$(command -v node)' > .xcode.env " \
38       "in the ios folder. This is needed by React Native to work correctly. " \
39       "We fallback to the DEPRECATED behavior of finding `node`. This will be REMOVED in a future version. " \
40       "You can read more about this here: https://reactnative.dev/docs/environment-setup#optional-configuring-your-environment" >&2
41    source "${REACT_NATIVE_PATH}/scripts/find-node-for-xcode.sh"
42fi
43
44# Execute argument, if present
45if [ -n "$1" ]; then
46  $1
47fi
48