Git Product home page Git Product logo

sync-dotenv's Introduction

sync ⚙️ dotenv

Keep your .env in sync with .env.example

Travis (.org) Coveralls github

Motivation

Projects often rely on environmental variables stored in a .env file to run... and because these variables sometimes contain sensitive data, we never add them to source control. Instead, these variables are added e.g. to a .env.example file so it's easy to get the project running for other developers. However, it's very easy to forget to update this file when a variable is added/updated in .env (during development). This can make it difficult for devs to get the project running (locally) because they rely on .env.example file to setup their environment (with their own configs).

Enter sync-dotenv 🔥

Description

sync-dotenv automates the process of keeping your .env in sync with .env.example.

Installation

$ npm install -g sync-dotenv

Install as a dev dependency (recommended)

$ npm install -D sync-dotenv

Usage

By default, sync-dotenv looks for a .env in your working directory and attempt to sync with .env.example when no argument is provided. Failure to find these files will cause the sync to fail.

$ sync-dotenv

Alternatively, you can use the --env and --sample flag to specify the source and destination file.

$ sync-dotenv --env foo/.env --sample bar/.env.example

Also, in the situation where you want to keep multiple files in sync with one source env file you can make use of the --samples flag specifying a globbing pattern to match:

$ sync-dotenv --env foo/.env --samples "env-samples/*"

# note: glob pattern should be provided as a string as shown above

For CLI options, use the --help flag

$ sync-dotenv --help

To run sync-dotenv whenever the .env file changes, you can for example use nodemon:

$ nodemon --watch .env --exec 'sync-dotenv --env .env --sample .env.example'

Examples

Sync (with .env.example) before every commit using husky

// package.json
{
  "scripts": {
    "env": "sync-dotenv"
  },
  "husky": {
    "hooks": {
      "pre-commit": "npm run env",
    }
  }
}

Or with file other than .env.example

{
  "scripts": {
-    "env": "sync-dotenv"
+    "env": "sync-dotenv --sample .env.development"
  }
}

Preserving variables in sample env

Sometimes you need to preserve certain variables in your example env file, you can optionally allow this by adding a sync-dotenv config in package.json like so

// package.json
"scripts": {
  ...
},
"sync-dotenv": {
  "preserve": ["CHANNEL"]
}

Avoid comments or empty lines in sample env

You might not want to copy empty lines or comments to your sample env, in this case you can still use sync-dotenv config in package.json with the following:

// package.json
"scripts": {
  ...
},
"sync-dotenv": {
  "emptyLines": true,
  "comments": false
}

Note that you can still combine those options with preserve.

Related

  • parse-dotenv - zero dependency .env to javascript object parser

Contributors

Thanks goes to these wonderful people (emoji key):

Luqman Olushi O.
Luqman Olushi O.

💻 📖 🚧 📦 ⚠️
Bolaji Olajide
Bolaji Olajide

💻
Kizito Akhilome
Kizito Akhilome

💻 ⚠️ 📖

This project follows the all-contributors specification. Contributions of any kind welcome!

License

This project is licensed under MIT

sync-dotenv's People

Contributors

akhilome avatar allcontributors[bot] avatar bolajiolajide avatar cmckni3 avatar danieldspx avatar dependabot[bot] avatar luqmanoop avatar x3rax avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sync-dotenv's Issues

Keys deleted from .env.example when not in .env

Hi @codeshifu , I have this problem.

Sometimes I keep some keys in .env.example for documentation but not in .env because I use defaults or something.

So, if you have keys in .env.example but not in .env, when you sync they are deleted from .env.example.

Example

.env:

BOTH=1
ENV=0

.env.example:

BOTH=2
EXAMPLE=42
ANOTHER=24
ENV=3

run sync-dotenv then

.env.example:

BOTH=2
ENV=3

The keys EXAMPLE and ANOTHER were deleted.

Thanks

Preserving variables in sample env ?

Hello

It's not clear from the README but what does this do...

"sync-dotenv": {
  "preserve": ["CHANNEL"]
}

Is "CHANNEL" a variable name that is to be preserved ?

Thanks,
Nick

