forked from AzuraCast/AzuraCast
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to auto-generated enums from PHP in TypeScript.
- Loading branch information
1 parent
1370da5
commit 33a7968
Showing
36 changed files
with
525 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
backend/src/Console/Command/Dev/GenerateTypescriptClassesCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Console\Command\Dev; | ||
|
||
use App\Console\Command\CommandAbstract; | ||
use App\Container\EnvironmentAwareTrait; | ||
use App\Container\LoggerAwareTrait; | ||
use App\TypeScript\NativeJsEnumTransformer; | ||
use App\TypeScript\NativeJsWriter; | ||
use Spatie\TypeScriptTransformer\Formatters\PrettierFormatter; | ||
use Spatie\TypeScriptTransformer\TypeScriptTransformer; | ||
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
#[AsCommand( | ||
name: 'azuracast:dev:ts', | ||
description: 'Trigger generation of TypeScript equivalents of PHP classes.', | ||
)] | ||
final class GenerateTypescriptClassesCommand extends CommandAbstract | ||
{ | ||
use LoggerAwareTrait; | ||
use EnvironmentAwareTrait; | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$backendBaseDir = $this->environment->getBackendDirectory(); | ||
$outputFile = $this->environment->getBaseDirectory() . '/frontend/entities/PhpClasses.ts'; | ||
|
||
$config = TypeScriptTransformerConfig::create() | ||
->autoDiscoverTypes($backendBaseDir) | ||
->transformers([ | ||
NativeJsEnumTransformer::class, | ||
]) | ||
->transformToNativeEnums() | ||
->writer(NativeJsWriter::class) | ||
->formatter(PrettierFormatter::class) | ||
->outputFile($outputFile); | ||
|
||
TypeScriptTransformer::create($config)->transform(); | ||
|
||
$io->writeln('TypeScript classes generated!'); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\TypeScript; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Ignore a specific enum case. | ||
*/ | ||
#[Attribute(Attribute::TARGET_CLASS_CONSTANT)] | ||
final class IgnoreCase | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\TypeScript; | ||
|
||
use ReflectionEnum; | ||
use ReflectionEnumBackedCase; | ||
use Spatie\TypeScriptTransformer\Structures\TransformedType; | ||
use Spatie\TypeScriptTransformer\Transformers\EnumTransformer; | ||
|
||
final class NativeJsEnumTransformer extends EnumTransformer | ||
{ | ||
protected function toEnum(ReflectionEnum $enum, string $name): TransformedType | ||
{ | ||
/** @var ReflectionEnumBackedCase[] $enumCases */ | ||
$enumCases = $enum->getCases(); | ||
|
||
$options = array_filter( | ||
array_map( | ||
function (ReflectionEnumBackedCase $case): ?string { | ||
$hiddenAttributes = $case->getAttributes(IgnoreCase::class); | ||
return (empty($hiddenAttributes)) | ||
? "'{$case->getName()}': {$this->toEnumValue($case)}" | ||
: null; | ||
}, | ||
$enumCases | ||
) | ||
); | ||
|
||
return TransformedType::create( | ||
$enum, | ||
$name, | ||
'Object.freeze({ ' . implode(', ', $options) . ' } as const)', | ||
keyword: 'const' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\TypeScript; | ||
|
||
use Spatie\TypeScriptTransformer\Structures\TransformedType; | ||
use Spatie\TypeScriptTransformer\Structures\TypesCollection; | ||
use Spatie\TypeScriptTransformer\Writers\ModuleWriter; | ||
|
||
final class NativeJsWriter extends ModuleWriter | ||
{ | ||
public function format(TypesCollection $collection): string | ||
{ | ||
$output = <<<'TS' | ||
/* | ||
* This file is automatically generated by AzuraCast. | ||
* To update it, run: | ||
* `backend/bin/console azuracast:dev:ts` | ||
*/ | ||
|
||
|
||
TS; | ||
|
||
$iterator = $collection->getIterator(); | ||
$iterator->uasort(function (TransformedType $a, TransformedType $b) { | ||
return strcmp($a->name ?? '', $b->name ?? ''); | ||
}); | ||
|
||
/** @var TransformedType $type */ | ||
foreach ($iterator as $type) { | ||
if ($type->isInline) { | ||
continue; | ||
} | ||
|
||
if ($type->keyword === 'const') { | ||
$this->handleNativeJsEnum($type, $output); | ||
} else { | ||
$output .= "export {$type->toString()}" . PHP_EOL . PHP_EOL; | ||
} | ||
} | ||
|
||
return $output; | ||
} | ||
|
||
protected function handleNativeJsEnum(TransformedType $type, string &$output): void | ||
{ | ||
$output .= "export const {$type->name} = {$type->transformed}"; | ||
if ($type->trailingSemicolon) { | ||
$output .= ';'; | ||
} | ||
$output .= PHP_EOL; | ||
|
||
$output .= "export type {$type->name}Enum = typeof {$type->name}[keyof typeof {$type->name}]"; | ||
if ($type->trailingSemicolon) { | ||
$output .= ';'; | ||
} | ||
$output .= PHP_EOL . PHP_EOL; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.