Skip to content

Commit 84be2c6

Browse files
Merge pull request #46 from laravel/storage
Test it can import storage routes
2 parents 0ef8d6d + 29f7238 commit 84be2c6

File tree

7 files changed

+51
-29
lines changed

7 files changed

+51
-29
lines changed

src/GenerateCommand.php

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ public function handle()
7878
if (! $this->option('skip-routes')) {
7979
$this->files->deleteDirectory($this->base());
8080

81-
$named = $routes->filter(fn (Route $route) => $route->name() && ! Str::endsWith($route->name(), '.'))->groupBy(fn (Route $route) => $route->name());
81+
$named = $routes->filter(
82+
fn (Route $route) => $route->name() && ! Str::endsWith($route->name(), '.') && ! Str::startsWith($route->name(), 'generated::')
83+
)->groupBy(fn (Route $route) => $route->name());
8284

8385
$named->each($this->writeNamedFile(...));
8486
$named->undot()->each($this->writeBarrelFiles(...));
@@ -98,7 +100,9 @@ private function appendContent($path, $content): void
98100
{
99101
$this->content[$path] ??= [];
100102

101-
$this->content[$path][] = $content;
103+
if (! in_array($content, $this->content[$path])) {
104+
$this->content[$path][] = $content;
105+
}
102106
}
103107

104108
private function prependContent($path, $content): void
@@ -112,7 +116,6 @@ private function writeContent(): void
112116
{
113117
foreach ($this->content as $path => $content) {
114118
$this->files->ensureDirectoryExists(dirname($path));
115-
116119
$this->files->put($path, TypeScript::cleanUp(implode(PHP_EOL, $content)));
117120
}
118121

@@ -193,25 +196,15 @@ private function writeControllerMethodExport(Route $route, string $path): void
193196

194197
private function writeNamedFile(Collection $routes, string $namespace): void
195198
{
196-
$path = join_paths($this->base(), ...explode('.', $namespace)).'.ts';
199+
$parts = explode('.', $namespace);
200+
array_pop($parts);
201+
$parts[] = 'index';
202+
203+
$path = join_paths($this->base(), ...$parts).'.ts';
197204

198205
$this->appendCommonImports($routes, $path, $namespace);
199206

200207
$routes->each(fn (Route $route) => $this->writeNamedMethodExport($route, $path));
201-
202-
$imports = $routes->map(fn (Route $route) => $route->namedMethod())->implode(', ');
203-
204-
$basename = basename($path, '.ts');
205-
206-
$base = TypeScript::safeMethod($basename, 'Route');
207-
208-
if ($base !== $imports) {
209-
$this->appendContent($path, "const {$base} = { {$imports} }\n");
210-
}
211-
212-
if ($base !== 'index') {
213-
$this->appendContent($path, "export default {$base}");
214-
}
215208
}
216209

217210
private function appendCommonImports(Collection $routes, string $path, string $namespace): void
@@ -224,7 +217,7 @@ private function appendCommonImports(Collection $routes, string $path, string $n
224217

225218
$importBase = str_repeat('/..', substr_count($namespace, '.') + 1);
226219

227-
$this->appendContent($path, 'import { '.implode(', ', $imports)." } from '.{$importBase}/wayfinder'\n");
220+
$this->appendContent($path, 'import { '.implode(', ', $imports)." } from '.{$importBase}/wayfinder'".PHP_EOL);
228221
}
229222

230223
private function writeNamedMethodExport(Route $route, string $path): void
@@ -255,13 +248,25 @@ private function writeBarrelFiles(array|Collection $children, string $parent): v
255248

256249
$indexPath = join_paths($this->base(), $parent, 'index.ts');
257250

258-
$childKeys = $children->keys()->mapWithKeys(fn ($child) => [$normalizeToCamelCase($child) => $child]);
251+
$childKeys = $children->keys()->mapWithKeys(fn ($child) => [
252+
$child => [
253+
'safe' => TypeScript::safeMethod($normalizeToCamelCase($child), 'Method'),
254+
'normalized' => $normalizeToCamelCase($child),
255+
],
256+
]);
259257

260-
$imports = $childKeys->filter(fn ($child, $key) => $key !== 'index')->map(fn ($child, $key) => "import {$key} from './{$child}'")->implode(PHP_EOL);
258+
if (! ($this->content[$indexPath] ?? false)) {
259+
$imports = $childKeys->filter(fn ($_, $key) => $key !== 'index')->map(fn ($alias, $key) => "import {$alias['safe']} from './{$key}'")->implode(PHP_EOL);
260+
} else {
261+
$keysWithGrandkids = $children->filter(fn ($grandChildren) => ! array_is_list(collect($grandChildren)->all()));
262+
$imports = $childKeys->only($keysWithGrandkids->keys())->map(fn ($alias, $key) => "import {$alias['safe']} from './{$key}'")->implode(PHP_EOL);
263+
}
261264

262-
$this->prependContent($indexPath, $imports);
265+
if ($imports) {
266+
$this->prependContent($indexPath, $imports);
267+
}
263268

264-
$keys = $childKeys->keys()->map(fn ($key) => str_repeat(' ', 4).$key)->implode(', '.PHP_EOL);
269+
$keys = $childKeys->map(fn ($alias, $key) => str_repeat(' ', 4).implode(': ', array_unique([$alias['normalized'], $alias['safe']])))->implode(', '.PHP_EOL);
265270

266271
$varExport = $normalizeToCamelCase(Str::afterLast($parent, DIRECTORY_SEPARATOR));
267272

testbench.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
providers:
2-
# - Workbench\App\Providers\WorkbenchServiceProvider
2+
- App\Providers\WorkbenchServiceProvider
33

44
workbench:
55
start: '/'

tests/DisallowedMethodNames.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import DisallowedMethodNameController, {
33
deleteMethod,
44
method404,
55
} from "../workbench/resources/js/actions/App/Http/Controllers/DisallowedMethodNameController";
6-
import route404 from "../workbench/resources/js/routes/disallowed/404";
6+
import disallowed from "../workbench/resources/js/routes/disallowed";
77

88
test("will append `method` to invalid methods", () => {
99
expect(method404.url()).toBe("/disallowed/404");
@@ -15,5 +15,5 @@ test("will append `method` to invalid methods", () => {
1515
});
1616

1717
test("will append `method` to invalid methods", () => {
18-
expect(route404.method404.url()).toBe("/disallowed/404");
18+
expect(disallowed[404].url()).toBe("/disallowed/404");
1919
});

tests/EmptyRoute.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, it } from "vitest";
2-
import { home } from "../workbench/resources/js/routes/home";
2+
import { home } from "../workbench/resources/js/routes";
33

44
it("doesn't add a / to an empty route", () => {
55
expect(home.url()).toBe("/");

tests/NamedRoutes.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, it } from "vitest";
2-
import edit from "../workbench/resources/js/routes/posts/edit";
2+
import { edit } from "../workbench/resources/js/routes/posts";
33

44
it("exports named routes", () => {
55
expect(edit.url(1)).toBe("/posts/1/edit");

tests/StorageRoute.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { expect, it } from "vitest";
2+
import storage from "../workbench/resources/js/routes/storage";
3+
4+
it("can import storage routes", () => {
5+
expect(storage.export("file-name")).toEqual({
6+
url: "/storage/file-name",
7+
method: "get",
8+
});
9+
});

workbench/app/Providers/WorkbenchServiceProvider.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Providers;
44

5+
use Illuminate\Support\Facades\Config;
56
use Illuminate\Support\ServiceProvider;
67

78
class WorkbenchServiceProvider extends ServiceProvider
@@ -11,7 +12,14 @@ class WorkbenchServiceProvider extends ServiceProvider
1112
*/
1213
public function register(): void
1314
{
14-
//
15+
Config::set([
16+
'filesystems.disks.export' => [
17+
'driver' => 'local',
18+
'root' => database_path('data/exports'),
19+
'serve' => true,
20+
'throw' => false,
21+
],
22+
]);
1523
}
1624

1725
/**

0 commit comments

Comments
 (0)