[Suggestion]: Support for syncing multiple `.env.*` files

Thanks for this great library. It solves an issue we've had with keeping .env files in sync and we're super glad we've found it.

We are wondering if it would be of interest for the library to support "fan-out" to multiple .env files from one .env.template.

Our setup often looks like:

.env.template
.env.local
.env.staging
.env.production

this helps us simulating and configuring interaction with different parts of the system. We would love to sync from the .env.template to all other env files. This would require, to my understanding, some globbing (in the root) by the library instead of relying on one file.

Thanks.

"TypeError: Cannot read property 'inherits' of undefined" in version 2.5.0

First things first: Great work, thanks for this tool 😃

I encounter the following error when I try to run sync-dotenv in version 2.5.0:

TypeError: Cannot read property 'inherits' of undefined                                                            │
    at Object.<anonymous> (/home/x3ro/.local/opt/n/lib/node_modules/sync-dotenv/dist/index.js:2:16669)             │found 0 vulnerabilities
    at Module._compile (internal/modules/cjs/loader.js:1147:30)                                                    │
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)                                      │x3ro@K1STE:~/Projects/x3ro/hacker-news-bot$ ^C
    at Module.load (internal/modules/cjs/loader.js:996:32)                                                         │x3ro@K1STE:~/Projects/x3ro/hacker-news-bot$ tail -f .env.example 
    at Function.Module._load (internal/modules/cjs/loader.js:896:14)                                               │ # Copy this file to `.env` and change values as needed.
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)                            │
    at internal/main/run_main_module.js:17:47

I tried installing it globally, in a project and even in an empty project.

Installing [email protected] works flawlessly.

Strange behaviour with KEY=#value

I have to store an env variable with a slack channel starting with # character.

Scenario 1:

.env:

# Default channel
CHANNEL=#general

.env.example:

# Default channel
CHANNEL=#random

run sync-dotenv then

.env.example:

CHANNEL= #general

Comment was deleted and value was overwritten with .env value and a spaces was added.

Scenario 2:

If I use quotes (singles or doubles)

.env:

# Default channel
CHANNEL='#general'

.env.example:

# Default channel
CHANNEL='#random'

run sync-dotenv then

.env.example:

CHANNEL= 

It deletes both, comment and value from key.

So I can't keep my .env.example with a useful example.

It would be great if you could consider this case.

Thanks

Update globby due to deprecated dependencies

Please update globby to at least 10.0.

So ever since those packages were deprecated 2 months ago, when I run npm install with sync-dotenv 2.7.0 as a dependency, I get these deprecation warnings:

$ npm install

npm WARN deprecated [email protected]: See https://github.com/lydell/source-map-url#deprecated
npm WARN deprecated [email protected]: Please see https://github.com/lydell/urix#deprecated
npm WARN deprecated [email protected]: https://github.com/lydell/resolve-url#deprecated
npm WARN deprecated [email protected]: See https://github.com/lydell/source-map-resolve#deprecated

sync-dotenv is using globby.sync which has the same input and output from 9.2.0 up until 12.0.0, at which point globby.sync became globbySync.

Summary

globby 9.2 could be updated to 10 or 11 without API breaking changes. It could be updated to 13.1 with 1 breaking change, which would simply require importing { globbySync } instead of globby, and calling globbySync directly instead of calling globby.sync.

I will open a pull request to update globby to 11.1 without the breaking change.

undefined not found

If no .env.example file is already created, the script just says undefined not found. I expected it to create the file automatically if it didn't already exist.

Symfony .env & .env.local

Hi ,

In symfony 4.3 they changed the way to use .env files , now .env file are the example & are commited into repo & we have to overwrite them with .env.local.

So to use sync-dotenv we should be able to indicate source / destination .

I will do a PR if got some time

Cannot use 'example.env' as example file name

Not a big deal, of course, but it's a tiny issue nonetheless -

