diff --git a/README.md b/README.md index 2a586ee..dba4490 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ ![image](https://github.com/user-attachments/assets/ad617e05-5d45-4b2c-a6b9-5cd095719fa3) - [![Latest Version on Packagist](https://img.shields.io/packagist/v/msamgan/laravel-env-keys-checker.svg?style=flat-square)](https://packagist.org/packages/msamgan/laravel-env-keys-checker) [![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/msamgan/laravel-env-keys-checker/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/msamgan/laravel-env-keys-checker/actions?query=workflow%3Arun-tests+branch%3Amain) [![Total Downloads](https://img.shields.io/packagist/dt/msamgan/laravel-env-keys-checker.svg?style=flat-square)](https://packagist.org/packages/msamgan/laravel-env-keys-checker) @@ -14,10 +13,30 @@ and you want to make sure that all the keys are available across all the .env fi With a team of developers, it is possible that some developers might forget to add the keys they used in their .env file to the .env.example file or the other way around. +## Table of Contents + +- [Features](#features) +- [Installation](#installation) +- [Usage](#usage) + - [To check if all the keys are available across all the .env files.](#to-check-if-all-the-keys-are-available-across-all-the-env-files) + - [To check if the .env and other provided files are present in .gitignore.](#to-check-if-the-env-and-other-provided-files-are-present-in-gitignore) +- [In Test](#in-test) + - [To check if all the keys are available across all the .env files.](#to-check-if-all-the-keys-are-available-across-all-the-env-files-1) + - [To check if the .env and other provided files are present in .gitignore.](#to-check-if-the-env-and-other-provided-files-are-present-in-gitignore-1) +- [Configuration](#configuration) +- [Testing](#testing) +- [Changelog](#changelog) +- [Contributing](#contributing) +- [Security Vulnerabilities](#security-vulnerabilities) +- [Credits](#credits) +- [License](#license) + ## Features - Check if all the keys are available across all the .env files. - Add the missing keys to the .env files automatically (configurable) considering the line numbers and empty lines. +- Check if the .env and other provided files are present in .gitignore, so that they are not committed to git by + mistake. ## Installation @@ -35,13 +54,24 @@ php artisan vendor:publish --tag="env-keys-checker-config" ## Usage +### To check if all the keys are available across all the .env files. + ```bash php artisan env:keys-check ``` +### To check if the .env and other provided files are present in .gitignore. + +```bash +php artisan env:in-git-ignore +``` + ## In Test -You can also use this package in your test cases to make sure that all the keys are available across all the .env files. +You can also use this package in your test cases to make sure the required feature is working as expected. + +### To check if all the keys are available across all the .env files. + Add the following code to your test case. Make sure to add ``--auto-add=none`` to override the default configuration. @@ -52,6 +82,16 @@ it('tests that the .env key are same across all .env files.', function () { }); ``` +### To check if the .env and other provided files are present in .gitignore. + +Add the following code to your test case. + +```php +it('tests that the .env and other provided files are present in .gitignore.', function () { + $this->artisan('env:in-git-ignore')->assertExitCode(0); +}); +``` + ## Configuration You can configure the package by publishing the configuration file. @@ -80,6 +120,14 @@ You can configure the package by publishing the configuration file. 'auto_add' => 'ask', ``` +```php +# config/env-keys-checker.php +# List of all the .env.* files to be checked if they +# are present in the .gitignore file. + +'gitignore_files' => ['.env'], +``` + ## Testing ```bash diff --git a/composer.json b/composer.json index 7198cbe..7d23ab0 100644 --- a/composer.json +++ b/composer.json @@ -72,7 +72,7 @@ "extra": { "laravel": { "providers": [ - "Msamgan\\LaravelEnvKeysChecker\\LaravelEnvKeysCheckerServiceProvider" + "Msamgan\\LaravelEnvKeysChecker\\LaravelEnvKeysCheckerServiceProvider" ], "aliases": { "LaravelEnvKeysChecker": "Msamgan\\LaravelEnvKeysChecker\\Facades\\LaravelEnvKeysChecker" @@ -81,4 +81,4 @@ }, "minimum-stability": "dev", "prefer-stable": true -} \ No newline at end of file +} diff --git a/config/env-keys-checker.php b/config/env-keys-checker.php index 5001a7b..bafdced 100644 --- a/config/env-keys-checker.php +++ b/config/env-keys-checker.php @@ -12,4 +12,8 @@ // auto: will add the missing keys automatically // none: will not add the missing keys 'auto_add' => 'ask', + + // List of all the .env.* files to be checked if they + // are present in the .gitignore file + 'gitignore_files' => ['.env'], ]; diff --git a/src/Commands/EnvInGitIgnoreCommand.php b/src/Commands/EnvInGitIgnoreCommand.php new file mode 100644 index 0000000..ea74899 --- /dev/null +++ b/src/Commands/EnvInGitIgnoreCommand.php @@ -0,0 +1,44 @@ +error('!! .gitignore file not found.'); + + return self::FAILURE; + } + + $gitIgnoreContent = array_map('trim', file($gitIgnoreFile)); + + $filesToCheck = config('env-keys-checker.gitignore_files', ['.env']); + + $missingFiles = collect(); + collect($filesToCheck)->each(function ($file) use ($gitIgnoreContent, $missingFiles) { + if (! in_array($file, $gitIgnoreContent)) { + $missingFiles->push($file); + } + }); + + if ($missingFiles->isEmpty()) { + $this->info('=> All files are present in .gitignore file.'); + + return self::SUCCESS; + } + + $this->error('!! ' . $missingFiles->implode(', ') . ' file(s) not found in .gitignore file.'); + + return self::FAILURE; + } +} diff --git a/src/Commands/LaravelEnvKeysCheckerCommand.php b/src/Commands/KeysCheckerCommand.php similarity index 98% rename from src/Commands/LaravelEnvKeysCheckerCommand.php rename to src/Commands/KeysCheckerCommand.php index ef277e9..88c7568 100644 --- a/src/Commands/LaravelEnvKeysCheckerCommand.php +++ b/src/Commands/KeysCheckerCommand.php @@ -11,7 +11,7 @@ use Msamgan\LaravelEnvKeysChecker\Actions\CheckKeys; use Msamgan\LaravelEnvKeysChecker\Actions\GetKeys; -class LaravelEnvKeysCheckerCommand extends Command +class KeysCheckerCommand extends Command { public $signature = 'env:keys-check {--auto-add=}'; diff --git a/src/LaravelEnvKeysCheckerServiceProvider.php b/src/LaravelEnvKeysCheckerServiceProvider.php index 2366730..36622e2 100644 --- a/src/LaravelEnvKeysCheckerServiceProvider.php +++ b/src/LaravelEnvKeysCheckerServiceProvider.php @@ -2,7 +2,8 @@ namespace Msamgan\LaravelEnvKeysChecker; -use Msamgan\LaravelEnvKeysChecker\Commands\LaravelEnvKeysCheckerCommand; +use Msamgan\LaravelEnvKeysChecker\Commands\EnvInGitIgnoreCommand; +use Msamgan\LaravelEnvKeysChecker\Commands\KeysCheckerCommand; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -18,6 +19,7 @@ public function configurePackage(Package $package): void $package ->name('laravel-env-keys-checker') ->hasConfigFile() - ->hasCommand(LaravelEnvKeysCheckerCommand::class); + ->hasCommand(KeysCheckerCommand::class) + ->hasCommand(EnvInGitIgnoreCommand::class); } }