Skip to content

Commit 3f69511

Browse files
Merge pull request #63 from maicol07/patch-1
feat: Add multiple imports handling
2 parents b5a5b92 + a3c4d39 commit 3f69511

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

.eslintrc.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"env": {
3+
"es2022": true,
4+
"node": true
5+
},
6+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
7+
"parser": "@typescript-eslint/parser",
8+
"parserOptions": {
9+
"ecmaVersion": "latest",
10+
"sourceType": "module"
11+
},
12+
"plugins": ["@typescript-eslint", "import"],
13+
"rules": {
14+
"import/no-duplicates": "error"
15+
}
16+
}

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
{
22
"type": "module",
33
"scripts": {
4-
"test": "vitest run && npm run test-routes-cached",
5-
"test-routes-cached": "WAYFINDER_CACHE_ROUTES=1 vitest run"
4+
"test": "vitest run && npm run lint && npm run test-routes-cached && npm run lint",
5+
"test-routes-cached": "WAYFINDER_CACHE_ROUTES=1 vitest run",
6+
"lint": "eslint ./workbench/resources/js --ext .ts,.tsx",
7+
"lint:fix": "eslint ./workbench/resources/js --ext .ts,.tsx --fix"
68
},
79
"devDependencies": {
810
"@types/node": "^22.7.5",
11+
"@typescript-eslint/eslint-plugin": "^7.0.0",
12+
"@typescript-eslint/parser": "^7.0.0",
13+
"eslint": "^8.56.0",
14+
"eslint-plugin-import": "^2.29.1",
915
"happy-dom": "^15.11.7",
1016
"vitest": "^2.1.3"
1117
}

src/GenerateCommand.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ class GenerateCommand extends Command
2929

3030
private $content = [];
3131

32+
/**
33+
* Imports array where the key is the generated file path and the value is an array of imports.
34+
* Each import is an array where the first element is the import path and the second element is an array of imported items.
35+
*
36+
* @var array<string, array<string, string[]>>
37+
*/
38+
private $imports = [];
39+
3240
public function __construct(
3341
private Filesystem $files,
3442
private Router $router,
@@ -115,6 +123,12 @@ private function writeContent(): void
115123
foreach ($this->content as $path => $content) {
116124
$this->files->ensureDirectoryExists(dirname($path));
117125
$this->files->put($path, TypeScript::cleanUp(implode(PHP_EOL, $content)));
126+
127+
// Prepend the imports to the file
128+
if (isset($this->imports[$path])) {
129+
$importLines = collect($this->imports[$path])->map(fn ($imports, $key) => 'import { '.implode(', ', array_unique($imports))." } from '{$key}'")->implode(PHP_EOL);
130+
$this->files->prepend($path, $importLines.PHP_EOL);
131+
}
118132
}
119133

120134
$this->content = [];
@@ -215,8 +229,13 @@ private function appendCommonImports(Collection $routes, string $path, string $n
215229
}
216230

217231
$importBase = str_repeat('/..', substr_count($namespace, '.') + 1);
232+
$pathKey = ".{$importBase}/wayfinder";
218233

219-
$this->appendContent($path, 'import { '.implode(', ', $imports)." } from '.{$importBase}/wayfinder'".PHP_EOL);
234+
$this->imports[$path] ??= [];
235+
$this->imports[$path][$pathKey] = [
236+
...($this->imports[$path][$pathKey] ?? []),
237+
...$imports,
238+
];
220239
}
221240

222241
private function writeNamedMethodExport(Route $route, string $path): void

workbench/routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
return 'Home';
4242
})->name('home');
4343

44-
Route::post('/optional/{parameter?}', [OptionalController::class, 'optional']);
44+
Route::post('/optional/{parameter?}', [OptionalController::class, 'optional'])->name('optional');
4545
Route::post('/many-optional/{one?}/{two?}/{three?}', [OptionalController::class, 'manyOptional']);
4646

4747
Route::post('/users/{user}', [ModelBindingController::class, 'show']);

0 commit comments

Comments
 (0)