env-cmd icon indicating copy to clipboard operation
env-cmd copied to clipboard

Does this work on windows?

Open amitrai99 opened this issue 4 years ago • 7 comments

I tried the basic example in the README and cant get it to run on Windows 10. Does this support Windows?

amitrai99 avatar Feb 19 '21 06:02 amitrai99

I am using env-cmd to run chromatic with the token defined in a .env file in the packages directory and it works without issues on my Windows 10 machine. Maybe a detailed error could help pinpoint the problem?

mvarendorff avatar Apr 12 '21 15:04 mvarendorff

On windows 10, running this:

env-cmd -x nr gatsby develop --verbose --port \$APP_PORT --host 0.0.0.0

should output:

env-cmd -x nr gatsby develop --verbose --port 9000 --host 0.0.0.0

but I get:

env-cmd -x nr gatsby develop --verbose --port \\$APP_PORT --host 0.0.0.0

I think env-cmd does not expand properly on powershell

phil-lgr avatar Oct 05 '21 19:10 phil-lgr

Simply doing:

    "start": "env-cmd -x nr gatsby develop --verbose --port $APP_PORT --host 0.0.0.0",

with no \\ work on powershell, but not on the mac terminal

There is something funny going on with \ on Windows and powershell

phil-lgr avatar Oct 05 '21 19:10 phil-lgr

Simpler test:

# on mac, works correctly
phil$ node_modules/.bin/env-cmd -x echo \$APP_PORT 
9000
# on windows, does not work
PS C:\Users\legep\> ..\..\node_modules\.bin\env-cmd -x echo \$APP_PORT 
"\\"

With verbose flag:

PS C:\Users\legep> ..\..\node_modules\.bin\env-cmd   --verbose  -x echo \$APP_PORT
Options: {"command":"echo","commandArgs":["\\"],"options":{"expandEnvs":true,"noOverride":false,"silent":false,"useShell":false,"verbose":true}}
Found .env file at default path: ./.env
"\\"
Child process exited with code: 0 and signal:. Terminating parent process...

phil-lgr avatar Oct 05 '21 19:10 phil-lgr

Potential fix for Windows, since \\$VAR does not get expanded like on unix:

diff --git a/node_modules/env-cmd/dist/expand-envs.js b/node_modules/env-cmd/dist/expand-envs.js
index b46324a..576a651 100644
--- a/node_modules/env-cmd/dist/expand-envs.js
+++ b/node_modules/env-cmd/dist/expand-envs.js
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
  * the environment variable doesn't exist, it leaves it as is.
 */
 function expandEnvs(str, envs) {
-    return str.replace(/(?<!\\)\$[a-zA-Z0-9_]+/g, varName => {
+    return str.replace('\\$', '$').replace(/(?<!\\)\$[a-zA-Z0-9_]+/g, varName => {
         const varValue = envs[varName.slice(1)];
         return varValue === undefined ? varName : varValue;
     });

Let me know if this makes sense, I could open a PR

@toddbluhm

phil-lgr avatar Oct 05 '21 20:10 phil-lgr

if you want to add windows to your actions:

jobs:
  build-test:
    runs-on: ${{ matrix.os }}
    strategy:
        fail-fast: false
        matrix:
            os: [ubuntu-latest, windows-latest]
    steps:
      - name: Checkout
        uses: actions/checkout@v2

phil-lgr avatar Oct 05 '21 20:10 phil-lgr

Thanks for the patch @phil-lgr

TomasMorton avatar Feb 22 '23 20:02 TomasMorton