-
Notifications
You must be signed in to change notification settings - Fork 749
Expand file tree
/
Copy pathcheck-and-run-apollo-cli.sh
More file actions
executable file
·95 lines (79 loc) · 2.97 KB
/
check-and-run-apollo-cli.sh
File metadata and controls
executable file
·95 lines (79 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# Only major and minor version should be specified here
REQUIRED_APOLLO_CLI_VERSION=2.16
# Specify fully qualified version here. Ideally this should be a LTS version.
REQUIRED_NODE_VERSION=8.15.0
# Using npx to execute 'apollo' looks for a local install in node_modules before checking $PATH (for a global install)
APOLLO_CLI="npx --no-install apollo"
# Part of this code has been adapted from
# https://github.com/facebook/react-native/blob/master/scripts/react-native-xcode.sh
# This script is supposed to be invoked as part of the Xcode build process
# and relies on environment variables set by Xcode
if [[ -z "$CONFIGURATION" ]]; then
echo "$0 must be invoked as part of an Xcode script phase"
exit 1
fi
# Add MacPorts default bin path if the user has `port` command.
if [[ -x /opt/local/bin/port ]]; then
PATH="$PATH:/opt/local/bin"
fi
use_correct_node_via_nvm() {
nvm version $REQUIRED_NODE_VERSION > /dev/null
if [[ $? -eq 0 ]]; then
# The version of node that we want is installed.
nvm use $REQUIRED_NODE_VERSION
else
nvm install $REQUIRED_NODE_VERSION
fi
}
use_correct_node_via_nodenv() {
nodenv install -s $REQUIRED_NODE_VERSION
nodenv shell $REQUIRED_NODE_VERSION
}
if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
. "$HOME/.nvm/nvm.sh"
use_correct_node_via_nvm
elif [[ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]]; then
. "$(brew --prefix nvm)/nvm.sh"
use_correct_node_via_nvm
fi
# Set up the nodenv node version manager if present
if [[ -x "$HOME/.nodenv/bin/nodenv" ]]; then
eval "$("$HOME/.nodenv/bin/nodenv" init -)"
use_correct_node_via_nodenv
fi
parse_version() {
if [[ $1 =~ ([0-9\.]+) ]]; then
echo ${BASH_REMATCH[1]}
fi
}
get_installed_cli_version() {
version=$($APOLLO_CLI -v)
if [[ $? -eq 0 ]]; then
echo "$version"
fi
}
# We consider versions to be compatible if the major and minor versions match
are_versions_compatible() {
[[ "$(parse_version $1 | cut -d. -f1-2)" == $2 ]]
}
install_apollo_cli() {
echo "warning: Installing apollo@$REQUIRED_APOLLO_CLI_VERSION in your project directory to avoid version conflicts..."
# Exit immediately if the command fails
set -e
npm install --prefix "$PROJECT_DIR" --no-package-lock apollo@$REQUIRED_APOLLO_CLI_VERSION
set +e
}
# Check the installed version of the Apollo CLI, if available
INSTALLED_APOLLO_CLI_VERSION="$(get_installed_cli_version)"
if [[ -z "$INSTALLED_APOLLO_CLI_VERSION" ]]; then
echo "warning: Apollo iOS requires version $REQUIRED_APOLLO_CLI_VERSION.x of the Apollo CLI to be installed \
either globally or in a local node_modules directory."
install_apollo_cli
elif ! are_versions_compatible "$INSTALLED_APOLLO_CLI_VERSION" $REQUIRED_APOLLO_CLI_VERSION; then
echo "warning: The version of Apollo.framework in your project requires Apollo CLI $REQUIRED_APOLLO_CLI_VERSION.x, \
but $INSTALLED_APOLLO_CLI_VERSION seems to be installed."
install_apollo_cli
fi
# Print commands before executing them (useful for troubleshooting)
set -x
$APOLLO_CLI "$@"