Skip to content

Commit

Permalink
backward compatibility/test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lmeysel committed Jul 28, 2023
1 parent 3a71ad5 commit efc4b96
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/CommandRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function makeDirectory($path)
}

private function deriveDtsFile($path) {
if(str_ends_with($path, '.d.ts')) {
if(preg_match('/.d.ts$/', $path)) {
return $path;
} else {
$dir = dirname($path) ?? '.';
Expand Down
26 changes: 16 additions & 10 deletions src/TypeScriptDeclarationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,30 @@

class TypeScriptDeclarationGenerator
{
private Collection $routes;
/**
* @var Collection
*/
private $routes;

protected $indent = " ";

public function __construct(Collection $routes)
/**
* @param Collection $routes
*/
public function __construct($routes)
{
$this->routes = $routes;
}

private function preamble()
{
return collect([
return join("\n", [
"/*",
" * Do not modify this file. It is auto-generated and corresponds to your routes",
" * exposed by ziggy route helper. Changes will not be preserved.",
" */",
"export {}"
])->join("\n");
]);
}

private function generateArgsType($route)
Expand All @@ -39,26 +45,26 @@ private function generateArgsType($route)
return "{ name: '$param' }";
}
});
return '['.$list->join(', ').']';
return '['.join(', ', $list->toArray()).']';
}

private function generateRouteDefinition() {
$overloads = $this->routes->map(function ($route, $name) {
$routeArgs = $this->generateArgsType($route);
return "'$name': $routeArgs,";
});
return collect(["declare module 'ziggy-js' {",
return join("\n", ["declare module 'ziggy-js' {",
$this->indent.'interface RouteLookup {',
str_repeat($this->indent, 2) . $overloads->join("\n" . str_repeat($this->indent, 2)),
str_repeat($this->indent, 2) . join("\n". str_repeat($this->indent, 2), $overloads->toArray()),
$this->indent.'}',
"}"])->join("\n");
"}"]);
}

public function generateDeclarations()
{
return collect([
return join("\n\n", [
$this->preamble(),
$this->generateRouteDefinition()
])->join("\n\n");
]);
}
}
2 changes: 1 addition & 1 deletion src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function toArray(): array
'defaults' => method_exists(app('url'), 'getDefaultParameters')
? app('url')->getDefaultParameters()
: [],
'routes' => $this->applyFilters($this->group)->map(fn ($route) => $route->except('parameterNames'))->toArray(),
'routes' => $this->applyFilters($this->group)->map(function ($route) { return $route->except('parameterNames'); })->toArray(),
];
}

Expand Down
14 changes: 9 additions & 5 deletions tests/Unit/CommandRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function can_generate_file_using_config_path()
/** @test */
public function can_generate_dts_file()
{
Artisan::call('ziggy:generate --declarations');
Artisan::call('ziggy:generate', ['--declarations' => true]);

$this->assertFileExists(base_path('resources/js/ziggy.d.ts'));
$this->assertFileExists(base_path('resources/js/ziggy.js'));
Expand All @@ -154,18 +154,22 @@ public function can_generate_dts_file()
/** @test */
public function can_generate_dts_file_without_routes()
{
Artisan::call('ziggy:generate --declarations-only');
Artisan::call('ziggy:generate', ['--declarations-only' => true]);

$this->assertFileExists(base_path('resources/js/ziggy.d.ts'));
$this->assertFileDoesNotExist(base_path('resources/js/ziggy.js'));

if(method_exists($this, 'assertFileDoesNotExist'))
$this->assertFileDoesNotExist(base_path('resources/js/ziggy.js'));
else
$this->assertFileNotExists(base_path('resources/js/ziggy.js'));
}

/** @test */
public function can_derive_dts_file_path_from_given_dts()
{
config(['ziggy.output.path' => 'resources/js/custom.d.ts']);

Artisan::call('ziggy:generate --declarations-only');
Artisan::call('ziggy:generate', ['--declarations-only' => true]);

$this->assertFileExists(base_path('resources/js/custom.d.ts'));
}
Expand All @@ -175,7 +179,7 @@ public function can_derive_dts_file_path_from_relative_file_without_extension()
{
config(['ziggy.output.path' => './ziggy']);

Artisan::call('ziggy:generate --declarations-only');
Artisan::call('ziggy:generate', ['--declarations-only' => true]);

$this->assertFileExists(base_path('ziggy.d.ts'));
}
Expand Down

0 comments on commit efc4b96

Please sign in to comment.