add clear build command

This commit is contained in:
Romulus21
2023-09-20 08:49:30 +02:00
parent 9b7a11dbd0
commit bfaf82f264

View File

@@ -0,0 +1,51 @@
<?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());
}
}
}