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/config/env-keys-checker.php b/config/env-keys-checker.php index 1beb8e1..5001a7b 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/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/resources/views/.gitkeep b/resources/views/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/Commands/LaravelEnvKeysCheckerCommand.php b/src/Commands/LaravelEnvKeysCheckerCommand.php index e78252e..7be733d 100644 --- a/src/Commands/LaravelEnvKeysCheckerCommand.php +++ b/src/Commands/LaravelEnvKeysCheckerCommand.php @@ -5,11 +5,12 @@ use Illuminate\Console\Command; use Illuminate\Support\Collection; +use function Laravel\Prompts\confirm; use function Laravel\Prompts\table; 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.'; @@ -18,6 +19,16 @@ public function handle(): int $envFiles = glob(base_path('.env*')); $ignoredFiles = config('env-keys-checker.ignore_files', []); + $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.'); @@ -47,6 +58,22 @@ 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; + } + return self::FAILURE; } @@ -99,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/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 @@ -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' ); } diff --git a/workbench/app/Providers/WorkbenchServiceProvider.php b/workbench/app/Providers/WorkbenchServiceProvider.php deleted file mode 100644 index 001d06d..0000000 --- a/workbench/app/Providers/WorkbenchServiceProvider.php +++ /dev/null @@ -1,25 +0,0 @@ -