From 5f4f4571e18fe295ab3d6088efa9a1141c19ff44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20=C3=96zisik?= Date: Tue, 6 Aug 2024 12:41:26 +0300 Subject: [PATCH 1/2] add force option --- src/ClearAssets.php | 11 ++++++++++- tests/ClearAssetsTest.php | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) 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); From 5bce0cdec576e8df5926db6c0078efca7d81e18e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20=C3=96zisik?= Date: Tue, 6 Aug 2024 12:42:31 +0300 Subject: [PATCH 2/2] update readme --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) 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: