Using the transformer without artisan #26
-
I think it would be great to be able to use the transformer without artisan, e.g. in non-laravel apps or in laravel packages that don't have artisan installed. Maybe this could be achieved by providing a more general CLI as a vendor binary? The path to the config file could be provided as an argument. If you are open to the idea, I'd be happy to give it a shot and create a pull request. Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
To keep things simple, we're going to keep a CLI outside the scope of this package. Feel free to create a repo of your own in which you create the CLI and require the things you need from our package. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply! I currently don't have the capacity to be a maintainer for a package like this, so I went with a different route and created a custom binary in my project. For anyone looking to do the same, here is the code: #!/usr/bin/env php
<?php
require __DIR__ . '/../vendor/autoload.php';
use Spatie\TypeScriptTransformer\Formatters\PrettierFormatter;
use Spatie\TypeScriptTransformer\Structures\TransformedType;
use Spatie\TypeScriptTransformer\TypeScriptTransformer;
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\SingleCommandApplication;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* Transformers take PHP classes(e.g., enums) as an input and will output
* a TypeScript representation of the PHP class.
*/
$transformers = [
Spatie\TypeScriptTransformer\Transformers\SpatieEnumTransformer::class,
Spatie\TypeScriptTransformer\Transformers\DtoTransformer::class,
];
(new SingleCommandApplication())
->setName('TypeScript Transform')
->addArgument('output_file', InputArgument::REQUIRED, 'Writes the generated TypeScript to this file')
->addArgument('paths', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Paths with classes to transform')
->addOption('enums', 'e', InputOption::VALUE_NONE, 'Use native TypeScript enums')
->addOption('format', 'f', InputOption::VALUE_NONE, 'Format the output')
->setCode(function (
InputInterface $input,
OutputInterface $output
) use ($transformers): int {
$io = new SymfonyStyle($input, $output);
$config = TypeScriptTransformerConfig::create()
->autoDiscoverTypes(...$input->getArgument('paths'))
->transformers($transformers)
->transformToNativeEnums($input->getOption('enums'))
->formatter($input->getOption('format') ? PrettierFormatter::class : null)
->outputFile($input->getArgument('output_file'));
$transformer = new TypeScriptTransformer($config);
try {
$results = $transformer->transform();
} catch (Exception $exception) {
$io->error($exception->getMessage());
return 1;
}
$io->table(
['PHP class', 'TypeScript entity'],
array_map(
fn (string $class, TransformedType $type) => [$class, $type->getTypeScriptName()],
array_keys((array) $results->getIterator()),
array_values((array) $results->getIterator())
)
);
$io->info("Transformed {$results->count()} PHP types to TypeScript");
return 0;
})
->run(); This code goes in a Run this to see the available options: ./bin/typescript-transform --help Which will print the following information:
Use it like this: ./bin/typescript-transform --format resources/types/generated.d.ts src Hope that helps! |
Beta Was this translation helpful? Give feedback.
To keep things simple, we're going to keep a CLI outside the scope of this package.
Feel free to create a repo of your own in which you create the CLI and require the things you need from our package.