diff --git a/nushell/config.nu b/nushell/config.nu index fe7b758..4a2518a 100644 --- a/nushell/config.nu +++ b/nushell/config.nu @@ -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 diff --git a/nushell/scripts/fnm.nu b/nushell/scripts/fnm.nu index e3fa781..171f995 100644 --- a/nushell/scripts/fnm.nu +++ b/nushell/scripts/fnm.nu @@ -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) + } + })) + } + } }