Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support passing except/only args to ziggy command #805

Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ Route::get('ziggy', fn () => response()->json(new Ziggy));

### Re-generating the routes file when your app routes change

If you are generating your Ziggy config as a file by running `php artisan ziggy:generate`, you may want to re-run that command when your app's route files change. The example below is a Laravel Mix plugin, but similar functionality could be achieved without Mix. Huge thanks to [Nuno Rodrigues](https://github.com/nacr) for [the idea and a sample implementation](https://github.com/tighten/ziggy/issues/321#issuecomment-689150082). See [#655 for a Vite example](https://github.com/tighten/ziggy/pull/655/files#diff-4aeb78f813e14842fcf95bdace9ced23b8a6eed60b23c165eaa52e8db2f97b61).
If you are generating your Ziggy config as a file by running `php artisan ziggy:generate`, you may want to re-run that command when your app's route files change. The example below is a Laravel Mix plugin, but similar functionality could be achieved without Mix. Huge thanks to [Nuno Rodrigues](https://github.com/nacr) for [the idea and a sample implementation](https://github.com/tighten/ziggy/issues/321#issuecomment-689150082). See [#655](https://github.com/tighten/ziggy/pull/655/files#diff-4aeb78f813e14842fcf95bdace9ced23b8a6eed60b23c165eaa52e8db2f97b61) or [vite-plugin-ziggy](https://github.com/aniftyco/vite-plugin-ziggy) for Vite examples.

<details>
<summary>Laravel Mix plugin example</summary>
Expand Down
10 changes: 9 additions & 1 deletion src/CommandRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ class CommandRouteGenerator extends Command
{--types : Generate a TypeScript declaration file.}
{--types-only : Generate only a TypeScript declaration file.}
{--url=}
{--group=}';
{--group=}
{--except= : Route name patterns to exclude.}
{--only= : Route name patterns to include.}';

protected $description = 'Generate a JavaScript file containing Ziggy’s routes and configuration.';

public function handle(Filesystem $filesystem)
{
$ziggy = new Ziggy($this->option('group'), $this->option('url') ? url($this->option('url')) : null);

if ($this->option('except') && ! $this->option('only')) {
$ziggy->filter(explode(',', $this->option('except')), false);
} else if ($this->option('only') && ! $this->option('except')) {
$ziggy->filter(explode(',', $this->option('only')));
}

$path = $this->argument('path') ?? config('ziggy.output.path', 'resources/js/ziggy.js');

if ($filesystem->isDirectory(base_path($path))) {
Expand Down
32 changes: 31 additions & 1 deletion tests/Unit/CommandRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@
expect(base_path('resources/js/ziggy.js'))->toEqualFile('./tests/fixtures/ziggy.js');
});

test('generate file using --except option', function () {
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
Route::get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');
Route::get('admin', fn () => '')->name('admin.dashboard'); // Excluded by options

artisan('ziggy:generate --except=admin.*');

expect(base_path('resources/js/ziggy.js'))->toEqualFile('./tests/fixtures/ziggy.js');
});

test('generate file using --only option', function () {
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
Route::get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');
Route::get('admin', fn () => '')->name('admin.dashboard'); // Excluded by options

artisan('ziggy:generate --only=postComments.index,slashes');

expect(base_path('resources/js/ziggy.js'))->toEqualFile('./tests/fixtures/ziggy.js');
});

test('generate file using both --only and --except', function () {
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
Route::get('slashes/{slug}', fn () => '')->where('slug', '.*')->name('slashes');

artisan('ziggy:generate --only=slashes --except=postComments.index');

// Options cancel each other out and are ignored
expect(base_path('resources/js/ziggy.js'))->toEqualFile('./tests/fixtures/ziggy.js');
});

test('generate file with custom output formatter', function () {
Route::get('posts/{post}/comments', fn () => '')->name('postComments.index');
Route::get('admin', fn () => '')->name('admin.dashboard'); // Excluded by config
Expand Down Expand Up @@ -162,7 +192,7 @@

class CustomFile extends File
{
function __toString(): string
public function __toString(): string
{
return <<<JAVASCRIPT
// This is a custom template
Expand Down
Loading