|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\AssetMapper\Command; |
| 13 | + |
| 14 | +use Symfony\Component\AssetMapper\ImportMap\ImportMapUpdateChecker; |
| 15 | +use Symfony\Component\AssetMapper\ImportMap\PackageUpdateInfo; |
| 16 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 17 | +use Symfony\Component\Console\Command\Command; |
| 18 | +use Symfony\Component\Console\Input\InputArgument; |
| 19 | +use Symfony\Component\Console\Input\InputInterface; |
| 20 | +use Symfony\Component\Console\Input\InputOption; |
| 21 | +use Symfony\Component\Console\Output\OutputInterface; |
| 22 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 23 | + |
| 24 | +#[AsCommand(name: 'importmap:outdated', description: 'List outdated JavaScript packages and their latest versions')] |
| 25 | +final class ImportMapOutdatedCommand extends Command |
| 26 | +{ |
| 27 | + private const COLOR_MAPPING = [ |
| 28 | + 'update-possible' => 'yellow', |
| 29 | + 'semver-safe-update' => 'red', |
| 30 | + ]; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + private readonly ImportMapUpdateChecker $updateChecker, |
| 34 | + ) { |
| 35 | + parent::__construct(); |
| 36 | + } |
| 37 | + |
| 38 | + protected function configure(): void |
| 39 | + { |
| 40 | + $this |
| 41 | + ->addArgument( |
| 42 | + name: 'packages', |
| 43 | + mode: InputArgument::IS_ARRAY | InputArgument::OPTIONAL, |
| 44 | + description: 'A list of packages to check', |
| 45 | + ) |
| 46 | + ->addOption( |
| 47 | + name: 'format', |
| 48 | + mode: InputOption::VALUE_REQUIRED, |
| 49 | + description: sprintf('The output format ("%s")', implode(', ', $this->getAvailableFormatOptions())), |
| 50 | + default: 'txt', |
| 51 | + ) |
| 52 | + ->setHelp(<<<'EOT' |
| 53 | +The <info>%command.name%</info> command will list the latest updates available for the 3rd party packages in <comment>importmap.php</comment>. |
| 54 | +Versions showing in <fg=red>red</> are semver compatible versions and you should upgrading. |
| 55 | +Versions showing in <fg=yellow>yellow</> are major updates that include backward compatibility breaks according to semver. |
| 56 | +
|
| 57 | + <info>php %command.full_name%</info> |
| 58 | +
|
| 59 | +Or specific packages only: |
| 60 | +
|
| 61 | + <info>php %command.full_name% <packages></info> |
| 62 | +EOT |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 67 | + { |
| 68 | + $io = new SymfonyStyle($input, $output); |
| 69 | + $packages = $input->getArgument('packages'); |
| 70 | + $packagesUpdateInfos = $this->updateChecker->getAvailableUpdates($packages); |
| 71 | + $packagesUpdateInfos = array_filter($packagesUpdateInfos, fn ($packageUpdateInfo) => $packageUpdateInfo->hasUpdate()); |
| 72 | + if (0 === \count($packagesUpdateInfos)) { |
| 73 | + return Command::SUCCESS; |
| 74 | + } |
| 75 | + |
| 76 | + $displayData = array_map(fn ($importName, $packageUpdateInfo) => [ |
| 77 | + 'name' => $importName, |
| 78 | + 'current' => $packageUpdateInfo->currentVersion, |
| 79 | + 'latest' => $packageUpdateInfo->latestVersion, |
| 80 | + 'latest-status' => PackageUpdateInfo::UPDATE_TYPE_MAJOR === $packageUpdateInfo->updateType ? 'update-possible' : 'semver-safe-update', |
| 81 | + ], array_keys($packagesUpdateInfos), $packagesUpdateInfos); |
| 82 | + |
| 83 | + if ('json' === $input->getOption('format')) { |
| 84 | + $io->writeln(json_encode($displayData, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES)); |
| 85 | + } else { |
| 86 | + $table = $io->createTable(); |
| 87 | + $table->setHeaders(['Package', 'Current', 'Latest']); |
| 88 | + foreach ($displayData as $datum) { |
| 89 | + $color = self::COLOR_MAPPING[$datum['latest-status']] ?? 'default'; |
| 90 | + $table->addRow([ |
| 91 | + sprintf('<fg=%s>%s</>', $color, $datum['name']), |
| 92 | + $datum['current'], |
| 93 | + sprintf('<fg=%s>%s</>', $color, $datum['latest']), |
| 94 | + ]); |
| 95 | + } |
| 96 | + $table->render(); |
| 97 | + } |
| 98 | + |
| 99 | + return Command::FAILURE; |
| 100 | + } |
| 101 | + |
| 102 | + private function getAvailableFormatOptions(): array |
| 103 | + { |
| 104 | + return ['txt', 'json']; |
| 105 | + } |
| 106 | +} |
0 commit comments