Skip to content

Commit

Permalink
🎨 readme update for global namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Oct 21, 2021
1 parent d0e2369 commit c7419f1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
20 changes: 20 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,23 @@ You can also specify an interface is nullable:
],
];
```


### Declare global
Generate your interfaces in a global namespace named `model`
```bash
artisn model:typer --global
```

```ts
export {}
declare global {
export namespace models {

export interface Provider {
// columns
id: number
user_id: number
avatar?: string
...
```
2 changes: 1 addition & 1 deletion src/Commands/ModelTyper.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct()
*/
public function handle()
{
echo (new ModelInterface($this->option('global') !== null))->generate();
echo (new ModelInterface($this->option('global')))->generate();
return 0;
}
}
13 changes: 7 additions & 6 deletions src/ModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ public function __construct(
*/
public function generate(): string
{
$allCode = '';
$models = $this->getModels();
$allCode = $this->getImports($models);

if ($this->global) {
$allCode = "export {}\ndeclare global {\n export namespace models {\n\n";
$allCode .= "export {}\ndeclare global {\n export namespace models {\n\n";
$this->space = ' ';
}
$models = $this->getModels();
$allCode .= $this->getImports($models);

foreach ($models as $model) {
$interface = $this->getInterface(new $model());
$allCode .= $this->getCode($interface);
Expand All @@ -84,14 +85,14 @@ private function getImports(Collection $models): string {
foreach ($models as $model) {
if ($interfaces = (new $model())->interfaces) {
foreach ($interfaces as $interface) {
if (isset($interfaces['import'])) {
if (isset($interface['import'])) {
$imports[ $interface[ 'import' ] ][] = $interface[ 'name' ];
}
}
}
}
foreach ($imports as $import=>$names) {
$code .= "{$this->space}import { " . join(', ', array_unique($names)) . " } from '$import'\n";
$code .= "import { " . join(', ', array_unique($names)) . " } from '$import'\n";
}
return $code;
}
Expand Down

0 comments on commit c7419f1

Please sign in to comment.