toddbluhm / env-cmd

Setting environment variables from a file
https://www.npmjs.com/package/env-cmd
MIT License
1.74k stars 66 forks source link

Does this work on windows? #238

Open amitrai99 opened 3 years ago

amitrai99 commented 3 years ago

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

mvarendorff commented 3 years ago

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?

phil-lgr commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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
TomasMorton commented 1 year ago

Thanks for the patch @phil-lgr