Skip to content

Commit 1016a5d

Browse files
authored
feat: Add multiple imports management
1 parent 78f86fb commit 1016a5d

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

src/GenerateCommand.php

Lines changed: 22 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 = [];
@@ -216,7 +230,14 @@ private function appendCommonImports(Collection $routes, string $path, string $n
216230

217231
$importBase = str_repeat('/..', substr_count($namespace, '.') + 1);
218232

219-
$this->appendContent($path, 'import { '.implode(', ', $imports)." } from '.{$importBase}/wayfinder'".PHP_EOL);
233+
if (!isset($this->imports[$path])) {
234+
$this->imports[$path] = [];
235+
}
236+
237+
$this->imports[$path][".{$importBase}/wayfinder"] = [
238+
...($this->imports[$path][".{$importBase}/wayfinder"] ?? []),
239+
...$imports
240+
];
220241
}
221242

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

0 commit comments

Comments
 (0)