Skip to content

Commit

Permalink
Force mode (#19)
Browse files Browse the repository at this point in the history
* add force option

* update readme
  • Loading branch information
aozisik authored Aug 6, 2024
1 parent e3e55a7 commit b1b87e8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
11 changes: 10 additions & 1 deletion src/ClearAssets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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()) {
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 15 additions & 0 deletions tests/ClearAssetsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b1b87e8

Please sign in to comment.