HEX
Server: Apache/2.2.34 (Unix) mod_fastcgi/mod_fastcgi-SNAP-0910052141
System: Linux Kou-Etsu-Dou 4.4.59+ #25556 SMP PREEMPT Thu Mar 4 18:03:46 CST 2021 x86_64
User: hosam (1026)
PHP: 7.2.29
Disabled: NONE
Upload Files
File: //usr/local/bin/nvm
#!/usr/bin/bash
# Node.js version manager, based on https://github.com/tj/n
# Released under MIT License.
# Copyright (C) 2017 Synology inc. <support@synology.com>
# Copyright (C) 2016 TJ Holowaychuk <tj@vision-media.ca>

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

#
# Setup.
#

VERSION="15"
UP=$'\033[A'
DOWN=$'\033[B'
N_PREFIX=${N_PREFIX-/usr/local}
BASE_VERSIONS_DIR=/usr/local/node/nvm/versions
NODEJS_ETC_DIR=/usr/syno/etc/packages/Node.js
PREFERENCE_CONF=$NODEJS_ETC_DIR/.version.pref
STABLE_VERSION="12.22.12"
VERSIONS_DIR=($BASE_VERSIONS_DIR)

#
# Log <type> <msg>
#

log() {
  printf "  \033[36m%10s\033[0m : \033[90m%s\033[0m\n" $1 $2
}

#
# Exit with the given <msg ...>
#

abort() {
  printf "\n  \033[31mError: $@\033[0m\n\n" && exit 1
}

#
# State
#

DEFAULT=0

for dir in ${VERSIONS_DIR[@]}; do
  test -d $dir || mkdir -p $dir
done


#
# Functions used when showing versions installed
#

enter_fullscreen() {
  printf "\033[?1049h"
  stty -echo
}

leave_fullscreen() {
  printf "\033[?1049l"
  stty echo
}

handle_sigint() {
  leave_fullscreen
  exit $?
}

handle_sigtstp() {
  leave_fullscreen
  kill -s SIGSTOP $$
}

#
# Output usage information.
#

display_help() {
  cat <<-EOF

  Node.js version management

  Usage: nvm [options] [COMMAND] [args]

  Commands:

    nvm                              Output installed version of Node.js
    nvm latest                       Output the latest Node.js version available
    nvm stable                       Output the latest stable Node.js version available
    nvm use <version> [args ...]     Execute Node.js <version> with [args ...]
    nvm bin <version>                Output bin path for <version>
    nvm rm <version ...>             Remove the given version(s)
    nvm ls                           Output the versions of node available
    nvm set                          Set current Node.js version

  Options:

    -V, --version   Output current version of Node.js version management
    -h, --help      Display help information
    -q, --quiet     Disable curl output (if available)
    -d, --download  Download only

  Aliases:

    which   bin
    use     as
    list    ls
    -       rm

EOF
}

err_no_installed_print_help() {
  printf "\n  \033[31mError: no installed version\033[0m\n"
  display_help
  exit 1
}

#
# Hide cursor.
#

hide_cursor() {
  printf "\e[?25l"
}

#
# Show cursor.
#

show_cursor() {
  printf "\e[?25h"
}

#
# Output version after selected.
#

next_version_installed() {
  list_versions_installed | grep $selected -A 1 | tail -n 1
}

#
# Output version before selected.
#

prev_version_installed() {
  list_versions_installed | grep $selected -B 1 | head -n 1
}

#
# Output n version.
#

display_n_version() {
  echo $VERSION && exit 0
}

#
# Check for installed version, and populate $active
#

