ronantakizawa/codereview-bench huggingface dataset:
Repository: nuxt/nuxt
File: packages/nuxt/src/core/builder.ts (TypeScript)

Code before:
  update: 'change',
}

async function watch (nuxt: Nuxt) {
  if (nuxt.options.experimental.watcher === 'parcel') {
    const success = await createParcelWatcher()
    if (success) { return }
  }

  if (nuxt.options.experimental.watcher === 'chokidar') {
    return createWatcher()
  }

  return createGranularWatcher()
}

function createWatcher () {
  const nuxt = useNuxt()
  const isIgnored = createIsIgnored(nuxt)

  const layerDirs = getLayerDirectories(nuxt)
  const paths: string[] = []
  for (const layer of layerDirs) {
    paths.push(layer.app)
    // Only add server if it's not inside app (avoid double-watching)
    if (!layer.server.includes(layer.app.replace(/\/?$/, '/'))) {
      paths.push(layer.server)
    }
  }

  const watcher = chokidarWatch(paths, {
    ...nuxt.options.watchers.chokidar,
    ignoreInitial: true,
    ignored: [isIgnored, /[\\/]node_modules[\\/]/],
  })

  const restartPaths = new Set<string>()
  const srcDir = nuxt.options.srcDir.replace(/\/?$/, '/')
  for (const pattern of nuxt.options.watch) {
    if (typeof pattern !== 'string') { continue }
    const path = resolve(nuxt.options.srcDir, pattern)
    if (!path.startsWith(srcDir)) {
      restartPaths.add(path)
    }
  }

  watcher.add([...restartPaths])

  watcher.on('all', (event, path) => {
    if (event === 'all' || event === 'ready' || event === 'error' || event === 'raw') {
      return

Review comment (suggestion):
```suggestion
    if (!layer.server.startsWith(layer.app.replace(/\/?$/, '/'))) {
```

Code after:
  update: 'change',
}

async function watch (nuxt: Nuxt) {
  if (nuxt.options.experimental.watcher === 'parcel') {
    const success = await createParcelWatcher()
    if (success) { return }
  }

  if (nuxt.options.experimental.watcher === 'chokidar') {
    return createWatcher()
  }

  return createGranularWatcher()
}

function createWatcher () {
  const nuxt = useNuxt()
  const isIgnored = createIsIgnored(nuxt)

  const layerDirs = getLayerDirectories(nuxt)
  const paths: string[] = []
  for (const layer of layerDirs) {
    paths.push(layer.app)
    // Only add server if it's not inside app (avoid double-watching)
    if (!layer.server.startsWith(layer.app.replace(/\/?$/, '/'))) {
      paths.push(layer.server)
    }
  }

  const watcher = chokidarWatch(paths, {
    ...nuxt.options.watchers.chokidar,
    ignoreInitial: true,
    ignored: [isIgnored, /[\\/]node_modules[\\/]/],
  })

  const restartPaths = new Set<string>()
  const srcDir = nuxt.options.srcDir.replace(/\/?$/, '/')
  for (const pattern of nuxt.options.watch) {
    if (typeof pattern !== 'string') { continue }
    const path = resolve(nuxt.options.srcDir, pattern)
    if (!path.startsWith(srcDir)) {
      restartPaths.add(path)
    }
  }

  watcher.add([...restartPaths])

  watcher.on('all', (event, path) => {
    if (event === 'all' || event === 'ready' || event === 'error' || event === 'raw') {
      return
Repository: nvm-sh/nvm
File: install.sh (Shell)

Code before:
    if nvm_has "git"; then
      install_nvm_from_git
    elif nvm_has "nvm_download"; then
      install_nvm_as_script
    else
      echo >&2 "You need git, curl, or wget to install nvm"
      exit 1
    fi
  elif [ "~$METHOD" = "~git" ]; then
    if ! nvm_has "git"; then
      echo >&2 "You need git to install nvm"
      exit 1
    fi
    install_nvm_from_git
  elif [ "~$METHOD" = "~script" ]; then
    if ! nvm_has "nvm_download"; then
      echo >&2 "You need curl or wget to install nvm"
      exit 1
    fi
    install_nvm_as_script
  fi

  echo

  NVM_PROFILE=$(nvm_detect_profile)

  SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\"  # This loads nvm"

  if [ -z "$NVM_PROFILE" ] ; then
    echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
    echo "=> Create one of them and run this script again"
    echo "=> Create it (touch $NVM_PROFILE) and run this script again"
    echo "   OR"
    echo "=> Append the following lines to the correct file yourself:"
    printf "$SOURCE_STR"
    echo
  else
    if ! grep -qc 'nvm.sh' "$NVM_PROFILE"; then
      echo "=> Appending source string to $NVM_PROFILE"
      printf "$SOURCE_STR\n" >> "$NVM_PROFILE"
    else
      echo "=> Source string already in $NVM_PROFILE"
    fi
  fi

  echo "=> Close and reopen your terminal to start using nvm"
  nvm_reset
}

#
# Unsets the various functions defined

Review comment (suggestion):
if you do `local NVM_PROFILE` on the line above it, then it won't leave behind an env var in most shells


Code after:
    if nvm_has "git"; then
      install_nvm_from_git
    elif nvm_has "nvm_download"; then
      install_nvm_as_script
    else
      echo >&2 "You need git, curl, or wget to install nvm"
      exit 1
    fi
  elif [ "~$METHOD" = "~git" ]; then
    if ! nvm_has "git"; then
      echo >&2 "You need git to install nvm"
      exit 1
    fi
    install_nvm_from_git
  elif [ "~$METHOD" = "~script" ]; then
    if ! nvm_has "nvm_download"; then
      echo >&2 "You need curl or wget to install nvm"
      exit 1
    fi
    install_nvm_as_script
  fi

  echo

  local NVM_PROFILE
  NVM_PROFILE=$(nvm_detect_profile)

  SOURCE_STR="\nexport NVM_DIR=\"$NVM_DIR\"\n[ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\"  # This loads nvm"

  if [ -z "$NVM_PROFILE" ] ; then
    echo "=> Profile not found. Tried $NVM_PROFILE (as defined in \$PROFILE), ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile."
    echo "=> Create one of them and run this script again"
    echo "=> Create it (touch $NVM_PROFILE) and run this script again"
    echo "   OR"
    echo "=> Append the following lines to the correct file yourself:"
    printf "$SOURCE_STR"
    echo
  else
    if ! grep -qc 'nvm.sh' "$NVM_PROFILE"; then
      echo "=> Appending source string to $NVM_PROFILE"
      printf "$SOURCE_STR\n" >> "$NVM_PROFILE"
    else
      echo "=> Source string already in $NVM_PROFILE"
    fi
  fi

  echo "=> Close and reopen your terminal to start using nvm"
  nvm_reset
}

#
Repository: nvm-sh/nvm
File: install.sh (Shell)

Code before:
#!/bin/bash

set -e

nvm_has() {
  type "$1" > /dev/null 2>&1
  return $?
}

nvm_install_dir() {
  echo ${NVM_DIR:-"$HOME/.nvm"}
}

nvm_download() {
  if nvm_has "curl"; then
    curl $*
  elif nvm_has "wget"; then
    # Emulate curl with wget
    ARGS=$(echo "$*" | sed -e 's/--progress-bar /--progress=bar /' \
                           -e 's/-L //' \
                           -e 's/-I /--server-response /' \
                           -e 's/-s /-q /' \
                           -e 's/-o /-O /' \
                           -e 's/-C - /-c /')
    wget $ARGS
  fi
}

install_nvm_from_git() {
  local install_dir
  install_dir=$(nvm_install_dir)
  if [ -z "$NVM_SOURCE" ]; then
    NVM_SOURCE="https://github.com/creationix/nvm.git"
  fi


Review comment (suggestion):
This syntax hasn't been previously used in `nvm` - can you confirm that it's portable, and will work in all supported shells? Brief experimentation suggests that it will, but there must be a reason it's never been used previously if so.


Code after:
#!/usr/bin/env bash

{ # this ensures the entire script is downloaded #

nvm_has() {
  type "$1" > /dev/null 2>&1
}

nvm_install_dir() {
  echo ${NVM_DIR:-"$HOME/.nvm"}
}

nvm_latest_version() {
  echo "v0.31.2"
}

#
# Outputs the location to NVM depending on:
# * The availability of $NVM_SOURCE
# * The method used ("script" or "git" in the script, defaults to "git")
# NVM_SOURCE always takes precedence unless the method is "script-nvm-exec"
#
nvm_source() {
  local NVM_METHOD
  NVM_METHOD="$1"
  local NVM_SOURCE_URL
  NVM_SOURCE_URL="$NVM_SOURCE"
  if [ "_$NVM_METHOD" = "_script-nvm-exec" ]; then
    NVM_SOURCE_URL="https://raw.githubusercontent.com/creationix/nvm/$(nvm_latest_version)/nvm-exec"
  elif [ -z "$NVM_SOURCE_URL" ]; then
    if [ "_$NVM_METHOD" = "_script" ]; then
      NVM_SOURCE_URL="https://raw.githubusercontent.com/creationix/nvm/$(nvm_latest_version)/nvm.sh"
    elif [ "_$NVM_METHOD" = "_git" ] || [ -z "$NVM_METHOD" ]; then
      NVM_SOURCE_URL="https://github.com/creationix/nvm.git"
    else
Repository: nvm-sh/nvm
File: install.sh (Shell)

Code before:
#
# Check whether the user has any globally-installed npm modules in their system
# Node, and warn them if so.
#
nvm_check_global_modules() {
  local NPM_GLOBAL_MODULES
  NPM_GLOBAL_MODULES=$(npm list -g --depth=0 | sed '/ npm@/d')
  
  local MODULE_COUNT
  MODULE_COUNT=$(
    printf %s\\n "$NPM_GLOBAL_MODULES" |
    sed -ne '1!p' |                             # Remove the first line
    wc -l | tr -d ' '                           # Count entries
  )

  if [ $MODULE_COUNT -ne 0 ]; then
    cat <<-'END_MESSAGE'
	=> You currently have modules installed globally with `npm`. These will no
	=> longer be linked to the active version of Node when you install a new node
	=> with `nvm`; and they may (depending on how you construct your `$PATH`)
	=> override the binaries of modules installed with `nvm`:

	END_MESSAGE
    printf %s\\n "$NPM_GLOBAL_MODULES"
    cat <<-'END_MESSAGE'

	=> If you wish to uninstall them at a later point (or re-install them under your
	=> `nvm` Nodes), you can remove them from the system Node as follows:

	     $ nvm use system
	     $ npm uninstall -g a_module

	END_MESSAGE
  fi
}

nvm_do_install() {
  if [ -z "$METHOD" ]; then
    # Autodetect install method
    if nvm_has "git"; then
      install_nvm_from_git
    elif nvm_has "nvm_download"; then
      install_nvm_as_script
    else
      echo >&2 "You need git, curl, or wget to install nvm"
      exit 1
    fi
  elif [ "~$METHOD" = "~git" ]; then
    if ! nvm_has "git"; then
      echo >&2 "You need git to install nvm"
      exit 1

Review comment (suggestion):
Please ensure that any `nvm_*` functions defined are also added to the `unset -f` lists in this file, and/or in `nvm.sh`, whichever is appropriate.


Code after:

#
# Detect profile file if not specified as environment variable
# (eg: PROFILE=~/.myprofile)
# The echo'ed path is guaranteed to be an existing file
# Otherwise, an empty string is returned
#
nvm_detect_profile() {
  if [ -f "$PROFILE" ]; then
    echo "$PROFILE"
  elif [ -f "$HOME/.bashrc" ]; then
    echo "$HOME/.bashrc"
  elif [ -f "$HOME/.bash_profile" ]; then
    echo "$HOME/.bash_profile"
  elif [ -f "$HOME/.zshrc" ]; then
    echo "$HOME/.zshrc"
  elif [ -f "$HOME/.profile" ]; then
    echo "$HOME/.profile"
  fi
}

#
# Check whether the user has any globally-installed npm modules in their system
# Node, and warn them if so.
#
nvm_check_global_modules() {
  command -v npm >/dev/null 2>&1 || return 0

  local NPM_VERSION
  NPM_VERSION="$(npm --version)"
  NPM_VERSION="${NPM_VERSION:--1}"
  [ "$NVM_DEBUG" = 0 ] && debug "NPM detected (at version ${NPM_VERSION}.)"
  [ "${NPM_VERSION%%[!-0-9]*}" -gt 0 ] || return 0

  local NPM_GLOBAL_MODULES
  NPM_GLOBAL_MODULES="$(
    npm list -g --depth=0 |
    sed '/ npm@/d' |
    sed '/ (empty)$/d'
  )"

  local MODULE_COUNT
  MODULE_COUNT="$(
    printf %s\\n "$NPM_GLOBAL_MODULES" |
    sed -ne '1!p' |                             # Remove the first line
    wc -l | tr -d ' '                           # Count entries
  )"
  [ "$NVM_DEBUG" = 0 ] && {
    debug "(${MODULE_COUNT}) global modules detected:"
    error "$NPM_GLOBAL_MODULES" ;}
Repository: nvm-sh/nvm
File: nvm.sh (Shell)

Code before:
        if [ -z "$NVM_RC_VERSION" ]; then
          >&2 nvm help
          return 127
        fi
      fi

      shift

      nobinary=0
      if [ "_$1" = "_-s" ]; then
        nobinary=1
        shift
        if [ "_$1" = "_-j" ]; then
          shift
          NVM_CPU_THREADS=$1
          shift
        else
          unset NVM_CPU_THREADS
        fi
      fi

      provided_version="$1"

      if [ -z "$provided_version" ]; then
        if [ $version_not_provided -ne 1 ]; then
          nvm_rc_version
        fi
        provided_version="$NVM_RC_VERSION"
      else
        shift
      fi

      VERSION="$(nvm_remote_version "$provided_version")"

      if [ "_$VERSION" = "_N/A" ]; then
        echo "Version '$provided_version' not found - try \`nvm ls-remote\` to browse available versions." >&2
        return 3
      fi

      ADDITIONAL_PARAMETERS=''
      local PROVIDED_REINSTALL_PACKAGES_FROM
      local REINSTALL_PACKAGES_FROM

      while [ $# -ne 0 ]
      do
        case "$1" in
          --reinstall-packages-from=*)
            PROVIDED_REINSTALL_PACKAGES_FROM="$(echo "$1" | command cut -c 27-)"
            REINSTALL_PACKAGES_FROM="$(nvm_version "$PROVIDED_REINSTALL_PACKAGES_FROM")"
          ;;
          --copy-packages-from=*)

Review comment (suggestion):
`-j -s` should probably error out, as should `-j $not_a_number` - i think it should be checked along with `-s`, and ideally in any order.


Code after:
      version_not_provided=0
      local provided_version
      local NVM_OS
      NVM_OS="$(nvm_get_os)"

      if ! nvm_has "curl" && ! nvm_has "wget"; then
        echo 'nvm needs curl or wget to proceed.' >&2;
        return 1
      fi

      if [ $# -lt 2 ]; then
        version_not_provided=1
        nvm_rc_version
        if [ -z "$NVM_RC_VERSION" ]; then
          >&2 nvm help
          return 127
        fi
      fi

      shift

      nobinary=0
      if [ "_$1" = "_-s" ]; then
        nobinary=1
        shift
        if [ "_$1" = "_-j" ]; then
          shift
          local NVM_CPU_THREAD_VALID
          NVM_CPU_THREAD_VALID=$(nvm_is_natural_num $1)
          if [ "$NVM_CPU_THREAD_VALID" = "true" ]; then
            NVM_MAKE_JOBS=$1
            echo "Set number of jobs to $MAKE_JOBS for 'make' utility"
          else
            unset NVM_MAKE_JOBS
            echo >&2 "$1 is invalid for CPU threads, should be a natural number"
          fi
          shift
        else
          unset NVM_MAKE_JOBS
        fi
      fi

      provided_version="$1"

      if [ -z "$provided_version" ]; then
        if [ $version_not_provided -ne 1 ]; then
          nvm_rc_version
        fi
        provided_version="$NVM_RC_VERSION"
      else
        shift
Repository: nvm-sh/nvm
File: nvm.sh (Shell)

Code before:

nvm_get_os() {
  local NVM_UNAME
  NVM_UNAME="$(uname -a)"
  local NVM_OS
  case "$NVM_UNAME" in
    Linux\ *) NVM_OS=linux ;;
    Darwin\ *) NVM_OS=darwin ;;
    SunOS\ *) NVM_OS=sunos ;;
    FreeBSD\ *) NVM_OS=freebsd ;;
  esac
  echo "$NVM_OS"
}

nvm_get_arch() {
  local HOST_ARCH
  local NVM_OS

  NVM_OS="$(nvm_get_os)"
  # If the OS is SunOS, first try to use pkgsrc to guess
  # the most appropriate arch. If it's not available, use
  # isainfo to get the instruction set supported by the
  # kernel.
  if [ "_$NVM_OS" = "_sunos" ]; then
    HOST_ARCH=$(pkg_info -Q MACHINE_ARCH pkg_install)
    local EXIT_CODE
    EXIT_CODE="$?"
    if [ "_$EXIT_CODE" != "_0" ]; then
      HOST_ARCH=$(isainfo -n)
    fi
  else
     HOST_ARCH="$(uname -m)"
  fi

  local NVM_ARCH
  case "$HOST_ARCH" in
    x86_64 | amd64) NVM_ARCH="x64" ;;
    i*86) NVM_ARCH="x86" ;;
    *) NVM_ARCH="$HOST_ARCH" ;;
  esac
  echo "$NVM_ARCH"
}

nvm_ensure_default_set() {
  local VERSION
  VERSION="$1"
  if [ -z "$VERSION" ]; then
    echo 'nvm_ensure_default_set: a version is required' >&2
    return 1
  fi
  if nvm_alias default >/dev/null 2>&1; then

Review comment (bug):
should this check `nvm_has pkg_info`? currently if it fails, it'll fall through to echoing the empty string, which isn't likely what `nvm_get_arch` should do.


Code after:

nvm_get_os() {
  local NVM_UNAME
  NVM_UNAME="$(uname -a)"
  local NVM_OS
  case "$NVM_UNAME" in
    Linux\ *) NVM_OS=linux ;;
    Darwin\ *) NVM_OS=darwin ;;
    SunOS\ *) NVM_OS=sunos ;;
    FreeBSD\ *) NVM_OS=freebsd ;;
  esac
  echo "$NVM_OS"
}

