[Refactor] create `nvm_grep` and remove standalone `GREP_OPTIONS` line.

Jordan Harband 2016-06-25 13:27:56 -07:00
parent 9f5322ee84
commit e1fdd316b7
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
1 changed files with 33 additions and 31 deletions

64
nvm.sh
View File

@ -25,6 +25,10 @@ nvm_err() {
>&2 nvm_echo "$@" >&2 nvm_echo "$@"
} }
nvm_grep() {
GREP_OPTIONS='' command grep "$@"
}
nvm_has() { nvm_has() {
type "$1" > /dev/null 2>&1 type "$1" > /dev/null 2>&1
} }
@ -333,7 +337,7 @@ nvm_remote_versions() {
return 1 return 1
fi fi
VERSIONS="$(nvm_echo "$(nvm_ls_remote "$PATTERN") VERSIONS="$(nvm_echo "$(nvm_ls_remote "$PATTERN")
$(nvm_ls_remote_iojs "$PATTERN")" | command grep -v "N/A" | command sed '/^$/d')" $(nvm_ls_remote_iojs "$PATTERN")" | nvm_grep -v "N/A" | command sed '/^$/d')"
;; ;;
esac esac
@ -596,7 +600,7 @@ nvm_resolve_alias() {
fi fi
if [ -n "$ALIAS_TEMP" ] \ if [ -n "$ALIAS_TEMP" ] \
&& command printf "$SEEN_ALIASES" | command grep -e "^$ALIAS_TEMP$" > /dev/null; then && command printf "$SEEN_ALIASES" | nvm_grep -e "^$ALIAS_TEMP$" > /dev/null; then
ALIAS="∞" ALIAS="∞"
break break
fi fi
@ -741,7 +745,7 @@ nvm_ls() {
local ZHS_HAS_SHWORDSPLIT_UNSET local ZHS_HAS_SHWORDSPLIT_UNSET
ZHS_HAS_SHWORDSPLIT_UNSET=1 ZHS_HAS_SHWORDSPLIT_UNSET=1
if nvm_has "setopt"; then if nvm_has "setopt"; then
ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; nvm_echo $?) ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | nvm_grep shwordsplit > /dev/null ; nvm_echo $?)
setopt shwordsplit setopt shwordsplit
fi fi
@ -775,13 +779,13 @@ nvm_ls() {
fi fi
fi fi
if ! [ -d "$NVM_DIRS_TO_SEARCH1" ] || ! (command ls -1qA "$NVM_DIRS_TO_SEARCH1" | command grep -q .); then if ! [ -d "$NVM_DIRS_TO_SEARCH1" ] || ! (command ls -1qA "$NVM_DIRS_TO_SEARCH1" | nvm_grep -q .); then
NVM_DIRS_TO_SEARCH1='' NVM_DIRS_TO_SEARCH1=''
fi fi
if ! [ -d "$NVM_DIRS_TO_SEARCH2" ] || ! (command ls -1qA "$NVM_DIRS_TO_SEARCH2" | command grep -q .); then if ! [ -d "$NVM_DIRS_TO_SEARCH2" ] || ! (command ls -1qA "$NVM_DIRS_TO_SEARCH2" | nvm_grep -q .); then
NVM_DIRS_TO_SEARCH2="$NVM_DIRS_TO_SEARCH1" NVM_DIRS_TO_SEARCH2="$NVM_DIRS_TO_SEARCH1"
fi fi
if ! [ -d "$NVM_DIRS_TO_SEARCH3" ] || ! (command ls -1qA "$NVM_DIRS_TO_SEARCH3" | command grep -q .); then if ! [ -d "$NVM_DIRS_TO_SEARCH3" ] || ! (command ls -1qA "$NVM_DIRS_TO_SEARCH3" | nvm_grep -q .); then
NVM_DIRS_TO_SEARCH3="$NVM_DIRS_TO_SEARCH2" NVM_DIRS_TO_SEARCH3="$NVM_DIRS_TO_SEARCH2"
fi fi
@ -892,7 +896,7 @@ nvm_ls_remote_index_tab() {
fi fi
ZHS_HAS_SHWORDSPLIT_UNSET=1 ZHS_HAS_SHWORDSPLIT_UNSET=1
if nvm_has "setopt"; then if nvm_has "setopt"; then
ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; nvm_echo $?) ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | nvm_grep shwordsplit > /dev/null ; nvm_echo $?)
setopt shwordsplit setopt shwordsplit
fi fi
VERSIONS="$(nvm_download -L -s "$MIRROR/index.tab" -o - \ VERSIONS="$(nvm_download -L -s "$MIRROR/index.tab" -o - \
@ -900,7 +904,7 @@ nvm_ls_remote_index_tab() {
1d; 1d;
s/^/$PREFIX/; s/^/$PREFIX/;
s/[[:blank:]].*//" \ s/[[:blank:]].*//" \
| command grep -w "$PATTERN" \ | nvm_grep -w "$PATTERN" \
| $SORT_COMMAND)" | $SORT_COMMAND)"
if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && nvm_has "unsetopt"; then if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && nvm_has "unsetopt"; then
unsetopt shwordsplit unsetopt shwordsplit
@ -1038,7 +1042,7 @@ nvm_print_implicit_alias() {
ZHS_HAS_SHWORDSPLIT_UNSET=1 ZHS_HAS_SHWORDSPLIT_UNSET=1
if nvm_has "setopt"; then if nvm_has "setopt"; then
ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; nvm_echo $?) ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | nvm_grep shwordsplit > /dev/null ; nvm_echo $?)
setopt shwordsplit setopt shwordsplit
fi fi
@ -1047,7 +1051,7 @@ nvm_print_implicit_alias() {
NVM_IOJS_VERSION="$($NVM_COMMAND)" NVM_IOJS_VERSION="$($NVM_COMMAND)"
EXIT_CODE="$?" EXIT_CODE="$?"
if [ "_$EXIT_CODE" = "_0" ]; then if [ "_$EXIT_CODE" = "_0" ]; then
NVM_IOJS_VERSION="$(nvm_echo "$NVM_IOJS_VERSION" | command sed "s/^$NVM_IMPLICIT-//" | command grep -e '^v' | command cut -c2- | command cut -d . -f 1,2 | uniq | command tail -1)" NVM_IOJS_VERSION="$(nvm_echo "$NVM_IOJS_VERSION" | command sed "s/^$NVM_IMPLICIT-//" | nvm_grep -e '^v' | command cut -c2- | command cut -d . -f 1,2 | uniq | command tail -1)"
fi fi
if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && nvm_has "unsetopt"; then if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && nvm_has "unsetopt"; then
@ -1073,11 +1077,11 @@ nvm_print_implicit_alias() {
ZHS_HAS_SHWORDSPLIT_UNSET=1 ZHS_HAS_SHWORDSPLIT_UNSET=1
if nvm_has "setopt"; then if nvm_has "setopt"; then
ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; nvm_echo $?) ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | nvm_grep shwordsplit > /dev/null ; nvm_echo $?)
setopt shwordsplit setopt shwordsplit
fi fi
LAST_TWO=$($NVM_COMMAND | command grep -e '^v' | command cut -c2- | command cut -d . -f 1,2 | uniq) LAST_TWO=$($NVM_COMMAND | nvm_grep -e '^v' | command cut -c2- | command cut -d . -f 1,2 | uniq)
if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && nvm_has "unsetopt"; then if [ "$ZHS_HAS_SHWORDSPLIT_UNSET" -eq 1 ] && nvm_has "unsetopt"; then
unsetopt shwordsplit unsetopt shwordsplit
@ -1092,7 +1096,7 @@ nvm_print_implicit_alias() {
ZHS_HAS_SHWORDSPLIT_UNSET=1 ZHS_HAS_SHWORDSPLIT_UNSET=1
if nvm_has "setopt"; then if nvm_has "setopt"; then
ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; nvm_echo $?) ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | nvm_grep shwordsplit > /dev/null ; nvm_echo $?)
setopt shwordsplit setopt shwordsplit
fi fi
for MINOR in $LAST_TWO; do for MINOR in $LAST_TWO; do
@ -1182,7 +1186,7 @@ nvm_get_minor_version() {
PREFIXED_VERSION="$(nvm_format_version "$VERSION")" PREFIXED_VERSION="$(nvm_format_version "$VERSION")"
local MINOR local MINOR
MINOR="$(nvm_echo "$PREFIXED_VERSION" | command grep -e '^v' | command cut -c2- | command cut -d . -f 1,2)" MINOR="$(nvm_echo "$PREFIXED_VERSION" | nvm_grep -e '^v' | command cut -c2- | command cut -d . -f 1,2)"
if [ -z "$MINOR" ]; then if [ -z "$MINOR" ]; then
nvm_err 'invalid version number! (please report this)' nvm_err 'invalid version number! (please report this)'
return 3 return 3
@ -1254,7 +1258,7 @@ nvm_install_merged_node_binary() {
if [ -n "$NVM_OS" ]; then if [ -n "$NVM_OS" ]; then
t="$VERSION-$NVM_OS-$(nvm_get_arch)" t="$VERSION-$NVM_OS-$(nvm_get_arch)"
url="$MIRROR/$VERSION/$NODE_PREFIX-${t}.tar.${compression}" url="$MIRROR/$VERSION/$NODE_PREFIX-${t}.tar.${compression}"
sum="$(nvm_download -L -s "$MIRROR/$VERSION/SHASUMS256.txt" -o - | command grep "${NODE_PREFIX}-${t}.tar.${compression}" | command awk '{print $1}')" sum="$(nvm_download -L -s "$MIRROR/$VERSION/SHASUMS256.txt" -o - | nvm_grep "${NODE_PREFIX}-${t}.tar.${compression}" | command awk '{print $1}')"
local tmpdir local tmpdir
tmpdir="$NVM_DIR/bin/node-${t}" tmpdir="$NVM_DIR/bin/node-${t}"
local tmptarball local tmptarball
@ -1264,7 +1268,7 @@ nvm_install_merged_node_binary() {
nvm_echo "Downloading $url..." && \ nvm_echo "Downloading $url..." && \
nvm_download -L -C - --progress-bar "$url" -o "$tmptarball" || \ nvm_download -L -C - --progress-bar "$url" -o "$tmptarball" || \
NVM_INSTALL_ERRORED=true NVM_INSTALL_ERRORED=true
if grep '404 Not Found' "$tmptarball" >/dev/null; then if nvm_grep '404 Not Found' "$tmptarball" >/dev/null; then
NVM_INSTALL_ERRORED=true NVM_INSTALL_ERRORED=true
nvm_err "HTTP 404 at URL $url"; nvm_err "HTTP 404 at URL $url";
fi fi
@ -1328,7 +1332,7 @@ nvm_install_iojs_binary() {
if nvm_binary_available "$VERSION"; then if nvm_binary_available "$VERSION"; then
t="$VERSION-$NVM_OS-$(nvm_get_arch)" t="$VERSION-$NVM_OS-$(nvm_get_arch)"
url="$MIRROR/$VERSION/$(nvm_iojs_prefix)-${t}.tar.${compression}" url="$MIRROR/$VERSION/$(nvm_iojs_prefix)-${t}.tar.${compression}"
sum="$(nvm_download -L -s "$MIRROR/$VERSION/SHASUMS256.txt" -o - | command grep "$(nvm_iojs_prefix)-${t}.tar.${compression}" | command awk '{print $1}')" sum="$(nvm_download -L -s "$MIRROR/$VERSION/SHASUMS256.txt" -o - | nvm_grep "$(nvm_iojs_prefix)-${t}.tar.${compression}" | command awk '{print $1}')"
local tmpdir local tmpdir
tmpdir="$NVM_DIR/bin/iojs-${t}" tmpdir="$NVM_DIR/bin/iojs-${t}"
local tmptarball local tmptarball
@ -1338,7 +1342,7 @@ nvm_install_iojs_binary() {
nvm_echo "Downloading $url..." && \ nvm_echo "Downloading $url..." && \
nvm_download -L -C - --progress-bar "$url" -o "$tmptarball" || \ nvm_download -L -C - --progress-bar "$url" -o "$tmptarball" || \
NVM_INSTALL_ERRORED=true NVM_INSTALL_ERRORED=true
if grep '404 Not Found' "$tmptarball" >/dev/null; then if nvm_grep '404 Not Found' "$tmptarball" >/dev/null; then
NVM_INSTALL_ERRORED=true NVM_INSTALL_ERRORED=true
nvm_err "HTTP 404 at URL $url"; nvm_err "HTTP 404 at URL $url";
fi fi
@ -1389,7 +1393,7 @@ nvm_install_node_binary() {
fi fi
t="$VERSION-$NVM_OS-$NVM_ARCH" t="$VERSION-$NVM_OS-$NVM_ARCH"
url="$NVM_NODEJS_ORG_MIRROR/$VERSION/node-${t}.tar.gz" url="$NVM_NODEJS_ORG_MIRROR/$VERSION/node-${t}.tar.gz"
sum=$(nvm_download -L -s "$NVM_NODEJS_ORG_MIRROR/$VERSION/SHASUMS.txt" -o - | command grep "node-${t}.tar.gz" | command awk '{print $1}') sum=$(nvm_download -L -s "$NVM_NODEJS_ORG_MIRROR/$VERSION/SHASUMS.txt" -o - | nvm_grep "node-${t}.tar.gz" | command awk '{print $1}')
local tmpdir local tmpdir
tmpdir="$NVM_DIR/bin/node-${t}" tmpdir="$NVM_DIR/bin/node-${t}"
local tmptarball local tmptarball
@ -1398,7 +1402,7 @@ nvm_install_node_binary() {
command mkdir -p "$tmpdir" && \ command mkdir -p "$tmpdir" && \
nvm_download -L -C - --progress-bar "$url" -o "$tmptarball" || \ nvm_download -L -C - --progress-bar "$url" -o "$tmptarball" || \
NVM_INSTALL_ERRORED=true NVM_INSTALL_ERRORED=true
if grep '404 Not Found' "$tmptarball" >/dev/null; then if nvm_grep '404 Not Found' "$tmptarball" >/dev/null; then
NVM_INSTALL_ERRORED=true NVM_INSTALL_ERRORED=true
nvm_err "HTTP 404 at URL $url"; nvm_err "HTTP 404 at URL $url";
fi fi
@ -1434,7 +1438,7 @@ nvm_get_make_jobs() {
NVM_OS="$(nvm_get_os)" NVM_OS="$(nvm_get_os)"
local NVM_CPU_THREADS local NVM_CPU_THREADS
if [ "_$NVM_OS" = "_linux" ]; then if [ "_$NVM_OS" = "_linux" ]; then
NVM_CPU_THREADS="$(command grep -c -E '^processor.+: [0-9]+' /proc/cpuinfo)" NVM_CPU_THREADS="$(nvm_grep -c -E '^processor.+: [0-9]+' /proc/cpuinfo)"
elif [ "_$NVM_OS" = "_freebsd" ] || [ "_$NVM_OS" = "_darwin" ]; then elif [ "_$NVM_OS" = "_freebsd" ] || [ "_$NVM_OS" = "_darwin" ]; then
NVM_CPU_THREADS="$(sysctl -n hw.ncpu)" NVM_CPU_THREADS="$(sysctl -n hw.ncpu)"
elif [ "_$NVM_OS" = "_sunos" ]; then elif [ "_$NVM_OS" = "_sunos" ]; then
@ -1495,10 +1499,10 @@ nvm_install_node_source() {
local tmptarball local tmptarball
tmptarball="$tmpdir/node-$VERSION.tar.gz" tmptarball="$tmpdir/node-$VERSION.tar.gz"
if [ "$(nvm_download -L -s -I "$NVM_NODEJS_ORG_MIRROR/$VERSION/node-$VERSION.tar.gz" -o - 2>&1 | command grep '200 OK')" != '' ]; then if [ "$(nvm_download -L -s -I "$NVM_NODEJS_ORG_MIRROR/$VERSION/node-$VERSION.tar.gz" -o - 2>&1 | nvm_grep '200 OK')" != '' ]; then
tarball="$NVM_NODEJS_ORG_MIRROR/$VERSION/node-$VERSION.tar.gz" tarball="$NVM_NODEJS_ORG_MIRROR/$VERSION/node-$VERSION.tar.gz"
sum=$(nvm_download -L -s "$NVM_NODEJS_ORG_MIRROR/$VERSION/SHASUMS.txt" -o - | command grep "node-${VERSION}.tar.gz" | command awk '{print $1}') sum=$(nvm_download -L -s "$NVM_NODEJS_ORG_MIRROR/$VERSION/SHASUMS.txt" -o - | nvm_grep "node-${VERSION}.tar.gz" | command awk '{print $1}')
elif [ "$(nvm_download -L -s -I "$NVM_NODEJS_ORG_MIRROR/node-$VERSION.tar.gz" -o - | command grep '200 OK')" != '' ]; then elif [ "$(nvm_download -L -s -I "$NVM_NODEJS_ORG_MIRROR/node-$VERSION.tar.gz" -o - | nvm_grep '200 OK')" != '' ]; then
tarball="$NVM_NODEJS_ORG_MIRROR/node-$VERSION.tar.gz" tarball="$NVM_NODEJS_ORG_MIRROR/node-$VERSION.tar.gz"
fi fi
@ -1726,9 +1730,6 @@ nvm() {
return return
fi fi
local GREP_OPTIONS
GREP_OPTIONS=''
# initialize local variables # initialize local variables
local VERSION local VERSION
local ADDITIONAL_PARAMETERS local ADDITIONAL_PARAMETERS
@ -1787,7 +1788,7 @@ nvm() {
local ZHS_HAS_SHWORDSPLIT_UNSET local ZHS_HAS_SHWORDSPLIT_UNSET
ZHS_HAS_SHWORDSPLIT_UNSET=1 ZHS_HAS_SHWORDSPLIT_UNSET=1
if nvm_has "setopt"; then if nvm_has "setopt"; then
ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; nvm_echo $?) ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | nvm_grep shwordsplit > /dev/null ; nvm_echo $?)
setopt shwordsplit setopt shwordsplit
fi fi
nvm_err "nvm --version: v$(nvm --version)" nvm_err "nvm --version: v$(nvm --version)"
@ -2029,7 +2030,7 @@ nvm() {
nvm_echo "$NVM_SUCCESS_MSG" nvm_echo "$NVM_SUCCESS_MSG"
# rm any aliases that point to uninstalled version. # rm any aliases that point to uninstalled version.
for ALIAS in $(command grep -l "$VERSION" "$(nvm_alias_path)/*" 2>/dev/null) for ALIAS in $(nvm_grep -l "$VERSION" "$(nvm_alias_path)/*" 2>/dev/null)
do do
nvm unalias "$(command basename "$ALIAS")" nvm unalias "$(command basename "$ALIAS")"
done done
@ -2249,7 +2250,7 @@ nvm() {
local ZHS_HAS_SHWORDSPLIT_UNSET local ZHS_HAS_SHWORDSPLIT_UNSET
ZHS_HAS_SHWORDSPLIT_UNSET=1 ZHS_HAS_SHWORDSPLIT_UNSET=1
if nvm_has "setopt"; then if nvm_has "setopt"; then
ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | command grep shwordsplit > /dev/null ; nvm_echo $?) ZHS_HAS_SHWORDSPLIT_UNSET=$(setopt | nvm_grep shwordsplit > /dev/null ; nvm_echo $?)
setopt shwordsplit setopt shwordsplit
fi fi
if [ "_$VERSION" = "_N/A" ]; then if [ "_$VERSION" = "_N/A" ]; then
@ -2373,7 +2374,7 @@ nvm() {
local NVM_OUTPUT local NVM_OUTPUT
NVM_OUTPUT="$(nvm_echo "$NVM_LS_REMOTE_PRE_MERGED_OUTPUT NVM_OUTPUT="$(nvm_echo "$NVM_LS_REMOTE_PRE_MERGED_OUTPUT
$NVM_LS_REMOTE_IOJS_OUTPUT $NVM_LS_REMOTE_IOJS_OUTPUT
$NVM_LS_REMOTE_POST_MERGED_OUTPUT" | command grep -v "N/A" | command sed '/^$/d')" $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | nvm_grep -v "N/A" | command sed '/^$/d')"
if [ -n "$NVM_OUTPUT" ]; then if [ -n "$NVM_OUTPUT" ]; then
nvm_print_versions "$NVM_OUTPUT" nvm_print_versions "$NVM_OUTPUT"
return $NVM_LS_REMOTE_EXIT_CODE || $NVM_LS_REMOTE_IOJS_EXIT_CODE return $NVM_LS_REMOTE_EXIT_CODE || $NVM_LS_REMOTE_IOJS_EXIT_CODE
@ -2568,6 +2569,7 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | command grep -v "N/A" | command sed '/^$/d'
nvm_has_system_node nvm_has_system_iojs \ nvm_has_system_node nvm_has_system_iojs \
nvm_download nvm_get_latest nvm_has nvm_get_latest \ nvm_download nvm_get_latest nvm_has nvm_get_latest \
nvm_supports_source_options nvm_auto nvm_supports_xz \ nvm_supports_source_options nvm_auto nvm_supports_xz \
nvm_echo nvm_err nvm_grep \
nvm_has_colors nvm_process_parameters > /dev/null 2>&1 nvm_has_colors nvm_process_parameters > /dev/null 2>&1
unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1 unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1
;; ;;