From 428452036c4270031c141710b04d4aa8d22f0df3 Mon Sep 17 00:00:00 2001 From: msamgan Date: Sun, 6 Oct 2024 12:39:42 -0400 Subject: [PATCH 1/3] WIP --- config/env-keys-checker.php | 6 ++++++ database/factories/ModelFactory.php | 19 ------------------- .../create_env_keys_checker_table.php.stub | 19 ------------------- resources/views/.gitkeep | 0 4 files changed, 6 insertions(+), 38 deletions(-) delete mode 100644 database/factories/ModelFactory.php delete mode 100644 database/migrations/create_env_keys_checker_table.php.stub delete mode 100644 resources/views/.gitkeep diff --git a/config/env-keys-checker.php b/config/env-keys-checker.php index 1beb8e1..52d52c7 100644 --- a/config/env-keys-checker.php +++ b/config/env-keys-checker.php @@ -6,4 +6,10 @@ // List of all the env keys to ignore while checking the env keys 'ignore_keys' => [], + + // strategy to add the missing keys to the .env file + // ask: will ask the user to add the missing keys + // auto: will add the missing keys automatically + // none: will not add the missing keys + 'auto_add' => 'ask' ]; diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php deleted file mode 100644 index 19df6db..0000000 --- a/database/factories/ModelFactory.php +++ /dev/null @@ -1,19 +0,0 @@ -id(); - - // add fields - - $table->timestamps(); - }); - } -}; diff --git a/resources/views/.gitkeep b/resources/views/.gitkeep deleted file mode 100644 index e69de29..0000000 From 0a2081f8cb1a6b583d292dbe4858c86e0dd6f655 Mon Sep 17 00:00:00 2001 From: msamgan Date: Sun, 6 Oct 2024 13:11:55 -0400 Subject: [PATCH 2/3] auto add strategy --- config/env-keys-checker.php | 2 +- src/Commands/LaravelEnvKeysCheckerCommand.php | 34 +++++++++++++++++++ src/Facades/LaravelEnvKeysChecker.php | 16 --------- src/LaravelEnvKeysChecker.php | 5 --- .../Providers/WorkbenchServiceProvider.php | 25 -------------- 5 files changed, 35 insertions(+), 47 deletions(-) delete mode 100644 src/Facades/LaravelEnvKeysChecker.php delete mode 100755 src/LaravelEnvKeysChecker.php delete mode 100644 workbench/app/Providers/WorkbenchServiceProvider.php diff --git a/config/env-keys-checker.php b/config/env-keys-checker.php index 52d52c7..5001a7b 100644 --- a/config/env-keys-checker.php +++ b/config/env-keys-checker.php @@ -11,5 +11,5 @@ // ask: will ask the user to add the missing keys // auto: will add the missing keys automatically // none: will not add the missing keys - 'auto_add' => 'ask' + 'auto_add' => 'ask', ]; diff --git a/src/Commands/LaravelEnvKeysCheckerCommand.php b/src/Commands/LaravelEnvKeysCheckerCommand.php index e78252e..ea83362 100644 --- a/src/Commands/LaravelEnvKeysCheckerCommand.php +++ b/src/Commands/LaravelEnvKeysCheckerCommand.php @@ -5,6 +5,7 @@ use Illuminate\Console\Command; use Illuminate\Support\Collection; +use function Laravel\Prompts\confirm; use function Laravel\Prompts\table; class LaravelEnvKeysCheckerCommand extends Command @@ -18,6 +19,7 @@ public function handle(): int $envFiles = glob(base_path('.env*')); $ignoredFiles = config('env-keys-checker.ignore_files', []); + $autoAddStrategy = config('env-keys-checker.auto_add', 'ask'); if (empty($envFiles)) { $this->error('!! No .env files found.'); @@ -47,9 +49,41 @@ public function handle(): int rows: $missingKeys, ); + if ($autoAddStrategy === 'ask') { + $confirmation = confirm('Do you want to add the missing keys to the .env files?'); + + if ($confirmation) { + $this->addKeysToFile($missingKeys, $envFiles); + } + + return self::SUCCESS; + } + + if ($autoAddStrategy === 'auto') { + $this->addKeysToFile($missingKeys, $envFiles); + + return self::SUCCESS; + } + + if ($autoAddStrategy === 'none') { + return self::SUCCESS; + } + return self::FAILURE; } + private function addKeysToFile($missingKeys, array $envFiles): void + { + $missingKeys->each(function ($missingKey) { + $filePath = base_path($missingKey['envFile']); + $envContent = file($filePath); + array_splice($envContent, $missingKey['line'] - 1, 0, $missingKey['key'].'=""'.PHP_EOL); + file_put_contents($filePath, $envContent); + }); + + $this->info('=> Missing keys added to all .env files.'); + } + private function getAllKeys($files): Collection { $ignoredKeys = config('env-keys-checker.ignore_keys', []); diff --git a/src/Facades/LaravelEnvKeysChecker.php b/src/Facades/LaravelEnvKeysChecker.php deleted file mode 100644 index 0fd9c5f..0000000 --- a/src/Facades/LaravelEnvKeysChecker.php +++ /dev/null @@ -1,16 +0,0 @@ - Date: Sun, 6 Oct 2024 13:31:15 -0400 Subject: [PATCH 3/3] Auto add. --- README.md | 11 ++++- pint.json | 11 +++++ src/Commands/LaravelEnvKeysCheckerCommand.php | 41 +++++++++++-------- tests/LaravelEnvKeysCheckerCommandTest.php | 2 +- tests/TestCase.php | 2 +- 5 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 pint.json diff --git a/README.md b/README.md index c51e6e6..865a51a 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Add the following code to your test case. ```php it('tests that the .env key are same across all .env files.', function () { - $this->artisan('env:keys-check')->assertExitCode(0); + $this->artisan('env:keys-check --auto-add=none')->assertExitCode(0); }); ``` @@ -64,6 +64,15 @@ You can configure the package by publishing the configuration file. 'ignore_keys' => [], ``` +```php +# config/env-keys-checker.php +# strategy to add the missing keys to the .env file +# ask: will ask the user to add the missing keys +# auto: will add the missing keys automatically +# none: will not add the missing keys +'auto_add' => 'ask', +``` + ## Testing ```bash diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..7eb5048 --- /dev/null +++ b/pint.json @@ -0,0 +1,11 @@ +{ + "preset": "laravel", + "rules": { + "concat_space": { + "spacing": "one" + }, + "ordered_imports": { + "sort_algorithm": "alpha" + } + } +} diff --git a/src/Commands/LaravelEnvKeysCheckerCommand.php b/src/Commands/LaravelEnvKeysCheckerCommand.php index ea83362..7be733d 100644 --- a/src/Commands/LaravelEnvKeysCheckerCommand.php +++ b/src/Commands/LaravelEnvKeysCheckerCommand.php @@ -10,7 +10,7 @@ class LaravelEnvKeysCheckerCommand extends Command { - public $signature = 'env:keys-check'; + 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.'; @@ -19,7 +19,16 @@ public function handle(): int $envFiles = glob(base_path('.env*')); $ignoredFiles = config('env-keys-checker.ignore_files', []); - $autoAddStrategy = config('env-keys-checker.auto_add', 'ask'); + $autoAddOption = $this->option('auto-add'); + $autoAddAvailableOptions = ['ask', 'auto', 'none']; + + $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)); + + return self::FAILURE; + } if (empty($envFiles)) { $this->error('!! No .env files found.'); @@ -65,25 +74,9 @@ public function handle(): int return self::SUCCESS; } - if ($autoAddStrategy === 'none') { - return self::SUCCESS; - } - return self::FAILURE; } - private function addKeysToFile($missingKeys, array $envFiles): void - { - $missingKeys->each(function ($missingKey) { - $filePath = base_path($missingKey['envFile']); - $envContent = file($filePath); - array_splice($envContent, $missingKey['line'] - 1, 0, $missingKey['key'].'=""'.PHP_EOL); - file_put_contents($filePath, $envContent); - }); - - $this->info('=> Missing keys added to all .env files.'); - } - private function getAllKeys($files): Collection { $ignoredKeys = config('env-keys-checker.ignore_keys', []); @@ -133,4 +126,16 @@ private function checkForKeyInFile($keyData, $envFiles, $missingKeys): void } }); } + + private function addKeysToFile($missingKeys, array $envFiles): void + { + $missingKeys->each(function ($missingKey) { + $filePath = base_path($missingKey['envFile']); + $envContent = file($filePath); + array_splice($envContent, $missingKey['line'] - 1, 0, $missingKey['key'] . '=""' . PHP_EOL); + file_put_contents($filePath, $envContent); + }); + + $this->info('=> Missing keys added to all .env files.'); + } } diff --git a/tests/LaravelEnvKeysCheckerCommandTest.php b/tests/LaravelEnvKeysCheckerCommandTest.php index cdc4bc6..009899a 100644 --- a/tests/LaravelEnvKeysCheckerCommandTest.php +++ b/tests/LaravelEnvKeysCheckerCommandTest.php @@ -1,6 +1,6 @@ artisan('env:keys-check') + $this->artisan('env:keys-check --auto-add=none') ->assertExitCode(0); }); diff --git a/tests/TestCase.php b/tests/TestCase.php index b9bbad5..2477fc4 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -13,7 +13,7 @@ protected function setUp(): void parent::setUp(); Factory::guessFactoryNamesUsing( - fn (string $modelName) => 'Msamgan\\LaravelEnvKeysChecker\\Database\\Factories\\'.class_basename($modelName).'Factory' + fn (string $modelName) => 'Msamgan\\LaravelEnvKeysChecker\\Database\\Factories\\' . class_basename($modelName) . 'Factory' ); }