Skip to content

Commit

Permalink
Merge pull request #687 from tighten/jbk/file-path-improvements
Browse files Browse the repository at this point in the history
Handle generated file extensions more robustly
  • Loading branch information
bakerkretzmar authored Nov 6, 2023
2 parents 0b34dcc + 0ac7f55 commit 125a990
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/CommandRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down
45 changes: 45 additions & 0 deletions tests/Unit/CommandRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', ['path' => 'resources/scripts/x.js', '--types' => true]);

$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', ['path' => 'resources/scripts/y.ts', '--types' => true]);

$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', ['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'));
}

/** @test */
public function can_generate_correct_file_extensions_from_ambiguous_path_argument()
{
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'));
}

/** @test */
public function can_generate_correct_file_extensions_from_directory_path_argument()
{
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'));
}
}

class CustomFileFormatter extends File
Expand Down

0 comments on commit 125a990

Please sign in to comment.