Skip to content

Conversation

@orzgithub
Copy link

Added instructions for using fnm with Clink.

Added extended instructions for clink.
@changeset-bot
Copy link

changeset-bot bot commented Mar 27, 2025

⚠️ No Changeset found

Latest commit: 00b5108

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@vercel
Copy link

vercel bot commented Mar 27, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
fnm ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 7, 2025 0:43am

Removed unnecessary match and fixed indent.
@bugficks
Copy link

Guess should have check PRs first... this is what I came up with:

  • tries to remove symlink on exit.
  • does doskey functionality in lua (haven't tested that though)
if os.getenv("FNM_AUTORUN_GUARD") == nil then
    os.setenv("FNM_AUTORUN_GUARD", "1")

    local function parse_fnm_env()
        local handle = io.popen('fnm env --use-on-cd 2>nul')
        if not handle then return nil end
        local out = handle:read("*a")
        handle:close()

        local env = {}
        for line in out:gmatch("[^\r\n]+") do
            -- Matches: set VAR=VALUE  (quotes rarely used, but handle if present)
            local var, value = line:match("^[Ss][Ee][Tt]%s+([^=]+)=(.*)$")
            if var and value then
                value = value:gsub('^"(.*)"$', '%1') -- remove surrounding quotes if present
                env[var:match("^%s*(.-)%s*$")] = value -- trim spaces from var
            end
            -- local line = line:match("^([Dd][Oo][Ss][Kk][Ee][Yy]%s+.*)$")
            -- if line then
            --     os.execute(line)
            -- end
        end
        return env, out
    end

    local function setup_fnm()
        local env, raw = parse_fnm_env()
        if not env or not raw then return end

        for var, value in pairs(env) do
            os.setenv(var, value)
        end
    end

    setup_fnm()
end

local function check_and_use_fnm()
    -- Check FNM_VERSION_FILE_STRATEGY environment variable
    local fnm_strategy = os.getenv("FNM_VERSION_FILE_STRATEGY")
    
    -- Check if we should use recursive strategy
    if fnm_strategy == "recursive" then
        os.execute("fnm use --silent-if-unchanged")
    else
        -- Check for .nvmrc file
        local nvmrc = io.open(".nvmrc", "r")
        if nvmrc ~= nil then
            io.close(nvmrc)
            os.execute("fnm use --silent-if-unchanged")
        else
            -- Check for .node-version file
            local node_version = io.open(".node-version", "r")
            if node_version ~= nil then
                io.close(node_version)
                os.execute("fnm use --silent-if-unchanged")
            end
        end
    end
end

-- remove fnm symlinks on exit
clink.onendedit(function(line)
    if line:match("^exit") then
        local path = os.getenv("FNM_MULTISHELL_PATH")
        if path then
            os.rmdir(path)
        end
    end
end)


-- doskey replacement
clink.onbeginedit(check_and_use_fnm)

@bugficks
Copy link

bugficks commented Jun 1, 2025

I've updated the script: this gist includes working doskey replacement and functions correctly even when launching the terminal using “Open Prompt Here” from Windows Explorer.

@orzgithub
Copy link
Author

I've updated the script: this gist includes working doskey replacement and functions correctly even when launching the terminal using “Open Prompt Here” from Windows Explorer.

Thanks! That's really a better solution. So what about open a pr for this project? If I merged these into my code manually, there would be no trace of you in the commit log (thanks via comments would probably not be appropriate here).

bugficks and others added 2 commits June 1, 2025 09:42
- doskey replacement with lua implementation
- remove symlink on exit
improved clink lua script:
@Schniz
Copy link
Owner

Schniz commented Jul 20, 2025

excuse me but what is clink

@orzgithub
Copy link
Author

excuse me but what is clink

Here is it's GitHub repo: chrisant996/clink
Clink is a tool for cmd.exe on Windows.
It brings tab completion functionality similar to bash, zsh, etc. into cmd, and also useful features such as color prompts for input commands. This tool also provides a function that can set some scripts written in Lua to be executed at startup, thereby achieving a startup script capability that is much more flexible than modifying the registry. It is directly injected into the cmd process, which means that it does not rely on specific terminal software like cmder. It can be used with any terminal like Windows Terminal, Windows legacy terminal, wezterm, Fluent Terminal, terminal components of various IDEs, etc. (cmder can of course also be used).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants