Skip to content

Auto Add #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
```

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions config/env-keys-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
19 changes: 0 additions & 19 deletions database/factories/ModelFactory.php

This file was deleted.

19 changes: 0 additions & 19 deletions database/migrations/create_env_keys_checker_table.php.stub

This file was deleted.

11 changes: 11 additions & 0 deletions pint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"preset": "laravel",
"rules": {
"concat_space": {
"spacing": "one"
},
"ordered_imports": {
"sort_algorithm": "alpha"
}
}
}
Empty file removed resources/views/.gitkeep
Empty file.
41 changes: 40 additions & 1 deletion src/Commands/LaravelEnvKeysCheckerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';

Expand All @@ -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.');
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.');
}
}
16 changes: 0 additions & 16 deletions src/Facades/LaravelEnvKeysChecker.php

This file was deleted.

5 changes: 0 additions & 5 deletions src/LaravelEnvKeysChecker.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/LaravelEnvKeysCheckerCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

it('Command Exist', function () {
$this->artisan('env:keys-check')
$this->artisan('env:keys-check --auto-add=none')
->assertExitCode(0);
});
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}

Expand Down
25 changes: 0 additions & 25 deletions workbench/app/Providers/WorkbenchServiceProvider.php

This file was deleted.

Loading