Skip to content

feature: add --dotenv arg#327

Closed
bkniffler wants to merge 1 commit intogoogle:mainfrom
bkniffler:main
Closed

feature: add --dotenv arg#327
bkniffler wants to merge 1 commit intogoogle:mainfrom
bkniffler:main

Conversation

@bkniffler
Copy link
Copy Markdown

Not sure if you desire to have this inbuilt, but to me its pretty much essential to preload env files before running commands. I'm sure others would welcome this.

Feature

Add an argument to load .env files prior to running the script.

Issues

NPM packages people seemed to publish to add this feature

Done

  • Tests pass
  • Appropriate changes to README are included in PR

@antonmedv
Copy link
Copy Markdown
Collaborator

Can you share use case? Seems very specific.

@bkniffler
Copy link
Copy Markdown
Author

e.g. setup and helper scripts in a project with environment specific or secret configurations (e.g. db secrets, NODE_ENV, ports, etc).

// Connect and expose a machines port to local
void (async function () {
  await $`ssh -L 8899:$PORT $HOST`;
})();

or doing stuff depending on NODE_ENV

void (async function () {
  if (process.env.NODE_ENV === 'development') 
      ...;
  else
      ...;
})();

etc.

So something like zx migrate-data.js --dotenv with a .env file containing the database address, port and password ist super convenient.

@antonmedv
Copy link
Copy Markdown
Collaborator

As no more interest was shown, let’s not merge it now.

Probably we can discuss adding such feature later.

@antonmedv antonmedv closed this Mar 15, 2022
@bkniffler
Copy link
Copy Markdown
Author

Alright, in case anyone needs it, I've pushed my version with this PR here: https://www.npmjs.com/package/zxenv

@Lp-Francois
Copy link
Copy Markdown

Hello! I am really interested to use a .env file as we don't want to commit databases credentials and they are the same as the ones already present in the .env of the project where the zx script is used.

For the moment I'll probably use this work-around: #119 (comment)
But it would be really nice to have an option to import .env values 🙏
Like require('dotenv').config(); at the beginning.

In a bash script I used to do:

# Load .env
export $(egrep -v '^#' .env | xargs)

@pvinis
Copy link
Copy Markdown

pvinis commented Mar 30, 2022

This is great, please reopen.

@joselcvarela
Copy link
Copy Markdown

I really feel this should be supported.
For example, you want to create a script to:

  • fetch data from a location which requires an API key or an access token
  • connect to a remote database which requires credentials
  • remote access (ssh) which also requires credentials

Those all should come from environment variables if you want to track these files.
Also, since this is a scripting tool it makes total sense to have this feature.

Although I believe it should auto detect any .env in the same folder of script and pass --env or --dotenv when .env is in another location

@antonmedv
Copy link
Copy Markdown
Collaborator

What about just adding to the script:

import 'dotenv/config'

That's it. Nothing else is required.

Although I believe it should auto detect any .env in the same folder of script

Definitely not support this. Things should be explicit.

@joselcvarela
Copy link
Copy Markdown

joselcvarela commented Jun 28, 2022

What about just adding to the script:

import 'dotenv/config'

This requires to have a package.json, node_modules folder and all the boilerplate that comes with npm.
We are in environment where everyone has zx installed on their machines and can just create a script anywhere.
If we need to create a package.json there's no need to use zx.

@antonmedv
Copy link
Copy Markdown
Collaborator

We are in environment where everyone has zx installed on their machines and can just create a script anywhere.

Interesting. Can you tell little bit more?

@joselcvarela
Copy link
Copy Markdown

We are in environment where everyone has zx installed on their machines and can just create a script anywhere.

Interesting. Can you tell little bit more?

For example, we have a folder structure which is related to DevOps.
In order to automate of some tedious tasks we use zx. Although, sometimes we need to fetch data from a service and zx already provides fetch which is great. But this service requires an API key and we don't want to commit this file with this API key for obvious reasons.

@antonmedv
Copy link
Copy Markdown
Collaborator

I see. This makes sense.

@antonmedv antonmedv reopened this Jun 28, 2022
@antonmedv antonmedv closed this Jun 28, 2022
@antonmedv
Copy link
Copy Markdown
Collaborator

Let’s open a new issue and discuss implementation there c

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.

5 participants