npx 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env bash
  2. # This is used by the Node.js installer, which expects the cygwin/mingw
  3. # shell script to already be present in the npm dependency folder.
  4. (set -o igncr) 2>/dev/null && set -o igncr; # cygwin encoding fix
  5. basedir=`dirname "$0"`
  6. case `uname` in
  7. *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
  8. esac
  9. NODE_EXE="$basedir/node.exe"
  10. if ! [ -x "$NODE_EXE" ]; then
  11. NODE_EXE=node
  12. fi
  13. # these paths are passed to node.exe, so they need to match whatever
  14. # kind of paths Node.js thinks it's using, typically win32 paths.
  15. CLI_BASEDIR="$("$NODE_EXE" -p 'require("path").dirname(process.execPath)')"
  16. if [ $? -ne 0 ]; then
  17. # if this didn't work, then everything else below will fail
  18. echo "Could not determine Node.js install directory" >&2
  19. exit 1
  20. fi
  21. NPM_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npm-cli.js"
  22. NPX_CLI_JS="$CLI_BASEDIR/node_modules/npm/bin/npx-cli.js"
  23. NPM_PREFIX=`"$NODE_EXE" "$NPM_CLI_JS" prefix -g`
  24. NPM_PREFIX_NPX_CLI_JS="$NPM_PREFIX/node_modules/npm/bin/npx-cli.js"
  25. # a path that will fail -f test on any posix bash
  26. NPX_WSL_PATH="/.."
  27. # WSL can run Windows binaries, so we have to give it the win32 path
  28. # however, WSL bash tests against posix paths, so we need to construct that
  29. # to know if npm is installed globally.
  30. if [ `uname` = 'Linux' ] && type wslpath &>/dev/null ; then
  31. NPX_WSL_PATH=`wslpath "$NPM_PREFIX_NPX_CLI_JS"`
  32. fi
  33. if [ -f "$NPM_PREFIX_NPX_CLI_JS" ] || [ -f "$NPX_WSL_PATH" ]; then
  34. NPX_CLI_JS="$NPM_PREFIX_NPX_CLI_JS"
  35. fi
  36. "$NODE_EXE" "$NPX_CLI_JS" "$@"