Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force mode #19

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading