Skip to content

nushell env wrapper #1144

@joknoll

Description

@joknoll

Description

As a developer using Vite+ I want to be able to source vp into nushell so that I can still use my main shell.

This includes wrapping the env use command and completions for vp.

Suggested solution

I've already implemented a env.nu that can be sourced in nushell like so 'source ~/.vite-plus/env.nu'

# Custom completer for the vp wrapper using fish completions
def "nu-complete vp" [context: string] {
    let fish_comp_path = ($env.HOME | path join ".vite-plus/completion/vp.fish")
    let fish_cmd = $"source '($fish_comp_path)'; complete '--do-complete=($context)'"
    fish --command $fish_cmd | from tsv --flexible --noheaders --no-infer | rename value description | update value {|row|
        let value = $row.value
        let need_quote = ['\' ',' '[' ']' '(' ')' ' ' '\t' "'" '"' "`"] | any {$in in $value}
        if ($need_quote and ($value | path exists)) {
            let expanded_path = if ($value starts-with ~) {$value | path expand --no-symlink} else {$value}
            $'"($expanded_path | str replace --all "\"" "\\\"")"'
        } else {$value}
    }
}
export def --env --wrapped vp [...args: string@"nu-complete vp"] { match $args {
    ["env", "use", ..] => {
        let out = (with-env { VITE_PLUS_ENV_USE_EVAL_ENABLE: "1" } {
                ^vp ...$args
            })
        let out_lines = ($out | lines)
        let exports = (
            $out_lines | find -r '^export ' | parse -r '^export (?P<key>\w+)=["'']?(?P<value>[^"'']+)["'']?' | reduce -f {} {|it, acc| $acc | insert $it.key $it.value}
        )
        if not ($exports | is-empty) {
            load-env $exports
        }
        let unsets = (
            $out_lines | find -r '^unset ' | parse -r '^unset (?P<key>\w+)' | get key
        )
        for key in $unsets { hide-env $key }
    }
    _ => { ^vp ...$args }
} }

Alternative

No response

Additional context

No response

Validations

  • Read the Contributing Guidelines.
  • Confirm this request is for Vite+ itself and not for Vite, Vitest, tsdown, Rolldown, or Oxc.
  • Check that there isn't already an issue requesting the same feature.

Metadata

Metadata

Assignees

No one assigned

    Priority

    None yet

    Start date

    None yet

    Target date

    None yet

    Effort

    None yet

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions