Skip to content

Commit

Permalink
Merge pull request #80 from red-freak/feature/echo-to-file
Browse files Browse the repository at this point in the history
added output-file argument to command
  • Loading branch information
tcampbPPU authored Nov 7, 2024
2 parents c6a59a6 + 3b6f466 commit 527cffc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ public function getFirstNameAttribute(): string // <- this
}
```

### Optional Arguments

```
output-file=./resources/js/types/models.d.ts : Echo the definitions into a file
```

### Additional Options

```
Expand Down
25 changes: 22 additions & 3 deletions src/Commands/ModelTyperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use FumeApp\ModelTyper\Actions\Generator;
use FumeApp\ModelTyper\Exceptions\ModelTyperException;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'model:typer')]
Expand All @@ -17,12 +18,20 @@ class ModelTyperCommand extends Command
*/
protected $name = 'model:typer';

/**
* Facade for Filesystem-Access
*
* @var Filesystem
*/
protected $files;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'model:typer
{output-file=./resources/js/types/models.d.ts : Echo the definitions into a file}
{--model= : Generate typescript interfaces for a specific model}
{--global : Generate typescript interfaces in a global namespace named models}
{--json : Output the result as json}
Expand Down Expand Up @@ -51,9 +60,11 @@ class ModelTyperCommand extends Command
*
* @return void
*/
public function __construct()
public function __construct(Filesystem $files)
{
parent::__construct();

$this->files = $files;
}

/**
Expand All @@ -62,7 +73,8 @@ public function __construct()
public function handle(Generator $generator): int
{
try {
$this->line($generator(
$path = $this->argument('output-file');
$output = $generator(
$this->option('model'),
$this->option('global'),
$this->option('json'),
Expand All @@ -77,7 +89,14 @@ public function handle(Generator $generator): int
$this->option('resolve-abstract'),
$this->option('fillables'),
$this->option('fillable-suffix')
));
);

if ($path) {
$this->files->ensureDirectoryExists(dirname($path));
$this->files->put($path, $output);
}

$this->line($output);
} catch (ModelTyperException $exception) {
$this->error($exception->getMessage());

Expand Down
4 changes: 4 additions & 0 deletions src/ModelTyperServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function boot(): void
ShowModelCommand::class,
]);
}

$this->app->singleton(ModelTyperCommand::class, function ($app) {
return new ModelTyperCommand($app['files']);
});
}

/**
Expand Down

0 comments on commit 527cffc

Please sign in to comment.