From b1b87e8af1df661bc529311d62a9f65d11033d23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20=C3=96z=C4=B1=C5=9F=C4=B1k?= Date: Tue, 6 Aug 2024 12:43:47 +0300 Subject: [PATCH] Force mode (#19) * add force option * update readme --- README.md | 9 +++++++++ src/ClearAssets.php | 11 ++++++++++- tests/ClearAssetsTest.php | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 81388fd..138c39f 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,15 @@ Simply run: php please assets:clear ``` +It will list all the assets that are not referenced anywhere in your content directory. You can delete them all at once, or one by one. + +If you want to skip the interactive mode, you can use the `--force` flag: + +```bash +# This will delete all the unused assets without asking for confirmation! +php please assets:clear --force +``` + ## Configuration To publish the config file, use: diff --git a/src/ClearAssets.php b/src/ClearAssets.php index 90a9822..bf31b9e 100644 --- a/src/ClearAssets.php +++ b/src/ClearAssets.php @@ -13,12 +13,14 @@ class ClearAssets extends Command { use RunsInPlease; - protected $name = 'statamic:assets:clear'; + protected $signature = 'statamic:assets:clear {--force=}'; protected $description = "Delete unused assets."; private $choice; + private $isForced = false; + const CMD_DELETE_ALL = 'Delete all'; const CMD_DELETE_BY_CHOICE = 'Choose what to delete'; const CMD_EXIT = 'Don\'t do anything'; @@ -31,6 +33,8 @@ class ClearAssets extends Command public function handle() { + $this->isForced = (bool) $this->option('force'); + $unusedAssets = $this->filterUnused(Asset::all()); if ($unusedAssets->isEmpty()) { @@ -132,6 +136,11 @@ private function removeAsset(Asset $asset) private function presentChoices() { + if ($this->isForced) { + $this->choice = self::CMD_DELETE_ALL; + return; + } + $this->choice = $this->choice( 'What would you like to do?', self::$choices, diff --git a/tests/ClearAssetsTest.php b/tests/ClearAssetsTest.php index 7fc2c84..5f7a169 100644 --- a/tests/ClearAssetsTest.php +++ b/tests/ClearAssetsTest.php @@ -110,6 +110,21 @@ public function it_confirms_deletion_one_by_one() $this->assertContainerFileCount('assets', 1); } + /** + * @test + */ + public function it_skips_confirmation_in_no_interaction_mode() + { + $this->createAsset('ankara.jpg'); + $this->createAsset('tallinn.jpg'); + + $this->artisan(ClearAssets::class, ['--force' => true]) + ->expectsOutput('Removing ankara.jpg') + ->expectsOutput('Removing tallinn.jpg'); + + $this->assertContainerFileCount('assets', 0); + } + private function createAsset($filename, $container = 'assets') { $tmpFile = tempnam(sys_get_temp_dir(), 'test_' . $filename);