check_current_version() {
  command -v node &> /dev/null
  if test $? -eq 0; then
    local current=$(node --version)
    if [ -n "$PROJECT_VERSION_CHECK" ]; then
      current=$(node -p "$PROJECT_VERSION_CHECK || process.exit(1)" || node --version)
    fi
    current=${current#v}
    if diff &> /dev/null \
      $BASE_VERSIONS_DIR/$current/bin/node \
      $(which node) ; then
      active=$current
    fi
  fi
}

#
# Display sorted versions directories paths.
#

versions_paths() {
  find $BASE_VERSIONS_DIR -maxdepth 2 -type d \
    | sed 's|'$BASE_VERSIONS_DIR'/||g' \
    | egrep "[0-9]+\.[0-9]+\.[0-9]+$" \
    | sort -k 1 -k 2,2n -k 3,3n -t .
}

#
# Display installed versions with <selected>
#

display_versions_with_selected() {
  selected=$1
  echo
  for version in $(versions_paths); do
    if test "$version" = "$selected"; then
      printf "  \033[36mο\033[0m $version\033[0m\n"
    else
      printf "    \033[90m$version\033[0m\n"
    fi
  done
  echo
}

#
# List installed versions.
#

list_versions_installed() {
  for version in $(versions_paths); do
    echo $version
  done
}

#
# Display current node --version and others installed.
#

display_versions() {
  enter_fullscreen
  check_current_version
  clear
  display_versions_with_selected $active

  trap handle_sigint INT
  trap handle_sigtstp SIGTSTP

  while true; do
    read -n 3 c
    case "$c" in
      $UP)
        clear
        display_versions_with_selected $(prev_version_installed)
        ;;
      $DOWN)
        clear
        display_versions_with_selected $(next_version_installed)
        ;;
      *)
        activate $selected
        leave_fullscreen
        exit
        ;;
    esac
  done
}

#
# Activate <version>
#

activate() {
  local version=$1
  check_current_version
  if test "$version" != "$active"; then
    local dir=$BASE_VERSIONS_DIR/$version
    for subdir in bin lib include share; do
      if test -L "$N_PREFIX/$subdir"; then
        find "$dir/$subdir" -mindepth 1 -maxdepth 1 -exec cp -fR "{}" "$N_PREFIX/$subdir" \;
      else
        cp -fR "$dir/$subdir" $N_PREFIX
      fi
    done
  fi
}

#
# Output bin path for <version>
#

display_bin_path_for_version() {
  test -z $1 && abort "version required"
  local version=${1#v}
  local bin=${VERSIONS_DIR[$DEFAULT]}/$version/bin/node
  if test -f $bin; then
    bin=$(realpath $bin)
    printf "$bin \n"
  else
    abort "$1 is not installed"
  fi
}

#
# Execute the given <version> of node with [args ...]
#

execute_with_version() {
  test -z $1 && abort "version required"
  local version=${1#v}

  if [ "$version" = "latest" ]; then
    version=$(display_latest_version)
  fi

  if [ "$version" = "stable" ]; then
    version=$(display_latest_stable_version)
  fi

  local bin=${VERSIONS_DIR[$DEFAULT]}/$version/bin/node

  shift # remove version

  if test -f $bin; then
    $bin "$@"
  else
    abort "$version is not installed"
  fi
}

#
# Display the latest release version.
#

display_latest_version() {
   list_versions_installed \
    | sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
    | tail -n1
}

#
# Display the latest stable release version.
#

display_latest_stable_version() {
  echo $STABLE_VERSION
}

#
# Set installed version.
#

set_version() { # $1: version
  if [ -n "$(list_versions_installed | grep $1)" ]; then
    activate $1
    return 0
  else
    echo "Node.js $1 is not installed."
    return 1
  fi
}

#
# Set preference version
#

set_preference() { # $1: version
  [ ! -d "$NODEJS_ETC_DIR" ] && mkdir -p $NODEJS_ETC_DIR
  echo "$1" > $PREFERENCE_CONF
}

#
# Handle arguments.
#

if test $# -eq 0; then
  test -z "$(versions_paths)" && err_no_installed_print_help
  display_versions
else
  while test $# -ne 0; do
    case $1 in
      -V|--version) display_n_version ;;
      -h|--help|help) display_help; exit ;;
      bin|which) display_bin_path_for_version $2; exit ;;
      as|use) shift; execute_with_version $@; exit ;;
      latest) display_latest_version; exit ;;
      stable) display_latest_stable_version; exit ;;
      ls|list) list_versions_installed; exit ;;
      set) set_version $2; exit $? ;;
      *) display_help;;
    esac
    shift
  done
fi