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..89bf90e --- /dev/null +++ b/src/Commands/EnvInGitIgnoreCommand.php @@ -0,0 +1,47 @@ +showFailureInfo(message: '.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->showSuccessInfo(message: 'All files are present in .gitignore file.'); + + return self::SUCCESS; + } + + $this->showFailureInfo(message: $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 67% rename from src/Commands/LaravelEnvKeysCheckerCommand.php rename to src/Commands/KeysCheckerCommand.php index ef277e9..38ad2b9 100644 --- a/src/Commands/LaravelEnvKeysCheckerCommand.php +++ b/src/Commands/KeysCheckerCommand.php @@ -3,16 +3,21 @@ namespace Msamgan\LaravelEnvKeysChecker\Commands; use Illuminate\Console\Command; +use Illuminate\Support\Collection; use function Laravel\Prompts\confirm; +use function Laravel\Prompts\progress; use function Laravel\Prompts\table; use Msamgan\LaravelEnvKeysChecker\Actions\AddKeys; use Msamgan\LaravelEnvKeysChecker\Actions\CheckKeys; use Msamgan\LaravelEnvKeysChecker\Actions\GetKeys; +use Msamgan\LaravelEnvKeysChecker\Concerns\HelperFunctions; -class LaravelEnvKeysCheckerCommand extends Command +class KeysCheckerCommand extends Command { + use HelperFunctions; + public $signature = 'env:keys-check {--auto-add=}'; public $description = 'Check if all keys in .env file are present across all .env files. Like .env, .env.example, .env.testing, etc.'; @@ -28,13 +33,17 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys) $autoAddStrategy = $autoAddOption ?: config('env-keys-checker.auto_add', 'ask'); if (! in_array($autoAddStrategy, $autoAddAvailableOptions)) { - $this->error('!! Invalid auto add option provided. Available options are: ' . implode(', ', $autoAddAvailableOptions)); + $this->showFailureInfo( + message: 'Invalid auto add option provided. Available options are: ' . implode(', ', $autoAddAvailableOptions) + ); return self::FAILURE; } if (empty($envFiles)) { - $this->error('!! No .env files found.'); + $this->showFailureInfo( + message: 'No .env files found.' + ); return self::FAILURE; } @@ -46,32 +55,31 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys) $keys = $getKeys->handle(files: $envFiles); $missingKeys = collect(); - $keys->each(function ($keyData) use ($envFiles, $missingKeys, $checkKeys) { - $checkKeys->handle(keyData: $keyData, envFiles: $envFiles, missingKeys: $missingKeys); - }); + + progress( + label: 'Checking keys...', + steps: $keys, + callback: fn ($key) => $checkKeys->handle(keyData: $key, envFiles: $envFiles, missingKeys: $missingKeys), + hint: 'It won\'t take long.' + ); if ($missingKeys->isEmpty()) { - $this->info('=> All keys are present in across all .env files.'); + $this->showSuccessInfo( + message: 'All keys are present in all .env files.' + ); return self::SUCCESS; } - table( - headers: ['Line', 'Key', 'Is missing in'], - rows: $missingKeys->map(function ($missingKey) { - return [ - $missingKey['line'], - $missingKey['key'], - $missingKey['envFile'], - ]; - })->toArray() - ); + $this->showMissingKeysTable($missingKeys); if ($autoAddStrategy === 'ask') { $confirmation = confirm('Do you want to add the missing keys to the .env files?'); if ($confirmation) { $addKeys->handle(missingKeys: $missingKeys); + + $this->showSuccessInfo('All missing keys have been added to the .env files.'); } return self::SUCCESS; @@ -84,5 +92,20 @@ public function handle(GetKeys $getKeys, CheckKeys $checkKeys, AddKeys $addKeys) } return self::FAILURE; + + } + + private function showMissingKeysTable(Collection $missingKeys): void + { + table( + headers: ['Line', 'Key', 'Is missing in'], + rows: $missingKeys->map(function ($missingKey) { + return [ + $missingKey['line'], + $missingKey['key'], + $missingKey['envFile'], + ]; + })->toArray() + ); } } diff --git a/src/Concerns/HelperFunctions.php b/src/Concerns/HelperFunctions.php new file mode 100644 index 0000000..57f4076 --- /dev/null +++ b/src/Concerns/HelperFunctions.php @@ -0,0 +1,19 @@ + ' . $message); + } + + private function showFailureInfo(string $message): void + { + error(' !! ' . $message); + } +} 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); } }