Currently, any sample filename provided via the CLI that ends with .env throws an error.
While I understand the reasoning - can't sync .env itself with .env - my team uses example.env as the filename for the example file (as if to say, "here's an example .env file").
Would it be OK to change the check for exclusively .env files - not all files that end with .env, or is this a matter of policy (or a best practice I'm unaware of)?

Thanks for your help!

'=' characters present in values get deleted, along with any text following

Take this .env file:
MY_URL=https://my.website.com?this=that

With a .env.example equivalent of:
MY_URL=

After processing with sync-dotenv this is changed to:
MY_URL=https://my.website.com?this

Whatever is parsing the = for delimiting Key and Value appears to be doing something else for any subsequent = occurrences.

Weird empty line entries after tenth new line

No matter how I order the .env file always it always gets screwed up after the tenth extra new line. The following gets inserted instead of new lines: __EMPTYLINE_10__= __EMPTYLINE_11__= etc instead of a blank line.

As a convoluted example:

APP_NAME=

APP_ENV=

APP_KEY=

APP_DEBUG=

APP_URL=

LOG_CHANNEL=

DB_CONNECTION=

DB_HOST=

DB_PORT=

DB_DATABASE=

DB_USERNAME=

DB_PASSWORD=

OTHER_ENV=

Gets synced as:

APP_NAME=

APP_ENV=

APP_KEY=

APP_DEBUG=

APP_URL=

LOG_CHANNEL=

DB_CONNECTION=

DB_HOST=

DB_PORT=

DB_DATABASE=
__EMPTYLINE_10__=
DB_USERNAME=
__EMPTYLINE_11__=
DB_PASSWORD=
__EMPTYLINE_12__=

Not sure if its on my end or the scripts end but was hoping you might have some answers

[Feature Proposal]: Opt-in Preservation of Public Variables in .env to .example.env Syncing

Feature Proposal: Enhancements for Syncing .env Files

Objective:

Improve the functionality of our CLI tool that synchronises .env files to .example.env files by providing an escape mechanism to preserve specific values for public variables.

Problem:

Currently, all variables in the .example.env file exist as empty strings after the syncing process. It would be helpful for public variables to retain their values.

Proposed Solution:

I thought of two way to achive this:

  1. Public Prefix Preservation: Allow users to specify a custom or default prefix for variable names that they want to remain public. Any variable with this prefix will retain its value during the sync process.

    CLI Example:

    sync-dotenv --preserve-public-prefix=NEXT_PUBLIC

    In this example, any variable starting with NEXT_PUBLIC will keep its original value in the .example.env file.

    Default behavior (when flag is used without a value): An error will be thrown requesting for a prefix input.

  2. Keyword Comment Preservation: Allow users to mark variables for preservation using a default or custom keyword comment at the end of the line.

    .env file Example:

    SENTRY_URL='https://example.ingest.sentry.io/example' #SYNC_VALUE

    CLI Example:

    sync-dotenv --preserve-keyword-comment=SYNC_VALUE

    In this example, any variable line ending with #SYNC_VALUE will keep its original value in the .example.env file.

    Default behavior (when flag is used without a value): An error will be thrown requesting for a keyword input.

Considerations:

  1. Need to ensure these changes are well documented in the CLI help text.

  2. Need to provide examples in the readme for the new flag and potential use cases.

  3. Error handling needs to be designed carefully to provide meaningful error messages, especially when the new flags are used without necessary inputs.

Reverse sync?

I have a use-case to sync .env and .env.example (or .env.dist in my case) in the opposite direction - pushing updates on the .env file whenever .env.dist additions are made. Not the immediate use-case you cover, but it feels possible and equivalent.

Currently this fails, due to some check on the .env file name. Is this check necessary?

> sync-dotenv --env .env.dist --sample .env

Cannot sync .env with .env

Synchronize Comments

Dear @codeshifu ,

thanks for this wonderful package - i really like it.
I recently stumbled upon the issue, that i have comments in my .env file. I would like to synchronize these comments with my .env.example file.

Would you consider adding this feature to your library?
Thanks a lot!

'=' characters present in values get deleted, along with any text following

Take this .env file:
MY_URL=https://my.website.com?this=that

With a .env.example equivalent of:
MY_URL=

After processing with sync-dotenv this is changed to:
MY_URL=https://my.website.com?this

Whatever is parsing the = for delimiting Key and Value appears to be doing something else for any subsequent = occurrences.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.