From e2b0a795b1eaabda0d5f6444f7b8cb0d25569515 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Thu, 2 Nov 2023 17:24:31 -0400 Subject: [PATCH 1/2] Support more path argument formats --- src/CommandRouteGenerator.php | 16 ++++++--- tests/Unit/CommandRouteGeneratorTest.php | 45 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/CommandRouteGenerator.php b/src/CommandRouteGenerator.php index 0ed4a3b2..0ad1cc35 100644 --- a/src/CommandRouteGenerator.php +++ b/src/CommandRouteGenerator.php @@ -33,20 +33,26 @@ public function handle() { $ziggy = new Ziggy($this->option('group'), $this->option('url') ? url($this->option('url')) : null); - $this->makeDirectory( - $path = $this->argument('path') ?? config('ziggy.output.path', 'resources/js/ziggy.js') - ); + $path = $this->argument('path') ?? config('ziggy.output.path', 'resources/js/ziggy.js'); + + if ($this->files->isDirectory(base_path($path))) { + $path .= '/ziggy'; + } else { + $this->makeDirectory($path); + } + + $name = preg_replace('/(\.d)?\.ts$|\.js$/', '', $path); if (! $this->option('types-only')) { $output = config('ziggy.output.file', File::class); - $this->files->put(base_path($path), new $output($ziggy)); + $this->files->put(base_path("{$name}.js"), new $output($ziggy)); } if ($this->option('types') || $this->option('types-only')) { $types = config('ziggy.output.types', Types::class); - $this->files->put(base_path(Str::replaceLast('.js', '.d.ts', $path)), new $types($ziggy)); + $this->files->put(base_path("{$name}.d.ts"), new $types($ziggy)); } $this->info('Files generated!'); diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index a0060f60..c663a847 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -213,6 +213,51 @@ public function can_derive_dts_file_path_from_given_path() $this->assertFileExists(base_path('resources/js/custom.d.ts')); $this->assertFileNotExists(base_path('resources/js/ziggy.d.ts')); } + + /** @test */ + public function can_generate_correct_file_extensions_from_js_path_argument() + { + Artisan::call('ziggy:generate resources/scripts/x.js --types'); + + $this->assertFileExists(base_path('resources/scripts/x.js')); + $this->assertFileExists(base_path('resources/scripts/x.d.ts')); + } + + /** @test */ + public function can_generate_correct_file_extensions_from_ts_path_argument() + { + Artisan::call('ziggy:generate resources/scripts/y.ts --types'); + + $this->assertFileExists(base_path('resources/scripts/y.js')); + $this->assertFileExists(base_path('resources/scripts/y.d.ts')); + } + + /** @test */ + public function can_generate_correct_file_extensions_from_dts_path_argument() + { + Artisan::call('ziggy:generate resources/scripts/z.d.ts --types'); + + $this->assertFileExists(base_path('resources/scripts/z.js')); + $this->assertFileExists(base_path('resources/scripts/z.d.ts')); + } + + /** @test */ + public function can_generate_correct_file_extensions_from_ambiguous_path_argument() + { + Artisan::call('ziggy:generate resources/scripts/foo --types'); + + $this->assertFileExists(base_path('resources/scripts/foo.js')); + $this->assertFileExists(base_path('resources/scripts/foo.d.ts')); + } + + /** @test */ + public function can_generate_correct_file_extensions_from_directory_path_argument() + { + Artisan::call('ziggy:generate resources/js --types'); + + $this->assertFileExists(base_path('resources/js/ziggy.js')); + $this->assertFileExists(base_path('resources/js/ziggy.d.ts')); + } } class CustomFileFormatter extends File From 0ac7f55b21812f4e7baec69efce3d7e2e57e9183 Mon Sep 17 00:00:00 2001 From: Jacob Baker-Kretzmar Date: Thu, 2 Nov 2023 17:40:40 -0400 Subject: [PATCH 2/2] Fix `Artisan::call()` syntax --- tests/Unit/CommandRouteGeneratorTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Unit/CommandRouteGeneratorTest.php b/tests/Unit/CommandRouteGeneratorTest.php index c663a847..1cd3fded 100644 --- a/tests/Unit/CommandRouteGeneratorTest.php +++ b/tests/Unit/CommandRouteGeneratorTest.php @@ -217,7 +217,7 @@ public function can_derive_dts_file_path_from_given_path() /** @test */ public function can_generate_correct_file_extensions_from_js_path_argument() { - Artisan::call('ziggy:generate resources/scripts/x.js --types'); + Artisan::call('ziggy:generate', ['path' => 'resources/scripts/x.js', '--types' => true]); $this->assertFileExists(base_path('resources/scripts/x.js')); $this->assertFileExists(base_path('resources/scripts/x.d.ts')); @@ -226,7 +226,7 @@ public function can_generate_correct_file_extensions_from_js_path_argument() /** @test */ public function can_generate_correct_file_extensions_from_ts_path_argument() { - Artisan::call('ziggy:generate resources/scripts/y.ts --types'); + Artisan::call('ziggy:generate', ['path' => 'resources/scripts/y.ts', '--types' => true]); $this->assertFileExists(base_path('resources/scripts/y.js')); $this->assertFileExists(base_path('resources/scripts/y.d.ts')); @@ -235,7 +235,7 @@ public function can_generate_correct_file_extensions_from_ts_path_argument() /** @test */ public function can_generate_correct_file_extensions_from_dts_path_argument() { - Artisan::call('ziggy:generate resources/scripts/z.d.ts --types'); + Artisan::call('ziggy:generate', ['path' => 'resources/scripts/z.d.ts', '--types' => true]); $this->assertFileExists(base_path('resources/scripts/z.js')); $this->assertFileExists(base_path('resources/scripts/z.d.ts')); @@ -244,7 +244,7 @@ public function can_generate_correct_file_extensions_from_dts_path_argument() /** @test */ public function can_generate_correct_file_extensions_from_ambiguous_path_argument() { - Artisan::call('ziggy:generate resources/scripts/foo --types'); + Artisan::call('ziggy:generate', ['path' => 'resources/scripts/foo', '--types' => true]); $this->assertFileExists(base_path('resources/scripts/foo.js')); $this->assertFileExists(base_path('resources/scripts/foo.d.ts')); @@ -253,7 +253,7 @@ public function can_generate_correct_file_extensions_from_ambiguous_path_argumen /** @test */ public function can_generate_correct_file_extensions_from_directory_path_argument() { - Artisan::call('ziggy:generate resources/js --types'); + Artisan::call('ziggy:generate', ['path' => 'resources/js', '--types' => true]); $this->assertFileExists(base_path('resources/js/ziggy.js')); $this->assertFileExists(base_path('resources/js/ziggy.d.ts'));