Skip to content

Commit

Permalink
2.0.0
Browse files Browse the repository at this point in the history
- Rewrite for Fish 3.0.
- Use More consistent command-line interface.
- Don't mutate global node so that activating a version
  only lasts for the lifetime of the current shell.
- Introduce the nvm_default_version variable to
  persist a Node version system-wide.
- Use XDG_DATA_HOME.
- Switch to GitHub Actions.
- Write new docs.
  • Loading branch information
jorgebucaran committed Dec 4, 2020
1 parent 2975358 commit 4b3c5bf
Show file tree
Hide file tree
Showing 9 changed files with 290 additions and 299 deletions.
24 changes: 15 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ name: CI
on: push

jobs:
build:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- run: |
sudo add-apt-repository -y ppa:fish-shell/nightly-master
- name: Install Fish
run: |
sudo apt-add-repository -yn ppa:fish-shell/release-3
sudo apt-get update
sudo apt-get -y install fish
- run: |
curl git.io/fisher --create-dirs -sLo ~/.config/fish/functions/fisher.fish
fish -c "fisher add . && nvm use lts && node -v"
sudo apt-get install -y fish
- name: Install Fisher
run: curl -sL https://git.io/fisher | source && fisher install jorgebucaran/fisher
shell: fish {0}

- name: Install NVM
run: |
fisher install $GITHUB_WORKSPACE
nvm install 6
test (nvm current) = v6.17.1
shell: fish {0}
115 changes: 45 additions & 70 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,125 +1,100 @@
# nvm.fish

