Skip to content

Commit

Permalink
Merge pull request #21 from worksome/display_error_when_no_file_exists
Browse files Browse the repository at this point in the history
Shows a friendly message if an environment file doens't exist.
  • Loading branch information
lukeraymonddowning authored Apr 13, 2022
2 parents beba651 + dedceeb commit 71011d4
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 9 deletions.
16 changes: 15 additions & 1 deletion src/Actions/ReadEnvironmentFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Dotenv\Parser\Entry;
use Dotenv\Parser\Parser;
use Illuminate\Support\Collection;
use Throwable;
use Worksome\Envy\Contracts\Actions\ReadsEnvironmentFile;
use Worksome\Envy\Exceptions\EnvironmentFileNotFoundException;
use Worksome\Envy\Support\EnvironmentVariable;

use function Safe\file_get_contents;
Expand All @@ -17,11 +19,23 @@ final class ReadEnvironmentFile implements ReadsEnvironmentFile
public function __invoke(string $envFilePath): Collection
{
$parser = new Parser();
$entries = $parser->parse(file_get_contents($envFilePath));
$entries = $parser->parse($this->getFileContents($envFilePath));

return collect($entries)->map(fn (Entry $entry) => new EnvironmentVariable(
$entry->getName(),
$entry->getValue()->get()->getChars()
));
}

/**
* @throws EnvironmentFileNotFoundException
*/
private function getFileContents(string $envFilePath): string
{
try {
return file_get_contents($envFilePath);
} catch (Throwable) {
throw new EnvironmentFileNotFoundException($envFilePath);
}
}
}
24 changes: 20 additions & 4 deletions src/Commands/PruneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Facades\Blade;
use Worksome\Envy\Commands\Concerns\HasUsefulConsoleMethods;
use Worksome\Envy\Envy;
use Worksome\Envy\Exceptions\EnvironmentFileNotFoundException;

use function Termwind\render;

Expand All @@ -30,10 +31,13 @@ final class PruneCommand extends Command

public function handle(Envy $envy): int
{
$pendingPrunes = $envy->pendingPrunes(
$envy->environmentCalls(),
$this->option('path') ? [strval($this->option('path'))] : null
);
try {
$pendingPrunes = $this->getPendingPrunes($envy);
} catch (EnvironmentFileNotFoundException $exception) {
$this->warning($exception->getMessage());

return self::INVALID;
}

if ($pendingPrunes->isEmpty()) {
render('<div class="px-1 py-1 bg-green-500 font-bold">There are no variables to prune!</div>');
Expand All @@ -57,6 +61,18 @@ public function handle(Envy $envy): int
return self::SUCCESS;
}

/**
* @return Collection<string, Collection<int, string>>
* @throws EnvironmentFileNotFoundException
*/
private function getPendingPrunes(Envy $envy): Collection
{
return $envy->pendingPrunes(
$envy->environmentCalls(),
$this->option('path') ? [strval($this->option('path'))] : null
);
}

/**
* @param Collection<string, Collection<int, string>> $pendingPrunes
*/
Expand Down
24 changes: 20 additions & 4 deletions src/Commands/SyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Support\Facades\Blade;
use Worksome\Envy\Commands\Concerns\HasUsefulConsoleMethods;
use Worksome\Envy\Envy;
use Worksome\Envy\Exceptions\EnvironmentFileNotFoundException;
use Worksome\Envy\Support\EnvironmentCall;

use function Termwind\render;
Expand All @@ -32,10 +33,13 @@ final class SyncCommand extends Command

public function handle(Envy $envy, Repository $config): int
{
$pendingUpdates = $envy->pendingUpdates(
$envy->environmentCalls(boolval($config->get('envy.exclude_calls_with_defaults', false))),
$this->option('path') ? [strval($this->option('path'))] : null,
);
try {
$pendingUpdates = $this->getPendingPrunes($envy, $config);
} catch (EnvironmentFileNotFoundException $exception) {
$this->warning($exception->getMessage());

return self::INVALID;
}

if ($pendingUpdates->isEmpty()) {
render('<div class="px-1 py-1 bg-green-500 font-bold">There are no variables to sync!</div>');
Expand All @@ -59,6 +63,18 @@ public function handle(Envy $envy, Repository $config): int
return self::SUCCESS;
}

/**
* @return Collection<string, Collection<int, EnvironmentCall>>
* @throws EnvironmentFileNotFoundException
*/
private function getPendingPrunes(Envy $envy, Repository $config): Collection
{
return $envy->pendingUpdates(
$envy->environmentCalls(boolval($config->get('envy.exclude_calls_with_defaults', false))),
$this->option('path') ? [strval($this->option('path'))] : null,
);
}

/**
* Outputs pending updates to the console.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/Actions/FiltersEnvironmentCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
namespace Worksome\Envy\Contracts\Actions;

use Illuminate\Support\Collection;
use Worksome\Envy\Exceptions\EnvironmentFileNotFoundException;
use Worksome\Envy\Support\EnvironmentCall;

interface FiltersEnvironmentCalls
{
/**
* @param Collection<int, EnvironmentCall> $environmentCalls
* @return Collection<int, EnvironmentCall>
* @throws EnvironmentFileNotFoundException
*/
public function __invoke(string $filePath, Collection $environmentCalls): Collection;
}
2 changes: 2 additions & 0 deletions src/Contracts/Actions/FindsEnvironmentVariablesToPrune.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
namespace Worksome\Envy\Contracts\Actions;

use Illuminate\Support\Collection;
use Worksome\Envy\Exceptions\EnvironmentFileNotFoundException;
use Worksome\Envy\Support\EnvironmentCall;

interface FindsEnvironmentVariablesToPrune
{
/**
* @param Collection<int, EnvironmentCall> $environmentCalls
* @return Collection<int, string>
* @throws EnvironmentFileNotFoundException
*/
public function __invoke(string $filePath, Collection $environmentCalls): Collection;
}
3 changes: 3 additions & 0 deletions src/Contracts/Actions/ReadsEnvironmentFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
namespace Worksome\Envy\Contracts\Actions;

use Illuminate\Support\Collection;
use Worksome\Envy\Exceptions\EnvironmentFileNotFoundException;
use Worksome\Envy\Support\EnvironmentVariable;

interface ReadsEnvironmentFile
{
/**
* @return Collection<int, EnvironmentVariable>
*
* @throws EnvironmentFileNotFoundException
*/
public function __invoke(string $envFilePath): Collection;
}
3 changes: 3 additions & 0 deletions src/Envy.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Worksome\Envy\Contracts\Actions\PrunesEnvironmentFile;
use Worksome\Envy\Contracts\Actions\UpdatesEnvironmentFile;
use Worksome\Envy\Contracts\Finder;
use Worksome\Envy\Exceptions\EnvironmentFileNotFoundException;
use Worksome\Envy\Support\EnvironmentCall;
use Worksome\Envy\Support\EnvironmentVariable;

Expand Down Expand Up @@ -50,6 +51,7 @@ public function environmentCalls(bool $excludeCallsWithDefaults = false): Collec
* @param Collection<int, EnvironmentCall> $environmentCalls
* @param array<int, string>|null $environmentFilePaths
* @return Collection<string, Collection<int, EnvironmentCall>>
* @throws EnvironmentFileNotFoundException
*/
public function pendingUpdates(Collection $environmentCalls, array|null $environmentFilePaths = null): Collection
{
Expand Down Expand Up @@ -113,6 +115,7 @@ public function hasPublishedConfigFile(): bool
* @param Collection<int, EnvironmentCall> $environmentCalls
* @param array<int, string>|null $environmentFilePaths
* @return Collection<string, Collection<int, string>>
* @throws EnvironmentFileNotFoundException
*/
public function pendingPrunes(Collection $environmentCalls, array|null $environmentFilePaths = null): Collection
{
Expand Down
15 changes: 15 additions & 0 deletions src/Exceptions/EnvironmentFileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Worksome\Envy\Exceptions;

use Exception;

final class EnvironmentFileNotFoundException extends Exception
{
public function __construct(string $environmentFileName)
{
parent::__construct("We were unable to locate [$environmentFileName]. Does it exist in your project?");
}
}
8 changes: 8 additions & 0 deletions tests/Feature/Commands/PruneCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Symfony\Component\Console\Command\Command;

it('prunes an environment file with additional entries', function () {
$this->artisan('envy:prune', ['--force' => true]);

Expand Down Expand Up @@ -56,3 +58,9 @@
return str_contains($newContent, 'MIX_URL');
});
});

it('shows a useful error message if a configured environment file doesn\'t exist', function () {
$this->artisan('envy:prune', [
'--path' => testAppPath('environments/.env.testing')
])->assertExitCode(Command::INVALID);
});
7 changes: 7 additions & 0 deletions tests/Feature/Commands/SyncCommandTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use Symfony\Component\Console\Command\Command;
use Worksome\Envy\Contracts\Actions\FindsEnvironmentCalls;

it('updates the .env file with missing keys', function () {
Expand Down Expand Up @@ -75,3 +76,9 @@

$this->assertFileChanged(testAppPath('environments/.env.empty'));
});

it('shows a useful error message if a configured environment file doesn\'t exist', function () {
$this->artisan('envy:sync', [
'--path' => testAppPath('environments/.env.testing')
])->assertExitCode(Command::INVALID);
});
6 changes: 6 additions & 0 deletions tests/Unit/Actions/ReadEnvironmentFileTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Worksome\Envy\Actions\ReadEnvironmentFile;
use Worksome\Envy\Exceptions\EnvironmentFileNotFoundException;
use Worksome\Envy\Support\EnvironmentVariable;

it('returns a collection of environment variables', function () {
Expand All @@ -24,3 +25,8 @@
'MIX_URL',
]);
});

it('throws an EnvironmentFileNotFoundException if the requested .env file could not be located', function () {
$action = new ReadEnvironmentFile();
$action(__DIR__ . '/../../Application/.env.testing');
})->throws(EnvironmentFileNotFoundException::class);

0 comments on commit 71011d4

Please sign in to comment.