diff --git a/modules/fnm/README.md b/modules/fnm/README.md new file mode 100644 index 000000000..f35fe0aab --- /dev/null +++ b/modules/fnm/README.md @@ -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 +``` diff --git a/modules/fnm/fnm.nu b/modules/fnm/fnm.nu new file mode 100644 index 000000000..0cad349b4 --- /dev/null +++ b/modules/fnm/fnm.nu @@ -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) + } + })) + } + } +}