nvm_get_arch() {
  local HOST_ARCH
  local NVM_OS
  local EXIT_CODE

  NVM_OS="$(nvm_get_os)"
  # If the OS is SunOS, first try to use pkgsrc to guess
  # the most appropriate arch. If it's not available, use
  # isainfo to get the instruction set supported by the
  # kernel.
  if [ "_$NVM_OS" = "_sunos" ]; then
    HOST_ARCH=$(pkg_info -Q MACHINE_ARCH pkg_install)
    EXIT_CODE=$?
    if [ $EXIT_CODE -ne 0 ]; then
      HOST_ARCH=$(isainfo -n)
    fi
  else
     HOST_ARCH="$(uname -m)"
  fi

  local NVM_ARCH
  case "$HOST_ARCH" in
    x86_64 | amd64) NVM_ARCH="x64" ;;
    i*86) NVM_ARCH="x86" ;;
    *) NVM_ARCH="$HOST_ARCH" ;;
  esac
  echo "$NVM_ARCH"
}

nvm_ensure_default_set() {
  local VERSION
  VERSION="$1"
  if [ -z "$VERSION" ]; then
    echo 'nvm_ensure_default_set: a version is required' >&2
    return 1
  fi
  if nvm_alias default >/dev/null 2>&1; then
Repository: nvm-sh/nvm
File: nvm.sh (Shell)

Code before:

nvm_get_os() {
  local NVM_UNAME
  NVM_UNAME="$(uname -a)"
  local NVM_OS
  case "$NVM_UNAME" in
    Linux\ *) NVM_OS=linux ;;
    Darwin\ *) NVM_OS=darwin ;;
    SunOS\ *) NVM_OS=sunos ;;
    FreeBSD\ *) NVM_OS=freebsd ;;
  esac
  echo "$NVM_OS"
}

