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

fix(fnm): Automatic version switching failed. #1064

Merged
merged 1 commit into from
Mar 7, 2025
Merged
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
94 changes: 47 additions & 47 deletions modules/fnm/fnm.nu
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
export-env {
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 | slice 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)

return $env_vars
}

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

$env.config = (
$env.config?
| default {}
| upsert hooks { default {} }
| upsert hooks.env_change { default {} }
| upsert hooks.env_change.PWD { default [] }
)
let __fnm_hooked = (
$env.config.hooks.env_change.PWD | any { try { get __fnm_hook } catch { false } }
)
if not $__fnm_hooked {
$env.config.hooks.env_change.PWD = ($env.config.hooks.env_change.PWD | append {
__fnm_hook: true,
code: {|before, after|
if ('FNM_DIR' in $env) and ([.nvmrc .node-version] | path exists | any { |it| $it }) {
(^fnm use); (fnm-env | load-env)
}
}
})
}
}
}
export-env {
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 | slice 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)
return $env_vars
}
if not (which fnm | is-empty) {
fnm-env | load-env
$env.config = (
$env.config?
| default {}
| upsert hooks { default {} }
| upsert hooks.env_change { default {} }
| upsert hooks.env_change.PWD { default [] }
)
let __fnm_hooked = (
$env.config.hooks.env_change.PWD | any { try { get __fnm_hook } catch { false } }
)
if not $__fnm_hooked {
$env.config.hooks.env_change.PWD = (
$env.config.hooks.env_change.PWD | append {
__fnm_hook: true
code: {|before, after|
if ('FNM_DIR' in $env) and ([.nvmrc .node-version] | path exists | any {|it| $it }) {
^fnm use
}
}
}
)
}
}
}