diff --git a/src/Actions/ReadEnvironmentFile.php b/src/Actions/ReadEnvironmentFile.php index c188a05..03edc36 100644 --- a/src/Actions/ReadEnvironmentFile.php +++ b/src/Actions/ReadEnvironmentFile.php @@ -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; @@ -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); + } + } } diff --git a/src/Commands/PruneCommand.php b/src/Commands/PruneCommand.php index a5e99ce..d7c5a49 100644 --- a/src/Commands/PruneCommand.php +++ b/src/Commands/PruneCommand.php @@ -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; @@ -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('
There are no variables to prune!
'); @@ -57,6 +61,18 @@ public function handle(Envy $envy): int return self::SUCCESS; } + /** + * @return Collection> + * @throws EnvironmentFileNotFoundException + */ + private function getPendingPrunes(Envy $envy): Collection + { + return $envy->pendingPrunes( + $envy->environmentCalls(), + $this->option('path') ? [strval($this->option('path'))] : null + ); + } + /** * @param Collection> $pendingPrunes */ diff --git a/src/Commands/SyncCommand.php b/src/Commands/SyncCommand.php index f45e18c..6734b41 100644 --- a/src/Commands/SyncCommand.php +++ b/src/Commands/SyncCommand.php @@ -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; @@ -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('
There are no variables to sync!
'); @@ -59,6 +63,18 @@ public function handle(Envy $envy, Repository $config): int return self::SUCCESS; } + /** + * @return Collection> + * @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. * diff --git a/src/Contracts/Actions/FiltersEnvironmentCalls.php b/src/Contracts/Actions/FiltersEnvironmentCalls.php index 5bb6a1f..011c6f3 100644 --- a/src/Contracts/Actions/FiltersEnvironmentCalls.php +++ b/src/Contracts/Actions/FiltersEnvironmentCalls.php @@ -5,6 +5,7 @@ namespace Worksome\Envy\Contracts\Actions; use Illuminate\Support\Collection; +use Worksome\Envy\Exceptions\EnvironmentFileNotFoundException; use Worksome\Envy\Support\EnvironmentCall; interface FiltersEnvironmentCalls @@ -12,6 +13,7 @@ interface FiltersEnvironmentCalls /** * @param Collection $environmentCalls * @return Collection + * @throws EnvironmentFileNotFoundException */ public function __invoke(string $filePath, Collection $environmentCalls): Collection; } diff --git a/src/Contracts/Actions/FindsEnvironmentVariablesToPrune.php b/src/Contracts/Actions/FindsEnvironmentVariablesToPrune.php index b8bf21b..cce6633 100644 --- a/src/Contracts/Actions/FindsEnvironmentVariablesToPrune.php +++ b/src/Contracts/Actions/FindsEnvironmentVariablesToPrune.php @@ -5,6 +5,7 @@ namespace Worksome\Envy\Contracts\Actions; use Illuminate\Support\Collection; +use Worksome\Envy\Exceptions\EnvironmentFileNotFoundException; use Worksome\Envy\Support\EnvironmentCall; interface FindsEnvironmentVariablesToPrune @@ -12,6 +13,7 @@ interface FindsEnvironmentVariablesToPrune /** * @param Collection $environmentCalls * @return Collection + * @throws EnvironmentFileNotFoundException */ public function __invoke(string $filePath, Collection $environmentCalls): Collection; } diff --git a/src/Contracts/Actions/ReadsEnvironmentFile.php b/src/Contracts/Actions/ReadsEnvironmentFile.php index b237577..e606b9f 100644 --- a/src/Contracts/Actions/ReadsEnvironmentFile.php +++ b/src/Contracts/Actions/ReadsEnvironmentFile.php @@ -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 + * + * @throws EnvironmentFileNotFoundException */ public function __invoke(string $envFilePath): Collection; } diff --git a/src/Envy.php b/src/Envy.php index d83f1bc..00a424e 100755 --- a/src/Envy.php +++ b/src/Envy.php @@ -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; @@ -50,6 +51,7 @@ public function environmentCalls(bool $excludeCallsWithDefaults = false): Collec * @param Collection $environmentCalls * @param array|null $environmentFilePaths * @return Collection> + * @throws EnvironmentFileNotFoundException */ public function pendingUpdates(Collection $environmentCalls, array|null $environmentFilePaths = null): Collection { @@ -113,6 +115,7 @@ public function hasPublishedConfigFile(): bool * @param Collection $environmentCalls * @param array|null $environmentFilePaths * @return Collection> + * @throws EnvironmentFileNotFoundException */ public function pendingPrunes(Collection $environmentCalls, array|null $environmentFilePaths = null): Collection { diff --git a/src/Exceptions/EnvironmentFileNotFoundException.php b/src/Exceptions/EnvironmentFileNotFoundException.php new file mode 100644 index 0000000..2a21115 --- /dev/null +++ b/src/Exceptions/EnvironmentFileNotFoundException.php @@ -0,0 +1,15 @@ +artisan('envy:prune', ['--force' => true]); @@ -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); +}); diff --git a/tests/Feature/Commands/SyncCommandTest.php b/tests/Feature/Commands/SyncCommandTest.php index c4f59d2..ec685f2 100644 --- a/tests/Feature/Commands/SyncCommandTest.php +++ b/tests/Feature/Commands/SyncCommandTest.php @@ -1,5 +1,6 @@ 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); +}); diff --git a/tests/Unit/Actions/ReadEnvironmentFileTest.php b/tests/Unit/Actions/ReadEnvironmentFileTest.php index 2571259..738526f 100644 --- a/tests/Unit/Actions/ReadEnvironmentFileTest.php +++ b/tests/Unit/Actions/ReadEnvironmentFileTest.php @@ -1,6 +1,7 @@ throws(EnvironmentFileNotFoundException::class);