> 100% pure-<a href="https://fishshell.com" title="friendly interactive shell">fish</a> Node.js version manager.
> Node version management lovingly made for [Fish](https://fishshell.com).
- `.nvmrc` support.
- Seamless shell integration. <kbd>Tab</kbd>-completions? You got them.
- No dependencies, no subshells, no configuration setup, no fluff—it's so easy it hurts.
- Because nvm.fish runs natively by fish, it's insanely fast ([see this discussion](https://github.com/jorgebucaran/fish-nvm/issues/82)).
Not [_that_](https://github.com/nvm-sh/nvm) POSIX-compatible script. Designed for [Fish](https://fishshell.com), this tool helps you manage multiple active versions of Node on a single local environment. Quickly install and switch between runtimes without cluttering your home directory or breaking system-wide scripts.

![](https://gistcdn.githack.com/jorgebucaran/00f6d3f301483a01a00e836eb17a2b3e/raw/26625256b5e5ccb632f990727db70055ae24e584/nvm.fish.svg)
- No dependencies, no setup, no clutter—it just works.
- 100% Fish—quick & easy to contribute to or change.
- <kbd>Tab</kbd>-completable seamless shell integration.
- `.node-version` and `.nvmrc` support. ✅
- [XDG Base Directory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) compliant.

## Installation

Install with [Fisher](https://github.com/jorgebucaran/fisher) (recommended):
Install with [Fisher](https://github.com/jorgebucaran/fisher):

```console
fisher install jorgebucaran/nvm.fish
```

<details>
<summary>Not using a package manager?</summary>

###
## Quickstart

Copy [`conf.d/nvm.fish`](conf.d/nvm.fish), [`functions/nvm.fish`](functions/nvm.fish), and [`completions/nvm.fish`](completions/nvm.fish) to your fish configuration directory preserving the directory structure.
Install the latest Node release and start using it.

```fish
set -q XDG_CONFIG_HOME; or set XDG_CONFIG_HOME ~/.config
for i in conf.d functions completions
curl https://git.io/$i.nvm.fish --create-dirs -sLo $XDG_CONFIG_HOME/fish/$i/nvm.fish
end
```console
nvm install latest
```

To uninstall nvm, just run:
Install the latest [LTS](https://github.com/nodejs/Release) (long-term support) Node release.

```console
nvm install lts
```
rm -f $XDG_CONFIG_HOME/fish/{conf.d,functions,completions}/nvm.fish && emit nvm_uninstall
```

</details>

## Quickstart

Download and switch to the latest Node.js release.
Install an older LTS release by codename.

```console
nvm use latest
nvm install carbon
```

> **Note:** This downloads the latest Node.js release tarball from the [official mirror](https://nodejs.org/dist), extracts it to <code>[\$XDG_CONFIG_HOME](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables)/nvm</code> and modifies your `$PATH` so it can be used immediately. Learn more about the Node.js release schedule [here](https://github.com/nodejs/Release).
> Installs `8.16.2`, the latest release of the Carbon LTS line.
Download and switch to the latest LTS (long-term support) Node.js release.
Or install a specific version of Node.

```console
nvm use lts
nvm install v12.9.1
```

You can create a `.nvmrc` file in the root of your project (or any parent directory) and run `nvm` to use the version in it. `nvm` will try to find the nearest `.nvmrc` file, traversing the directory tree from the current working directory upwards.
> Supports full or partial version numbers, starting with an optional "v".
```console
node -v > .nvmrc
nvm
The `nvm install` command activates the specified Node version only in the current environment. If you want to set the default version for new shells use:

```fish
set --universal nvm_default_version v12.9.1
```

Run `nvm` in any subdirectory of a directory with an `.nvmrc` file to switch to the version from that file. Similarly, running `nvm use <version>` updates that `.nvmrc` file with the specified version.
Activate a version you've already installed.

```console
├── README.md
├── dist
├── node_modules
├── package.json
└── src
└── index.js
nvm use lts
```

List which versions you have installed (includes any previously installed system Node if there is one).

```console
echo lts >.nvmrc
cd src
nvm
node -v
v10.15.1
$ nvm list
system
v8.17.0 lts/carbon
v12.9.1
▶ v14.15.1 lts/fermium
v15.3.0 latest
```

### Listing versions

List all the supported Node.js versions you can download and switch to.
Or list all the Node versions available to install.

```console
nvm ls
nvm list-remote
```

Want to remove a Node version? You can do that too.

```console
...
10.14.2 (lts/dubnium)
10.15.0 (lts/dubnium)
11.0.0
11.1.0
11.2.0
11.3.0
11.4.0
11.5.0
11.6.0
11.7.0 (latest/current)
nvm remove v12.9.1
```

You can use a regular expression to narrow down the output.
## `.nvmrc`

```console
nvm ls '^8.[4-6]'
```
An `.nvmrc` file makes it easy to peg a specific version of Node for different projects. Just create an `.nvmrc` (or `.node-version`) file containing a version number or alias, e.g., `latest`, `lts`, `carbon`, in the root of your project.

```console
8.4.0 (lts/carbon)
8.5.0 (lts/carbon)
8.6.0 (lts/carbon)
node -v >.nvmrc
```

To customize the download mirror, e.g., if you are behind a firewall, you can set `$nvm_mirror`:
Then run `nvm install` to install or `nvm use` to activate that version. Works from anywhere inside your project by traversing the directory hierarchy until an `.nvmrc` is found!

```console
set -g nvm_mirror http://npm.taobao.org/mirrors/node
nvm install
```

## License
Expand Down
23 changes: 18 additions & 5 deletions completions/nvm.fish
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
complete -xc nvm -n __fish_use_subcommand -a ls -d "List available versions matching <regex>"
complete -xc nvm -n __fish_use_subcommand -a use -d "Download <version> and modify PATH so it's available"
complete -xc nvm -n __fish_use_subcommand -a --help -d "Show usage help"
complete -xc nvm -n __fish_use_subcommand -a --version -d "Show the current version of nvm"
complete -c nvm --exclusive --long version -d "Print nvm version"
complete -c nvm --exclusive --long help -d "Print this help message"

nvm complete
complete -c nvm --exclusive --condition "__fish_use_subcommand" -a install -d "Download and activate a given version (use nearest .nvmrc file if none is given)"
complete -c nvm --exclusive --condition "__fish_use_subcommand" -a use -d "Activate a version in the current shell"
complete -c nvm --exclusive --condition "__fish_use_subcommand" -a list -d "List installed versions"
complete -c nvm --exclusive --condition "__fish_use_subcommand" -a list-remote -d "List versions available to install (matching optional regex)"
complete -c nvm --exclusive --condition "__fish_use_subcommand" -a current -d "Print currently-active version"
complete -c nvm --exclusive --condition "__fish_seen_subcommand_from install" -a "(
test -e $nvm_data && string split ' ' <$nvm_data/.index
)"
complete -c nvm --exclusive --condition "__fish_seen_subcommand_from use" -a "(_nvm_list | string split ' ')"
complete -c nvm --exclusive --condition "__fish_use_subcommand" -a uninstall -d "Uninstall a version"
complete -c nvm --exclusive --condition "__fish_seen_subcommand_from uninstall" -a "(
_nvm_list | string split ' ' | string replace system ''
)"
complete -c nvm --exclusive --condition "__fish_seen_subcommand_from use uninstall" -a "(
set --query nvm_default_version && echo default
)"
38 changes: 24 additions & 14 deletions conf.d/nvm.fish
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
function _nvm_uninstall -e nvm_uninstall
if test -s "$nvm_config/version"
read -l ver <$nvm_config/version
if set -l i (contains -i -- "$nvm_config/$ver/bin" $fish_user_paths)
set -e fish_user_paths[$i]
end
command rm -f $nvm_config/version
end

for name in (set -n | command awk '/^nvm_/')
set -e "$name"
end

functions -e (functions -a | command awk '/^_nvm_/')
set --global nvm_version 2.0.0

set --query XDG_DATA_HOME \
&& set --global nvm_data $XDG_DATA_HOME/nvm \
|| set --global nvm_data ~/.local/share/nvm
set --query nvm_mirror || set --global nvm_mirror https://nodejs.org/dist

if set --query nvm_default_version && ! set --query nvm_current_version
nvm use $nvm_default_version >/dev/null
end

function _nvm_install -e nvm_install
test ! -d $nvm_data && command mkdir -p $nvm_data
echo "Downloading the Node distribution index for the first time..." 2>/dev/null
_nvm_index_update $nvm_mirror/index.tab $nvm_data/.index
end

function _nvm_uninstall -e nvm_uninstall
command rm -rf $nvm_data
set --query nvm_current_version && _nvm_version_deactivate $nvm_current_version

set --names | string replace --filter --regex "^nvm_" -- "set --erase nvm_" | source
functions --erase (functions --all | string match --entire --regex "^_nvm_")
complete --erase --command nvm
end
12 changes: 12 additions & 0 deletions functions/_nvm_index_update.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function _nvm_index_update -a mirror index
command curl --location --silent $mirror | command awk -v OFS=\t '
/v0.9.12/ { exit } # Unsupported
NR > 1 {
print $1 (NR == 2 ? " latest" : $10 != "-" ? " lts/" tolower($10) : "")
}
' > $index.temp && command mv $index.temp $index && return

command rm -f $index.temp
echo "nvm: Invalid index or unavailable host: \"$mirror\"" >&2
return 1
end
11 changes: 11 additions & 0 deletions functions/_nvm_list.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function _nvm_list
set --local versions $nvm_data/*
set --query versions[1] \
&& string match --entire --regex (string match --regex "v\d.+" $versions \
| string escape --style=regex \
| string join "|"
) <$nvm_data/.index

command --all node | string match --quiet --invert --regex "^$nvm_data" \
&& echo system
end
4 changes: 4 additions & 0 deletions functions/_nvm_version_activate.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function _nvm_version_activate -a v
set --global --export nvm_current_version $v
set --prepend PATH $nvm_data/$v/bin
end
5 changes: 5 additions & 0 deletions functions/_nvm_version_deactivate.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function _nvm_version_deactivate -a v
test "$nvm_current_version" = "$v" && set --erase nvm_current_version
set --local index (contains --index -- $nvm_data/$v/bin $PATH) \
&& set --erase PATH[$index]
end
Loading

0 comments on commit 4b3c5bf

Please sign in to comment.