Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

nushell/modules/commands.nu

Raw
# Tiny interactive shell commands and plain-external wrappers.
use lib.nu save-if-changed

const SCOPED_EXTERNALS = [
    bun codex claude herdr topgrade mise npm cargo go zig just make mvn mvnd gradle node
]
const WORK_SCOPE_MODULE = path self work_scope.nu

# Keep custom command modules in charge of their own external-launch boundary.
export def generate-scoped-externals [] {
    for name in $SCOPED_EXTERNALS {
        if not ($name =~ '^[A-Za-z0-9_.+-]+$') {
            print --stderr $"Invalid scoped external name: ($name)"
            exit 1
        }
        let prefix = $"($name) "
        let commands = (scope commands | where {|cmd| $cmd.name == $name or ($cmd.name | str starts-with $prefix)} | get name)
        let aliases = (scope aliases | where name == $name | get name)
        if ($commands | is-not-empty) or ($aliases | is-not-empty) {
            print --stderr $"Refusing to wrap ($name); existing Nu definitions: commands=($commands | to nuon), aliases=($aliases | to nuon)"
            exit 1
        }
    }

    let autoload_dir = ($nu.user-autoload-dirs | last)
    mkdir $autoload_dir
    let wrappers = $SCOPED_EXTERNALS | each {|name|
        [
            "@complete external"
            $"def --wrapped ($name | to nuon) [...args] {"
            $"    work_scope ($name | to nuon) ...$args"
            "}"
        ] | str join (char nl)
    }
    let body = [
        "# generated by commands.nu; edit SCOPED_EXTERNALS there instead"
        $"use ($WORK_SCOPE_MODULE | to nuon)"
        ""
        ($wrappers | str join (char nl))
    ] | str join (char nl)
    save-if-changed ($autoload_dir | path join '90-work-scope.nu') $body
}

export def "&" [cmd: string, ...args] {
    job spawn --description $cmd { run-external $cmd ...$args }
}

# Luci: bugabinga.net ci
# luci exposes cli via ssh.
# my ssh config is already setup for easy and safe access.
# this wrapper simply delegates to luci via ssh.
export def luci [...args] { ssh ci ...$args }

# Remove a literal NUL entry.
export def rm-nul [target: path = "NUL"] {
    if not $env.WIN32 {
        error make {msg: "rm-nul is only available on Windows"}
    }

    let slash = "\u{5c}"
    # `\\?\` is parsed as a glob by Nu because of its `?`; `\\.\` reaches Win32 literally.
    let target = $slash + $slash + "." + $slash + ($target | path expand) | into string
    rm --permanent $target
}