Skip to content

Commit

Permalink
Merge pull request #805 from joshmanders/feature/support-passing-exce…
Browse files Browse the repository at this point in the history
…pt-only-args-to-ziggy-command

feat: support passing except/only args to ziggy command
  • Loading branch information
bakerkretzmar authored Jan 22, 2025
2 parents 6612c8c + 33a4b16 commit ee0c18c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
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

0 comments on commit ee0c18c

Please sign in to comment.