Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fnm #593

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions modules/fnm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Fast Node Manager (fnm)

Enables Nushell support for [Fast Node Manager (fnm)](https://github.com/Schniz/fnm), a fast and simple Node.js version manager. Based on [this GitHub issue](https://github.com/Schniz/fnm/issues/463) and [fnm-nushell](https://github.com/Southclaws/fnm-nushell).

Requires `fnm` to be installed separately.

## Install

Clone this repo or copy the `fnm.nu` file wherever your prefer to keep your Nushell scripts.

Edit your Nushell config file (`$nu.config-path`) and add the line:

```nu
use /path/to/fnm.nu
```
37 changes: 37 additions & 0 deletions modules/fnm/fnm.nu
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export-env {
def fnm-env [] {
mut env_vars = {}
let pwsh_vars = (^fnm env --shell power-shell)
let var_table = ($pwsh_vars
| lines
| parse "$env:{key} = \"{value}\""
)
for v in $var_table {
mut value: any = null
if ($v.key | str downcase) == 'path' {
$value = ($v.value | split row (char esep))
} else {
$value = $v.value
}
$env_vars = ($env_vars | insert $v.key $value)
}
return $env_vars
}

if not (which fnm | is-empty) {
fnm-env | load-env

if (not ($env | default false __fnm_hooked | get __fnm_hooked)) {
$env.__fnm_hooked = true
$env.config = ($env | default {} config).config
$env.config = ($env.config | default {} hooks)
$env.config = ($env.config | update hooks ($env.config.hooks | default {} env_change))
$env.config = ($env.config | update hooks.env_change ($env.config.hooks.env_change | default [] PWD))
$env.config = ($env.config | update hooks.env_change.PWD ($env.config.hooks.env_change.PWD | append { |before, after|
if ('FNM_DIR' in $env) and ([.nvmrc .node-version] | path exists | any { |it| $it }) {
(^fnm use); (fnm-env | load-env)
}
}))
}
}
}