Skip to content

Commit

Permalink
Update fnm.nu module and load .nushellrc file
Browse files Browse the repository at this point in the history
  • Loading branch information
myty committed Apr 9, 2024
1 parent 3e8a27f commit 1b1b0e1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
2 changes: 1 addition & 1 deletion nushell/config.nu
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use "~/.config/dotfiles/nushell/nu_scripts/custom-completions/git/git-completion
use "~/.config/dotfiles/nushell/nu_scripts/custom-completions/npm/npm-completions.nu" *
use "~/.config/dotfiles/nushell/nu_scripts/custom-completions/scoop/scoop-completions.nu" *

source "~/.config/dotfiles/nushell/scripts/fnm.nu"
use "~/.config/dotfiles/nushell/nu_scripts/modules/fnm/fnm.nu"

# Load Starship
source ~/.cache/starship/init.nu
58 changes: 34 additions & 24 deletions nushell/scripts/fnm.nu
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
export-env {
$env.config = ($env.config | upsert hooks {
env_change: {
PWD: ($env.config.hooks.env_change.PWD ++
[{
condition: {|before, after| [.nvmrc .node-version] | path exists | any { |it| $it }}
code: {|before, after|
if ('FNM_DIR' in $env) {
fnm --log-level=quiet use
}
}
}]
def fnm-env [] {
mut env_vars = {}
let pwsh_vars = (
^fnm env --shell power-shell |
lines |
parse "$env:{key} = \"{value}\""
)
}
})
}

# fnm-prefixed vars
for v in ($pwsh_vars | range 1..) {
$env_vars = ($env_vars | insert $v.key $v.value)
}

# path
let env_used_path = ($env | columns | where {str downcase | $in == "path"} | get 0)
let path_value = ($pwsh_vars | get 0.value | split row (char esep))
$env_vars = ($env_vars | insert $env_used_path $path_value)

if not (which fnm | is-empty) {
^fnm env --json | from json | load-env
# Checking `Path` for Windows
let path = if 'Path' in $env { $env.Path } else { $env.PATH }
let node_path = if (sys).host.name == 'Windows' {
$"($env.FNM_MULTISHELL_PATH)"
} else {
$"($env.FNM_MULTISHELL_PATH)/bin"
}
$env.PATH = ($path | prepend [ $node_path ])
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 --silent-if-unchanged); (fnm-env | load-env)
}
}))
}
}
}

0 comments on commit 1b1b0e1

Please sign in to comment.