Skip to content

Commit

Permalink
Merge pull request #22 from nodenv/script-dir-symlinks
Browse files Browse the repository at this point in the history
Resolves symlinks to get script dir
  • Loading branch information
jasonkarns authored Jan 17, 2017
2 parents 66026d0 + e8ac198 commit e1ad593
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions bin/nodenv-package-json-engine
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,31 @@
# test the installed versions against that range and print the
# greatest matching version.

# Setup for relative includes

# Setup for relative includes. Adapted from:
# http://www.ostricher.com/2014/10/the-right-way-to-get-the-directory-of-a-bash-script/
# http://stackoverflow.com/a/12694189/407845
DIR="${BASH_SOURCE%/*}"
if [[ ! -d "$DIR" ]]; then DIR="$PWD"; fi
get_script_dir() {
local dir
local src="${BASH_SOURCE[0]}"

SEMVER="$DIR/../node_modules/sh-semver/semver.sh"
# while $src is a symlink, resolve it
while [ -h "$src" ]; do
dir="${src%/*}"
src="$( readlink "$src" )"

# If $src was a relative symlink (so no "/" as prefix),
# need to resolve it relative to the symlink base directory
[[ $src != /* ]] && src="$dir/$src"
done
dir="${src%/*}"

if [ -d "$dir" ]; then
echo "$dir"
else
echo "$PWD"
fi
}

find_package_json_path() {
local root="$1"
Expand Down Expand Up @@ -50,6 +69,9 @@ version_from_package_json() {
fi
}

DIR="$(get_script_dir)"
SEMVER="$DIR/../node_modules/sh-semver/semver.sh"

if should_find_in_package_json; then
PACKAGE_JSON_PATH=$(find_package_json_path "$PWD")

Expand Down

0 comments on commit e1ad593

Please sign in to comment.