nvm_get_arch() {
  local HOST_ARCH
  local NVM_OS

  NVM_OS="$(nvm_get_os)"
  # If the OS is SunOS, first try to use pkgsrc to guess
  # the most appropriate arch. If it's not available, use
  # isainfo to get the instruction set supported by the
  # kernel.
  if [ "_$NVM_OS" = "_sunos" ]; then
    HOST_ARCH=$(pkg_info -Q MACHINE_ARCH pkg_install)
    local EXIT_CODE
    EXIT_CODE="$?"
    if [ "_$EXIT_CODE" != "_0" ]; then
      HOST_ARCH=$(isainfo -n)
    fi
  else
     HOST_ARCH="$(uname -m)"
  fi

  local NVM_ARCH
  case "$HOST_ARCH" in
    x86_64 | amd64) NVM_ARCH="x64" ;;
    i*86) NVM_ARCH="x86" ;;
    *) NVM_ARCH="$HOST_ARCH" ;;
  esac
  echo "$NVM_ARCH"
}

nvm_ensure_default_set() {
  local VERSION
  VERSION="$1"
  if [ -z "$VERSION" ]; then
    echo 'nvm_ensure_default_set: a version is required' >&2
    return 1
  fi
  if nvm_alias default >/dev/null 2>&1; then

Review comment (suggestion):
Right, good catch :+1: The original intent was to catch this case with:

```
if [ $EXIT_CODE -ne 0 ]; then
  HOST_ARCH=$(isainfo -n)
fi
```

but the `local` command before the test for `$?` would overwrite `$?`. So I moved the `local` declaration at the top of the function and left it as is. I also [added tests to catch that problem](https://github.com/creationix/nvm/pull/854/files#diff-7a60576df1bb49d940a58f93de5a1fc7R72). 

I think having a `nvm_has_pkg_info` function is a bit overkill for this use case, but If you'd still prefer to handle it that way I'm fine with it too.


Code after:

nvm_get_os() {
  local NVM_UNAME
  NVM_UNAME="$(uname -a)"
  local NVM_OS
  case "$NVM_UNAME" in
    Linux\ *) NVM_OS=linux ;;
    Darwin\ *) NVM_OS=darwin ;;
    SunOS\ *) NVM_OS=sunos ;;
    FreeBSD\ *) NVM_OS=freebsd ;;
  esac
  echo "$NVM_OS"
}

