italevski/env-diff

Check the difference between env files and actualize each other

1.0.1 2024-06-06 09:12 UTC

This package is auto-updated.

Last update: 2024-12-06 10:05:37 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score

EnvDiff is tool to compare environment keys to find the difference between .env files and actualize them.

Example cast

Installation

composer require tekill/env-diff

Manual running

Actualize variables

Compare .env with .env.dist and add missing variables to .env file.

php ./vendor/bin/env-diff actualize

Compare .env with .env.example and add missing variables to .env file.

php ./vendor/bin/env-diff actualize .env.example

Compare .env-target with .env.example and add missing variables to .env-target file.

php ./vendor/bin/env-diff actualize .env.example .env-target

If you want to delete outdated values just run command with -k=false option.

php ./vendor/bin/env-diff actualize -k=false

Show differences

Command has same interface, arguments and options.

Compare .env with .env.dist and show differences between them.

php ./vendor/bin/env-diff diff

Composer script

Add code block in composer.json:

"scripts": {
    "post-update-cmd": "LF\\EnvDiff\\Composer\\ScriptHandler::actualizeEnv"
}

The .env will then be created or updated by the composer script, to match the structure of the dist file .env.dist by asking you the missing variables.

By default, the dist file is assumed to be in the same place than the target .env file, suffixed by .dist. This can be changed in the configuration:

{
    "extra": {
        "lf-env-diff": [
            {
                "dist": "path/to/env.dist",
                "target": "path/to/.env"
            }
        ]
    }
}

The script handler will ask you interactively for variables which are missing in the target env file, using the value of the dist file as default value. If composer is run in a non-interactive mode --no-interaction, the values of the dist file will be used for missing variables.

Warning: This handler will overwrite any comments or spaces into your target .env file so handle with care.

Managing multiple ignored files

The handler can manage multiple ignored files. To use this feature, the lf-env-diff extra should contain a JSON array with multiple configurations inside it instead of a configuration object:

{
    "extra": {
        "lf-env-diff": [
            {
                 "dist": "path/to/.env.dist",
                 "target": "path/to/.env"
            },
            {
                 "dist": "path/to/.env.dist",
                 "target": "path/to/.env-test",
                 "keep-outdated": false
            }
        ]
    }
}

Show difference

Add code block in composer.json:

"scripts": {
    "post-update-cmd": "LF\\EnvDiff\\Composer\\ScriptHandler::showDifference"
}

This handler has same behavior as described before.

Git hooks

You can use Git hook that gets triggered after any 'git pull' whenever one of the files specified has changed. Useful to update any web application dependency or sync configuration.

Create post-merge hook in .git/hooks directory of your project:

#/usr/bin/env bash

changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"

check_run() {
  echo "$changed_files" | grep -E --quiet "$1" && eval "$2"
}

# Actualize env files if the `env.dist` file gets changed
check_run env.dist "php ./vendor/bin/env-diff actualize"