-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeg
executable file
·109 lines (92 loc) · 3.89 KB
/
keg
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env bash
# Opening to the keg cli
# For navigation commands calls node script to load the path from keg.config.json
# All other commands call the root keg-cli node script
keg(){
# Check if the keg-cli root dir has been set
# If not set, then this code will find the root path based on the BASH_SOURCE[0] argument
# Works when the Keg-CLI installed in the keg-hub/repos and is symlinked into the /usr/local/bin
if [[ -z "$KEG_CLI_PATH" ]]; then
# Get the KEG-CLI path based on the keg root directory
local SOURCE="${BASH_SOURCE[0]}"
# Resolve $SOURCE until the file is no longer a symlink
while [ -h "$SOURCE" ]; do
KEG_CLI_PATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
# If $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
[[ $SOURCE != /* ]] && SOURCE="$KEG_CLI_PATH/$SOURCE"
done
KEG_CLI_PATH="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
export KEG_CLI_PATH
fi
# Check if reloading the bash portion of the keg cli
if [[ "$1" == "reload" || "$1" == "rl" || "$1" == "src" ]] && [[ -z "$2" ]]; then
# Reload this bash script
echo "[ KEG CLI ] Reloading Keg CLI..." >&2
source $KEG_CLI_PATH/keg
return
# Setup a symlink for the keg-cli
# Allows developing on the keg-cli from to repo and not the globally installed module
elif [[ "$1" == "dev" ]] && [[ "$2" == "symlink" || "$2" == "sym" ]]; then
local YARN_GLOBAL_DIR="$(yarn global dir)"
echo "[ KEG CLI ] Creating symlink for local keg-cli development..." >&2
# Remove the old symlink, and create a new one linked to the repo file
rm -rf $YARN_GLOBAL_DIR/node_modules/.bin/keg
# TODO: Update $KEG_CLI_PATH to be the locally installed version keg KEG-CLI
# Unless the symlink already exists, it will point to the globally installed version
# Which will just re-create the old symlink
ln -s $KEG_CLI_PATH/keg $YARN_GLOBAL_DIR/node_modules/.bin/keg
return 0
# Shortcut to navigate to the users home directory
elif [[ "$1" == "home" || "$1" == "hm" ]] && [[ -z "$2" ]]; then
cd ~/
return 0
# Check to navigate to a custom keg path ( internal app || linked taps )
# Only run this code if 1 or less arguments are passed
elif [[ "$#" == "0" || "$#" == "1" ]]; then
local CMD_OUTPUT
# If no arguments, get the root keg path
if [[ "$#" == "0" ]]; then
CMD_OUTPUT="$(node $KEG_CLI_PATH/scripts/cli/getConfigPath.js keg)"
else
CMD_OUTPUT="$(node $KEG_CLI_PATH/scripts/cli/getConfigPath.js $@)"
fi
# Convert the ouput to an array
IFS=$'\n'
local OUTPUT_ARR=($CMD_OUTPUT)
unset IFS
# Get the second line containing the path to move to
local MOVE_TO_PATH="${OUTPUT_ARR[1]}"
# If we have a path to move to
# Do the move, then return so we don't run any other code
if [[ "$MOVE_TO_PATH" ]]; then
echo "${OUTPUT_ARR[0]}"
cd $MOVE_TO_PATH
echo ""
return 0
fi
fi
# Auto load the envs before running the keg-cli cmd
# Ensures any required keg-cli ENV's are loaded
if [[ -z "$KEG_ENV_LOADED" ]]; then
# Check if $KEG_DOCKER_NAME is already loaded
if [[ -z "$KEG_DOCKER_NAME" ]]; then
local KEG_DM_ENVS=$KEG_CLI_PATH/scripts/docker/docker-desktop.env
# Ensure the file exists
if [[ -f "$KEG_DM_ENVS" ]]; then
# Load the docker ENVs, but route the output to dev/null
# This way nothing is printed to the terminal
set -o allexport
source $KEG_DM_ENVS >/dev/null 2>&1
set +o allexport
fi
fi
# Set the keg auto loaded to true, so we don't call the auto load again
# for any future keg-cli commands
export KEG_ENV_LOADED="loaded"
fi
# All other commands get passed on to the node keg-cli script
node $KEG_CLI_PATH/keg-cli $@
# return the exit code of the node keg-cli script
return "$?"
}