Skip to content

Commit

Permalink
refactor build command
Browse files Browse the repository at this point in the history
  • Loading branch information
gwleuverink committed Jan 9, 2024
1 parent 8d297f5 commit a1f7d4c
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/Commands/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,41 @@

use function Laravel\Prompts\info;
use function Laravel\Prompts\error;
use function Laravel\Prompts\progress;

class Build extends Command
{
protected $signature = 'bundle:build';
protected $description = 'Scan resource directory and bundle all imports for production';
protected $description = 'Scan build_patsh and bundle all imports for production';

public function handle(Finder $finder): int
{
$this->call('bundle:clear');

$errors = 0;

// Find all usages of x-bundle
// Find and bundle all components
collect(config('bundle.build_paths'))
// Find all files matching given glob pattern
->map(fn($glob) => $finder->files()->in($glob)->depth(0))
// Map them to an array
->flatMap(fn(Finder $iterator) => iterator_to_array($iterator))
// pregmatch each file for x-bundle components
// Pregmatch each file for x-bundle components
->flatMap(fn(SplFileInfo $file) => preg_grep('/^<x-bundle.*?>$/', file($file)))
// filter uniques
// Filter uniques
->unique()
// Then render the blade! The component does the rest
->each(function($component) use (&$errors) {
// Start progress bar
->pipe(fn($components) => progress('Building Bundle imports', $components, function($component) use (&$file, $errors) {
try {
Blade::render($component);
$this->components->task($component);
// Render the blade. The component does the rest
$this->components->task(
"$component from: $file",
fn() => Blade::render($component)
);
} catch(Throwable $e) {
$this->components->error($component);
$errors++;
}
});

}));

if($errors) {
error('Bundle compiled with errors');
Expand Down

0 comments on commit a1f7d4c

Please sign in to comment.