forked from cisco/node-jose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runas
executable file
·94 lines (79 loc) · 1.96 KB
/
runas
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
#! /usr/bin/env bash
#
# usage:
# assumes bash, homebrew, and nvm for managing node
## Load NVM
source $(brew --prefix nvm)/nvm.sh
# CONSTANTS
REFRESH_LIMIT=28800
ENVIRON=${1:-default}
shift 1
# Retain 'my' prompt
export CLICOLOR=1
export PS1="RUNAS{$ENVIRON} [\[\033[0;92m\]\D{%F}T\t\[\033[0m\]] \H:\W\[\033[0;91m\]$\[\033[0m\] "
RUN_ARGS=${*:-bash -i}
if [ ! -d env ] ; then
echo "Creating 'env' directory"
mkdir env
fi
## Remember next and current environment
NEXT_NODE=env/${ENVIRON}
CURR_NODE=
if [ -h node_modules ]; then
CURR_NODE=$(readlink -n node_modules)
fi
# Setup "switch to" and "revert to" commands
SWITCH_CMD=
REVERT_CMD=
if [ "${NEXT_NODE}" != "${CURR_NODE}" ] ; then
echo "Preserving existing '${CURR_NODE}' ..."
SWITCH_CMD="ln -sfn ${NEXT_NODE} node_modules"
if [ ! -z "${CURR_NODE}" ] ; then
REVERT_CMD="ln -sfn ${CURR_NODE} node_modules"
else
rm -f node_modules
fi
fi
## setp environment
RETCODE=
nvm use ${ENVIRON}
RETCODE=$?
if [ 0 -ne ${RETCODE} ]; then
# environment doesn't exist!
exit ${RETCODE}
fi
# create/update if needed
UPDATE_CMD="npm update -d"
UPDATE_TIME=$(date +%s)
if [ ! -d ${NEXT_NODE} ] ; then
echo "Creating '${NEXT_NODE}'"
mkdir -p ${NEXT_NODE}
UPDATE_CMD="npm install -d"
elif [ -f ${NEXT_NODE}/.updated ] ; then
UPDATE_LAST=$(cat ${NEXT_NODE}/.updated)
UPDATE_LAST=$(( ${UPDATE_LAST} + ${REFRESH_LIMIT} ))
if [ ${UPDATE_LAST} -gt ${UPDATE_TIME} ] ; then
UPDATE_CMD="true"
fi
fi
if [ ! -z "${SWITCH_CMD}" ] ; then
${SWITCH_CMD}
fi
if [ ! -z "${UPDATE_CMD}" ] ; then
echo "updating '${NEXT_NODE}'"
${UPDATE_CMD} &> /dev/null && \
echo ${UPDATE_TIME} > ${NEXT_NODE}/.updated
RETCODE=$?
fi
# run arguments
if [ 0 -eq ${RETCODE} ] ; then
echo "running '${RUN_ARGS}' under ${ENVIRON}"
${RUN_ARGS}
RETCODE=$?
fi
## if the environment was switched, revert it
if [ ! -z "${REVERT_CMD}" ]; then
echo "... reverting to '${CURR_NODE}'"
${REVERT_CMD}
fi
exit ${RETCODE}