52 lines
1.2 KiB
PHP
52 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Exception;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
class ClearBuild extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'clear-build';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Clean unused files in build/assets';
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*/
|
|
public function handle(): void
|
|
{
|
|
try {
|
|
// list all current build files used
|
|
$filesUsed = [];
|
|
$manifest = File::get(public_path('build/manifest.json'));
|
|
foreach (json_decode($manifest) as $line) {
|
|
$filesUsed[] = str_replace('assets/', '', $line->file);
|
|
}
|
|
|
|
$count = 0;
|
|
foreach (File::files(public_path('build/assets')) as $file) {
|
|
if (! in_array($file->getFilename(), $filesUsed)) {
|
|
unlink($file->getPathname());
|
|
$count++;
|
|
}
|
|
}
|
|
|
|
$this->info($count.' files deleted.');
|
|
} catch (Exception $e) {
|
|
$this->error($e->getMessage());
|
|
}
|
|
}
|
|
}
|