Hyper: Add Hyper CLI to user PATH without reboot needing

Created on 15 Apr 2018  路  10Comments  路  Source: vercel/hyper

We are currently adding Hyper CLI path to user PATH in Windows registry: https://github.com/zeit/hyper/blob/262eb8ad9b7b9b15351f331765151538d67a09e2/app/utils/cli-install.js#L49-L91

A caveat is that environment variables are cached and users should open and validate "Edit environment variables for your account" dialog to force a cache refresh (or simply reboot their workstation).

@Stanzilla gave a link that could help to improve this: https://stackoverflow.com/a/11955920

good first issue help wanted 鈱笍 CLI tool Windows

Most helpful comment

For Windows, what about using the refreshenv.cmd that chocolately includes? I ran it upon first opening of hyper and it worked excellently. So the install could run it or hyper could run on first open? (have not dug into the app so I'm not familiar with how exactly it would be best implemented).

https://github.com/chocolatey/choco/blob/master/src/chocolatey.resources/redirects/RefreshEnv.cmd

@echo off
::
:: RefreshEnv.cmd
::
:: Batch file to read environment variables from registry and
:: set session variables to these values.
::
:: With this batch file, there should be no need to reload command
:: environment every time you want environment changes to propagate

echo | set /p dummy="Reading environment variables from registry. Please wait... "

goto main

:: Set one environment variable from registry key
:SetFromReg
    "%WinDir%\System32\Reg" QUERY "%~1" /v "%~2" > "%TEMP%\_envset.tmp" 2>NUL
    for /f "usebackq skip=2 tokens=2,*" %%A IN ("%TEMP%\_envset.tmp") do (
        echo/set %~3=%%B
    )
    goto :EOF

:: Get a list of environment variables from registry
:GetRegEnv
    "%WinDir%\System32\Reg" QUERY "%~1" > "%TEMP%\_envget.tmp"
    for /f "usebackq skip=2" %%A IN ("%TEMP%\_envget.tmp") do (
        if /I not "%%~A"=="Path" (
            call :SetFromReg "%~1" "%%~A" "%%~A"
        )
    )
    goto :EOF

:main
    echo/@echo off >"%TEMP%\_env.cmd"

    :: Slowly generating final file
    call :GetRegEnv "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" >> "%TEMP%\_env.cmd"
    call :GetRegEnv "HKCU\Environment">>"%TEMP%\_env.cmd" >> "%TEMP%\_env.cmd"

    :: Special handling for PATH - mix both User and System
    call :SetFromReg "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" Path Path_HKLM >> "%TEMP%\_env.cmd"
    call :SetFromReg "HKCU\Environment" Path Path_HKCU >> "%TEMP%\_env.cmd"

    :: Caution: do not insert space-chars before >> redirection sign
    echo/set Path=%%Path_HKLM%%;%%Path_HKCU%% >> "%TEMP%\_env.cmd"

    :: Cleanup
    del /f /q "%TEMP%\_envset.tmp" 2>nul
    del /f /q "%TEMP%\_envget.tmp" 2>nul

    :: Set these variables
    call "%TEMP%\_env.cmd"

    echo | set /p dummy="Done"
    echo .

All 10 comments

process.env.Path = process.env.Path + ';' + binPath ?

Would make it work the first time Hyper launches, granted, not outside of it, but is that that important?

I think that's the better workaround @Stanzilla 馃憤

I have just installed and used hyper for the first time.
I open the env variables dialog box. No variable added there (no reboot yet)
I added one my-self (tried both to %USERPROFILE%\AppData\Localhyper and with \app-)
It now "sees" the hyper command, but if I run hyper i it will just open a new terminal (nothing installed ( i can install plugins by adding in the hyper.js file)

Anything I am missing, or its a know issue?

@buffos Hyper CLI is only available on our canary release (upcoming v2)
You don't have to add anything by yourself :)

FYI I created a more generic version of this issue back in 2016 馃槃 https://github.com/zeit/hyper/issues/1211, maybe you can close it (https://github.com/zeit/hyper/issues/2284#issuecomment-359806862)?

@Stanzilla I think this is important to have hyper in PATH (outside itself). I think that hyper --verbose will be used to make some diagnostics for example.

@chabou my idea was mainly meant as a workaround, since we can't easily refresh the environment at Hyper launch BUT we can add it to process.env temporarily. That change gets lost as soon as Hyper closes. We would not drop setting the registry change for it, though.

That means we have the following scenario:

Current:

User installs Hyper
Hyper gets added to PATH
Hyper opens, cannot refresh the env so hyper cli does not work
User opens different terminal emulator
hyper cli also does not work if said terminal emulator did not refresh env.

After the change:

User installs Hyper
Hyper gets added to PATH
Hyper opens, temporarily adds hyper to process.env so hyper cli works
User opens different terminal emulator
hyper cli also does not work if said terminal emulator did not refresh env.

I don't really see a downside to it?

For Windows, what about using the refreshenv.cmd that chocolately includes? I ran it upon first opening of hyper and it worked excellently. So the install could run it or hyper could run on first open? (have not dug into the app so I'm not familiar with how exactly it would be best implemented).

https://github.com/chocolatey/choco/blob/master/src/chocolatey.resources/redirects/RefreshEnv.cmd

@echo off
::
:: RefreshEnv.cmd
::
:: Batch file to read environment variables from registry and
:: set session variables to these values.
::
:: With this batch file, there should be no need to reload command
:: environment every time you want environment changes to propagate

echo | set /p dummy="Reading environment variables from registry. Please wait... "

goto main

:: Set one environment variable from registry key
:SetFromReg
    "%WinDir%\System32\Reg" QUERY "%~1" /v "%~2" > "%TEMP%\_envset.tmp" 2>NUL
    for /f "usebackq skip=2 tokens=2,*" %%A IN ("%TEMP%\_envset.tmp") do (
        echo/set %~3=%%B
    )
    goto :EOF

:: Get a list of environment variables from registry
:GetRegEnv
    "%WinDir%\System32\Reg" QUERY "%~1" > "%TEMP%\_envget.tmp"
    for /f "usebackq skip=2" %%A IN ("%TEMP%\_envget.tmp") do (
        if /I not "%%~A"=="Path" (
            call :SetFromReg "%~1" "%%~A" "%%~A"
        )
    )
    goto :EOF

:main
    echo/@echo off >"%TEMP%\_env.cmd"

    :: Slowly generating final file
    call :GetRegEnv "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" >> "%TEMP%\_env.cmd"
    call :GetRegEnv "HKCU\Environment">>"%TEMP%\_env.cmd" >> "%TEMP%\_env.cmd"

    :: Special handling for PATH - mix both User and System
    call :SetFromReg "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" Path Path_HKLM >> "%TEMP%\_env.cmd"
    call :SetFromReg "HKCU\Environment" Path Path_HKCU >> "%TEMP%\_env.cmd"

    :: Caution: do not insert space-chars before >> redirection sign
    echo/set Path=%%Path_HKLM%%;%%Path_HKCU%% >> "%TEMP%\_env.cmd"

    :: Cleanup
    del /f /q "%TEMP%\_envset.tmp" 2>nul
    del /f /q "%TEMP%\_envget.tmp" 2>nul

    :: Set these variables
    call "%TEMP%\_env.cmd"

    echo | set /p dummy="Done"
    echo .

Hey just wanted to check if this was in 3 or if I needed to do a manual refresh of my env variables in Windows?

Is this issue still open? I am first time contributer.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alejandrofloresm picture alejandrofloresm  路  3Comments

sarneeh picture sarneeh  路  3Comments

ConstantinChirila picture ConstantinChirila  路  3Comments

eoinmurray picture eoinmurray  路  3Comments

cooperpellaton picture cooperpellaton  路  3Comments