nvm_get_arch() {
  local HOST_ARCH
  local NVM_OS
  local EXIT_CODE

  NVM_OS="$(nvm_get_os)"
  # If the OS is SunOS, first try to use pkgsrc to guess
  # the most appropriate arch. If it's not available, use
  # isainfo to get the instruction set supported by the
  # kernel.
  if [ "_$NVM_OS" = "_sunos" ]; then
    HOST_ARCH=$(pkg_info -Q MACHINE_ARCH pkg_install)
    EXIT_CODE=$?
    if [ $EXIT_CODE -ne 0 ]; then
      HOST_ARCH=$(isainfo -n)
    fi
  else
     HOST_ARCH="$(uname -m)"
  fi

  local NVM_ARCH
  case "$HOST_ARCH" in
    x86_64 | amd64) NVM_ARCH="x64" ;;
    i*86) NVM_ARCH="x86" ;;
    *) NVM_ARCH="$HOST_ARCH" ;;
  esac
  echo "$NVM_ARCH"
}

nvm_ensure_default_set() {
  local VERSION
  VERSION="$1"
  if [ -z "$VERSION" ]; then
    echo 'nvm_ensure_default_set: a version is required' >&2
    return 1
  fi
  if nvm_alias default >/dev/null 2>&1; then
Repository: nvm-sh/nvm
File: nvm.sh (Shell)

Code before:

nvm_get_os() {
  local NVM_UNAME
  NVM_UNAME="$(uname -a)"
  local NVM_OS
  case "$NVM_UNAME" in
    Linux\ *) NVM_OS=linux ;;
    Darwin\ *) NVM_OS=darwin ;;
    SunOS\ *) NVM_OS=sunos ;;
    FreeBSD\ *) NVM_OS=freebsd ;;
  esac
  echo "$NVM_OS"
}

