Skip to content

Commit 7fe28ba

Browse files
committed
wip
1 parent a84e66d commit 7fe28ba

File tree

5 files changed

+30
-27
lines changed

5 files changed

+30
-27
lines changed

src/GenerateCommand.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ private function appendContent($path, $content): void
9898
{
9999
$this->content[$path] ??= [];
100100

101-
$this->content[$path][] = $content;
101+
if (! in_array($content, $this->content[$path])) {
102+
$this->content[$path][] = $content;
103+
}
102104
}
103105

104106
private function prependContent($path, $content): void
@@ -112,7 +114,6 @@ private function writeContent(): void
112114
{
113115
foreach ($this->content as $path => $content) {
114116
$this->files->ensureDirectoryExists(dirname($path));
115-
116117
$this->files->put($path, TypeScript::cleanUp(implode(PHP_EOL, $content)));
117118
}
118119

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

194195
private function writeNamedFile(Collection $routes, string $namespace): void
195196
{
196-
$path = join_paths($this->base(), ...explode('.', $namespace)).'.ts';
197+
$parts = explode('.', $namespace);
198+
array_pop($parts);
199+
$parts[] = 'index';
200+
201+
$path = join_paths($this->base(), ...$parts).'.ts';
197202

198203
$this->appendCommonImports($routes, $path, $namespace);
199204

200205
$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-
}
215206
}
216207

217208
private function appendCommonImports(Collection $routes, string $path, string $namespace): void
@@ -224,7 +215,7 @@ private function appendCommonImports(Collection $routes, string $path, string $n
224215

225216
$importBase = str_repeat('/..', substr_count($namespace, '.') + 1);
226217

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

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

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

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

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

262-
$this->prependContent($indexPath, $imports);
263+
if ($imports) {
264+
$this->prependContent($indexPath, $imports);
265+
}
263266

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

266269
$varExport = $normalizeToCamelCase(Str::afterLast($parent, DIRECTORY_SEPARATOR));
267270

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect, it } from "vitest";
22
import storage from "../workbench/resources/js/routes/storage";
33

44
it("can import storage routes", () => {
5-
expect(storage.export('file-name')).toEqual({
5+
expect(storage.export("file-name")).toEqual({
66
url: "/storage/file-name",
77
method: "get",
88
});

0 commit comments

Comments
 (0)