This commit is contained in:
Yash Singh 2021-04-19 09:58:42 -07:00 committed by Yash-Singh1
parent 8cd1fd220a
commit e1bbf0b619
6 changed files with 8 additions and 176 deletions

View File

@ -31,7 +31,6 @@
- [Set default node version](#set-default-node-version)
- [Use a mirror of node binaries](#use-a-mirror-of-node-binaries)
- [.nvmrc](#nvmrc)
- [`nvm cat`](#nvm-cat)
- [Deeper Shell Integration](#deeper-shell-integration)
- [bash](#bash)
- [Automatically call `nvm use`](#automatically-call-nvm-use)
@ -518,27 +517,14 @@ Now using node v5.9.1 (npm v3.7.3)
```bash
# This is a comment
node # Another comment
node # Another comment
```
When reading the `.nvmrc` file, nvm will:
1. Remove all comments on their own line as shown in the first line of the example
2. Remove all comments in middle of a line. Because of this, # is not allowed inside your `nvm` alias. Example on line 3
3. Remove all newlines such as the one on line 2
4. Remove all tabs, such as the one on line 3
5. Remove all spaces, such as the ones between `node` and `# This is another comment`, because of this, `nvm` aliases may not have a space
For a preview of how your file looks without comments, check out the `nvm cat` command:
##### nvm cat
The following is the usage for `nvm cat`:
```
nvm cat [-pc] [--silent] -f [file] [header]
```
The `--path` or the `-p` boolean flag indicates whether the file provided is the absolute path to the file. If it is not true, then `nvm` will search for the file provided. The `-c` or the `--comments` boolean flag determines whether comments will be stripped or not by `nvm`. The `--silent` flag enables silent mode. The `-f` option allows you to provide the file that is being read by `nvm`. The default option for `-f` is `.nvmrc`. An alias for `-f` is `--file`. Any other argument that matches the wildcard * will be appended to the header. The header is printed out before the file's contents are. If the header is not provided then the `NVM_CAT_HEADER` enviormental variable. This variable is not set by default. You can set it to change your header. Any instance of `:FILE:` will be replaced by the file being read in the header.
`nvm use` et. al. will traverse directory structure upwards from the current directory looking for the `.nvmrc` file. In other words, running `nvm use` et. al. in any subdirectory of a directory with an `.nvmrc` will result in that `.nvmrc` being utilized.
The contents of a `.nvmrc` file **must** be the `<version>` (as described by `nvm --help`) followed by a newline. No trailing spaces are allowed, and the trailing newline is required.
5. Trim the lines
### Deeper Shell Integration
@ -702,10 +688,8 @@ nvm exposes the following environment variables:
- `NVM_INC` - node's include file directory (useful for building C/C++ addons for node).
- `NVM_CD_FLAGS` - used to maintain compatibility with zsh.
- `NVM_RC_VERSION` - version from .nvmrc file if being used.
- `NVM_CAT_HEADER` - this enviormental variable contains the default header the `cat` command uses
Additionally, nvm modifies `PATH`, and, if present, `MANPATH` and `NODE_PATH` when changing versions. All instances of `:FILE:` will be replaced with the file being read in `NVM_CAT_HEADER`.
Additionally, nvm modifies `PATH`, and, if present, `MANPATH` and `NODE_PATH` when changing versions.
## Bash Completion
@ -755,13 +739,6 @@ nvm uninstall:
my_alias default v10.22.0 v12.18.3 v14.8.0
```
nvm cat:
> $ nvm cat -f <kbd>Tab</kbd>
```
.nvmrc example.txt this.config foo.sh .barconfig
```
## Compatibility Issues
`nvm` will encounter some issues if you have some non-default settings set. (see [#606](/../../issues/606))

93
nvm.sh
View File

@ -393,7 +393,7 @@ nvm_rc_version() {
fi
return 1
fi
NVM_RC_VERSION="$(command cat "${NVMRC_PATH}" | command tr -d '\r' | sed -e 's/\s*#.*$//' | sed -e '/^\s*$/d' | command tr -d '\n' | command tr -d '\t' | command head -n 1 | sed -e 's/[[:space:]]//g')" || command printf ''
NVM_RC_VERSION="$(command cat "${NVMRC_PATH}" | command tr -d '\r' | sed -e 's/\s*#.*$// ; /^\s*$/d' | command tr -d '\n' | command tr -d '\t' | command head -n 1 | sed -e 's/ *$//g ; s/^ *//g')" || command printf ''
if [ -z "${NVM_RC_VERSION}" ]; then
if [ "${NVM_USE_SILENT}" != true ]; then
nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\""
@ -2893,12 +2893,6 @@ nvm() {
nvm_echo ' nvm which [current | <version>] Display path to installed node version. Uses .nvmrc if available'
nvm_echo ' nvm cache dir Display path to the cache directory for nvm'
nvm_echo ' nvm cache clear Empty cache directory for nvm'
nvm_echo ' nvm cat Allows you to preview how nvm reads a file'
nvm_echo ' --silent Turn on silent mode and do not print out anything'
nvm_echo ' --file, -f [file] Allows you to set the file read, default is .nvmrc'
nvm_echo ' -c, --comments This flag enables and disables whether comments will be stripped'
nvm_echo ' -p, --path If path mode is on, nvm will read the path given, not search for the file'
nvm_echo ' [header] Any argument matching * will be appended to the header'
nvm_echo
nvm_echo 'Example:'
nvm_echo ' nvm install 8.0.0 Install a specific version number'
@ -3377,78 +3371,6 @@ nvm() {
fi
return $EXIT_CODE
;;
"cat")
local NVM_USE_SILENT
local NVM_USE_COMMENTS
local NVM_METHOD
local NVM_FILE
local NVM_RC_VERSION
local NVM_HEADER
export NVM_RC_VERSION=''
local NVM_RC_LOCATION
NVM_FILE=".nvmrc"
local NVM_FINDING_FILE
while [ $# -ne 0 ]
do
if [ "$NVM_FINDING_FILE" == 1 ]; then
NVM_FILE=${1}
NVM_FINDING_FILE=0
shift;continue
fi
case $1 in
"--silent") NVM_USE_SILENT=1;shift;continue ;;
"--comments" | "-c") NVM_USE_COMMENTS=1;shift;continue ;;
"-f" | "--file") NVM_FINDING_FILE=1;shift;continue ;;
"-p" | "--path") NVM_METHOD=1;shift;continue ;;
"*")
if [ -z "$NVM_HEADER" ]; then
NVM_HEADER="${1}"
else
NVM_HEADER="${NVM_HEADER} ${1}"
fi
esac
shift
done
if [ -n "${NVM_CAT_HEADER}" ] && [ -z "${NVM_HEADER}" ]; then
NVM_HEADER="${NVM_HEADER}"
fi
NVM_HEADER="$(command echo ${NVM_HEADER} | sed s/:FILE:/${NVM_FILE}/g)"
if [ -z "${NVM_FILE}" ]; then
if [ "$NVM_USE_SILENT" -ne 1 ]; then
nvm_err "Empty file provided, aborting"
fi
exit 1
fi
if [ "${NVM_METHOD}" == 1 ] && [ ! -f "$NVM_FILE" ]; then
if [ "${NVM_USE_SILENT}" != 1 ]; then
nvm_err "The file ${NVM_FILE} does not exist."
fi
return 1
fi
if [ "$NVM_USE_COMMENTS" != 1 ]; then
if [ "$NVM_METHOD" != 1 ]; then
NVM_RC_LOCATION="$(nvm_find_up ${NVM_FILE})/${NVM_FILE}"
NVM_RC_VERSION="$(command cat "${NVM_RC_LOCATION}" | command tr -d '\r' | sed -e 's/\s*#.*$//' | sed -e '/^\s*$/d' | command tr -d '\n' | command tr -d '\t' | command head -n 1 | sed -e 's/[[:space:]]//g')" || command printf ''
else
NVM_RC_VERSION="$(command cat "${NVM_FILE}" | command tr -d '\r' | sed -e 's/\s*#.*$//' | sed -e '/^\s*$/d' | command tr -d '\n' | command tr -d '\t' | command head -n 1 | sed -e 's/[[:space:]]//g')" || command printf ''
fi
else
if [ "$NVM_METHOD" != 1 ]; then
NVM_RC_LOCATION="$(nvm_find_up ${NVM_FILE})/${NVM_FILE}"
NVM_RC_VERSION="$(command cat "${NVM_RC_LOCATION}")"
else
NVM_RC_VERSION="$(command cat ${NVM_FILE})"
fi
fi
if [ "$NVM_USE_SILENT" != 1 ]; then
if [ -n "${NVM_HEADER}" ]; then
nvm_echo "$NVM_HEADER"
fi
nvm_echo "$NVM_RC_VERSION"
fi
;;
"uninstall")
if [ $# -ne 1 ]; then
>&2 nvm --help
@ -3581,7 +3503,7 @@ nvm() {
;;
"use")
local PROVIDED_VERSION
local NVM_SILENT
local NVM_USE_SILENT
local NVM_SILENT_ARG
local NVM_DELETE_PREFIX
NVM_DELETE_PREFIX=0
@ -3590,7 +3512,7 @@ nvm() {
while [ $# -ne 0 ]; do
case "$1" in
--silent)
NVM_SILENT=1
NVM_USE_SILENT=1
NVM_SILENT_ARG='--silent'
;;
--delete-prefix) NVM_DELETE_PREFIX=1 ;;
@ -3611,7 +3533,7 @@ nvm() {
VERSION="$(nvm_match_version "lts/${NVM_LTS:-*}")"
elif [ -z "${PROVIDED_VERSION-}" ]; then
# Get the .nvmrc version depending if quiet mode is on or not
if [ $NVM_USE_SILENT -ne 1 ]; then
if [ "$NVM_USE_SILENT" -ne 1 ]; then
nvm_rc_version
else
nvm_rc_version --silent
@ -4048,14 +3970,9 @@ nvm() {
# so, unalias it.
nvm unalias "${ALIAS}"
return $?
elif [ "${ALIAS}" == "*#*" ]; then
elif grep -q "#" <<< "${ALIAS}"; then
nvm_err "Aliases may not have a # inside it"
exit 1
elif [ "${ALIAS}" == "${TARGET}" ]; then
nvm_err "The alias may not equal the target"
exit 1
elif [ "${ALIAS}" == "* *" ]; then
nvm_err "The alias may not have a space inside it"
elif [ "${TARGET}" != '--' ]; then
# a target was passed: create an alias
if [ "${ALIAS#*\/}" != "${ALIAS}" ]; then

View File

@ -1,17 +0,0 @@
#!/bin/sh
die () { echo "$@" ; rm -f .nvmrc; exit 1; }
\. ../../../nvm.sh
touch .nvmrc
echo 'test' > .nvmrc
OUTPUT=$(nvm cat --silent || die 'nvm cat --silent failed')
EXPECTED_OUTPUT=""
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
|| die "'nvm cat --silent' output was not silenced to '$EXPECTED_OUTPUT'; got '$OUTPUT'"
rm -f .nvmrc

View File

@ -1,16 +0,0 @@
#!/bin/sh
die () { echo "$@" ; rm -f .nvmrc; exit 1; }
\. ../../../nvm.sh
touch .nvmrc
echo '# test' > .nvmrc
OUTPUT=$(nvm cat -c || die 'nvm cat -c failed')
EXPECTED_OUTPUT="# test"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
|| die "'nvm cat -c' output was not expected as '$EXPECTED_OUTPUT'; got '$OUTPUT'"
rm -f .nvmrc

View File

@ -1,16 +0,0 @@
#!/bin/sh
die () { echo "$@" ; rm -f .nvmrc; exit 1; }
\. ../../../nvm.sh
touch .testfornvmcat
echo 'test' > .testfornvmcat
OUTPUT=$(nvm cat -p -f .testfornvmcat -c || die 'nvm cat -p -f .testfornvmcat -c failed')
EXPECTED_OUTPUT="$(cat .testfornvmcat)"
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
|| die "'nvm cat -p -f .testfornvmcat -c' output was not as '${EXPECTED_OUTPUT}'; got '${OUTPUT}'"
rm -f .testfornvmcat

View File

@ -1,13 +0,0 @@
#!/bin/sh
touch .testfornvmcat
rm -f .testfornvmcat
die() { echo "$@"; exit 1; }
nvm deactivate >/dev/null 2>&1
\. ../../../nvm.sh
OUTPUT=$(nvm cat -p -f .testfornvmcat -c 2> /dev/null && die nvm cat -p -f .testfornvmcat -c failed)
EXPECTED_OUTPUT=""
[ "_$OUTPUT" = "_$EXPECTED_OUTPUT" ] \
|| die "'nvm cat -p -f .testfornvmcat -c' output was not as '${EXPECTED_OUTPUT}'; got '${OUTPUT}'"