nvm_get_arch() {
  local HOST_ARCH
  local NVM_OS

  NVM_OS="$(nvm_get_os)"
  # If the OS is SunOS, first try to use pkgsrc to guess
  # the most appropriate arch. If it's not available, use
  # isainfo to get the instruction set supported by the
  # kernel.
  if [ "_$NVM_OS" = "_sunos" ]; then
    HOST_ARCH=$(pkg_info -Q MACHINE_ARCH pkg_install)
    local EXIT_CODE
    EXIT_CODE="$?"
    if [ "_$EXIT_CODE" != "_0" ]; then
      HOST_ARCH=$(isainfo -n)
    fi
  else
     HOST_ARCH="$(uname -m)"
  fi

  local NVM_ARCH
  case "$HOST_ARCH" in
    x86_64 | amd64) NVM_ARCH="x64" ;;
    i*86) NVM_ARCH="x86" ;;
    *) NVM_ARCH="$HOST_ARCH" ;;
  esac
  echo "$NVM_ARCH"
}

nvm_ensure_default_set() {
  local VERSION
  VERSION="$1"
  if [ -z "$VERSION" ]; then
    echo 'nvm_ensure_default_set: a version is required' >&2
    return 1
  fi
  if nvm_alias default >/dev/null 2>&1; then

Review comment (suggestion):
We already have `nvm_has` - i don't mean a new function, i mean calling `nvm_has` with the argument `pkg_info`


Code after:

nvm_get_os() {
  local NVM_UNAME
  NVM_UNAME="$(uname -a)"
  local NVM_OS
  case "$NVM_UNAME" in
    Linux\ *) NVM_OS=linux ;;
    Darwin\ *) NVM_OS=darwin ;;
    SunOS\ *) NVM_OS=sunos ;;
    FreeBSD\ *) NVM_OS=freebsd ;;
  esac
  echo "$NVM_OS"
}

nvm_get_arch() {
  local HOST_ARCH
  local NVM_OS
  local EXIT_CODE

  NVM_OS="$(nvm_get_os)"
  # If the OS is SunOS, first try to use pkgsrc to guess
  # the most appropriate arch. If it's not available, use
  # isainfo to get the instruction set supported by the
  # kernel.
  if [ "_$NVM_OS" = "_sunos" ]; then
    HOST_ARCH=$(pkg_info -Q MACHINE_ARCH pkg_install)
    EXIT_CODE=$?
    if [ $EXIT_CODE -ne 0 ]; then
      HOST_ARCH=$(isainfo -n)
    fi
  else
     HOST_ARCH="$(uname -m)"
  fi

  local NVM_ARCH
  case "$HOST_ARCH" in
    x86_64 | amd64) NVM_ARCH="x64" ;;
    i*86) NVM_ARCH="x86" ;;
    *) NVM_ARCH="$HOST_ARCH" ;;
  esac
  echo "$NVM_ARCH"
}

nvm_ensure_default_set() {
  local VERSION
  VERSION="$1"
  if [ -z "$VERSION" ]; then
    echo 'nvm_ensure_default_set: a version is required' >&2
    return 1
  fi
  if nvm_alias default >/dev/null 2>&1; then