add clear build command
This commit is contained in:
51
app/Console/Commands/ClearBuild.php
Normal file
51
app/Console/Commands/ClearBuild.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user