Skip to content
Closed
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
91 changes: 88 additions & 3 deletions src/GenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class GenerateCommand extends Command

private $content = [];

private $cached = [];

private $generatedFiles = [];

/**
* Imports array where the key is the generated file path and the value is an array of imports.
* Each import is an array where the first element is the import path and the second element is an array of imported items.
Expand Down Expand Up @@ -72,29 +76,36 @@ public function handle()
});

if (! $this->option('skip-actions')) {
$this->files->deleteDirectory($this->base());
$this->readCacheFile();
if ($this->cached->isEmpty()) {
$this->files->deleteDirectory($this->base());
}

$controllers = $routes->filter(fn (Route $route) => $route->hasController())->groupBy(fn (Route $route) => $route->dotNamespace());

$controllers->undot()->each($this->writeBarrelFiles(...));
$controllers->each($this->writeControllerFile(...));

$this->writeContent();
$this->writeCacheFile();

info('[Wayfinder] Generated actions in '.$this->base());
}

$this->pathDirectory = 'routes';

if (! $this->option('skip-routes')) {
$this->files->deleteDirectory($this->base());
$this->readCacheFile();
if ($this->cached->isEmpty()) {
$this->files->deleteDirectory($this->base());
}

$named = $routes->filter(fn (Route $route) => $route->name())->groupBy(fn (Route $route) => $route->name());

$named->each($this->writeNamedFile(...));
$named->undot()->each($this->writeBarrelFiles(...));

$this->writeContent();
$this->writeCacheFile();

info('[Wayfinder] Generated routes in '.$this->base());
}
Expand All @@ -103,6 +114,59 @@ public function handle()

$this->files->ensureDirectoryExists($this->base());
$this->files->copy(__DIR__.'/../resources/js/wayfinder.ts', join_paths($this->base(), 'index.ts'));

$this->cleanup();
}

private function readCacheFile(): void
{
$this->cached = collect();
$file = join_paths($this->base(), 'cache.json');
if ($this->files->exists($file)) {
$this->cached = collect(json_decode(file_get_contents($file, true), true));
}
}

private function writeCacheFile(): void
{
$file = join_paths($this->base(), 'cache.json');
$this->files->put($file, json_encode($this->cached->toArray()));
}

public function cacheKey($route, $path): string
{
$data = [
'controller' => $route->controller(),
'method' => $route->name() ? $route->namedMethod() : null,
'namedMethod' => $route->jsMethod(),
'original_method' => $route->originalJsMethod(),
'isInvokable' => $route->hasInvokableController(),
'shouldExport' => ! $route->hasInvokableController(),
'path' => $route->controllerPath(),
'line' => $route->controllerMethodLineNumber(),
'parameters' => $route->parameters()->pluck('safeName')->toArray(),
'verbs' => $route->verbs()->pluck('actual')->toArray(),
'uri' => $route->uri(),
'withForm' => $this->option('with-form') ?? false,
// 'rand' => rand(),
];

return hash('xxh128', $path.json_encode($data));
}

private function cleanup()
{
$file = join_paths($this->base(), 'generated_files.json');
if ($this->files->exists($file)) {
$prevFiles = json_decode(file_get_contents($file, true), true);
$filesToBeDeleted = array_diff($prevFiles, $this->generatedFiles);
foreach ($filesToBeDeleted as $path) {
if ($this->files->exists($path)) {
$this->files->delete($path);
}
}
}
$this->files->put($file, json_encode($this->generatedFiles));
}

private function appendContent($path, $content): void
Expand Down Expand Up @@ -140,6 +204,13 @@ private function writeContent(): void
private function writeControllerFile(Collection $routes, string $namespace): void
{
$path = join_paths($this->base(), ...explode('.', $namespace)).'.ts';
$this->generatedFiles[] = $path;

$cahceKeys = $routes->map(fn ($route) => $this->cacheKey($route, $path));
if ($cahceKeys->every(fn ($key) => $this->cached->contains($key)) && $this->files->exists($path)) {
return;
}
$this->cached->push(...$cahceKeys);

$this->appendCommonImports($routes, $path, $namespace);

Expand Down Expand Up @@ -221,6 +292,13 @@ private function writeNamedFile(Collection $routes, string $namespace): void
$parts[] = 'index';

$path = join_paths($this->base(), ...$parts).'.ts';
$this->generatedFiles[] = $path;

$cahceKeys = $routes->map(fn ($route) => $this->cacheKey($route, $path));
if ($cahceKeys->every(fn ($key) => $this->cached->contains($key)) && $this->files->exists($path)) {
return;
}
$this->cached->push(...$cahceKeys);

$this->appendCommonImports($routes, $path, $namespace);

Expand Down Expand Up @@ -279,6 +357,13 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v
}

$indexPath = join_paths($this->base(), $parent, 'index.ts');
$this->generatedFiles[] = $indexPath;

$cahceKeys = $children->flatten()->map(fn ($route) => $this->cacheKey($route, $indexPath));
if ($cahceKeys->every(fn ($key) => $this->cached->contains($key)) && $this->files->exists($indexPath)) {
// return;
}

$keysWithGrandkids = $children->filter(fn ($grandChildren) => ! array_is_list(collect($grandChildren)->all()));

$childKeys = $children->keys()->mapWithKeys(function ($child) use ($indexPath, $keysWithGrandkids) {
Expand Down
Loading