Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add --dotenv arg #327

Closed
wants to merge 1 commit into from
Closed

feature: add --dotenv arg #327

wants to merge 1 commit into from

Conversation

bkniffler
Copy link

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
Collaborator

Can you share use case? Seems very specific.

@bkniffler
Copy link
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
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
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

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

pvinis commented Mar 30, 2022

This is great, please reopen.

@joselcvarela
Copy link

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
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

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
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

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
Collaborator

I see. This makes sense.

@antonmedv antonmedv reopened this Jun 28, 2022
@antonmedv antonmedv closed this Jun 28, 2022
@antonmedv
Copy link
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.

None yet

5 participants