Skip to content

Commit 2cfaa9d

Browse files
committed
feature #51523 [AssetMapper] Allow specifying packages to update with importmap:update (jmsche)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [AssetMapper] Allow specifying packages to update with importmap:update | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A Currently, the `importmap:update` command updates all packages. This PR allows specifying which packages the developer wants to update. Commits ------- 4d57c41 [AssetMapper] Allow specifying packages to update with importmap:update
2 parents 726883c + 4d57c41 commit 2cfaa9d

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

src/Symfony/Component/AssetMapper/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Mark the component as non experimental
88
* Add a `importmap:install` command to download all missing downloaded packages
9+
* Allow specifying packages to update for the `importmap:update` command
910

1011
6.3
1112
---

src/Symfony/Component/AssetMapper/Command/ImportMapUpdateCommand.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,19 @@
1111

1212
namespace Symfony\Component\AssetMapper\Command;
1313

14+
use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry;
1415
use Symfony\Component\AssetMapper\ImportMap\ImportMapManager;
1516
use Symfony\Component\Console\Attribute\AsCommand;
1617
use Symfony\Component\Console\Command\Command;
18+
use Symfony\Component\Console\Input\InputArgument;
1719
use Symfony\Component\Console\Input\InputInterface;
1820
use Symfony\Component\Console\Output\OutputInterface;
1921
use Symfony\Component\Console\Style\SymfonyStyle;
2022

2123
/**
2224
* @author Kévin Dunglas <kevin@dunglas.dev>
2325
*/
24-
#[AsCommand(name: 'importmap:update', description: 'Updates all JavaScript packages to their latest versions')]
26+
#[AsCommand(name: 'importmap:update', description: 'Updates JavaScript packages to their latest versions')]
2527
final class ImportMapUpdateCommand extends Command
2628
{
2729
public function __construct(
@@ -33,21 +35,37 @@ public function __construct(
3335
protected function configure(): void
3436
{
3537
$this
38+
->addArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'List of packages\' names')
3639
->setHelp(<<<'EOT'
3740
The <info>%command.name%</info> command will update all from the 3rd part packages
3841
in <comment>importmap.php</comment> to their latest version, including downloaded packages.
3942
4043
<info>php %command.full_name%</info>
44+
45+
Or specific packages only:
46+
47+
<info>php %command.full_name% <packages></info>
4148
EOT
42-
);
49+
)
50+
;
4351
}
4452

4553
protected function execute(InputInterface $input, OutputInterface $output): int
4654
{
55+
$packages = $input->getArgument('packages');
56+
4757
$io = new SymfonyStyle($input, $output);
48-
$this->importMapManager->update();
58+
$updatedPackages = $this->importMapManager->update($packages);
4959

50-
$io->success('Updated all packages in importmap.php.');
60+
if (0 < \count($packages)) {
61+
$io->success(sprintf(
62+
'Updated %s package%s in importmap.php.',
63+
implode(', ', array_map(static fn (ImportMapEntry $entry): string => $entry->importName, $updatedPackages)),
64+
1 < \count($updatedPackages) ? 's' : '',
65+
));
66+
} else {
67+
$io->success('Updated all packages in importmap.php.');
68+
}
5169

5270
return Command::SUCCESS;
5371
}

src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function getImportMapJson(): string
9292
*/
9393
public function require(array $packages): array
9494
{
95-
return $this->updateImportMapConfig(false, $packages, []);
95+
return $this->updateImportMapConfig(false, $packages, [], []);
9696
}
9797

9898
/**
@@ -102,15 +102,17 @@ public function require(array $packages): array
102102
*/
103103
public function remove(array $packages): void
104104
{
105-
$this->updateImportMapConfig(false, [], $packages);
105+
$this->updateImportMapConfig(false, [], $packages, []);
106106
}
107107

108108
/**
109-
* Updates all existing packages to the latest version.
109+
* Updates either all existing packages or the specified ones to the latest version.
110+
*
111+
* @return ImportMapEntry[]
110112
*/
111-
public function update(): array
113+
public function update(array $packages = []): array
112114
{
113-
return $this->updateImportMapConfig(true, [], []);
115+
return $this->updateImportMapConfig(true, [], [], $packages);
114116
}
115117

116118
/**
@@ -190,7 +192,7 @@ private function buildImportMapJson(): void
190192
*
191193
* @return ImportMapEntry[]
192194
*/
193-
private function updateImportMapConfig(bool $update, array $packagesToRequire, array $packagesToRemove): array
195+
private function updateImportMapConfig(bool $update, array $packagesToRequire, array $packagesToRemove, array $packagesToUpdate): array
194196
{
195197
$currentEntries = $this->loadImportMapEntries();
196198

@@ -205,7 +207,7 @@ private function updateImportMapConfig(bool $update, array $packagesToRequire, a
205207

206208
if ($update) {
207209
foreach ($currentEntries as $importName => $entry) {
208-
if (null === $entry->url) {
210+
if (null === $entry->url || (0 !== \count($packagesToUpdate) && !\in_array($importName, $packagesToUpdate, true))) {
209211
continue;
210212
}
211213

src/Symfony/Component/AssetMapper/Tests/ImportMap/ImportMapManagerTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,64 @@ public function testUpdate()
376376
$this->assertSame('contents of cowsay.js', $actualContents);
377377
}
378378

379+
public function testUpdateWithSpecificPackages()
380+
{
381+
$rootDir = __DIR__.'/../fixtures/importmaps_for_writing';
382+
$manager = $this->createImportMapManager(['assets' => ''], $rootDir);
383+
384+
$map = [
385+
'lodash' => [
386+
'url' => 'https://ga.jspm.io/npm:lodash@1.2.3/lodash.js',
387+
],
388+
'cowsay' => [
389+
'url' => 'https://ga.jspm.io/npm:cowsay@4.5.6/cowsay.umd.js',
390+
'downloaded_to' => 'vendor/cowsay.js',
391+
],
392+
'bootstrap' => [
393+
'url' => 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.esm.js',
394+
'preload' => true,
395+
],
396+
'app' => [
397+
'path' => 'app.js',
398+
],
399+
];
400+
$mapString = var_export($map, true);
401+
file_put_contents($rootDir.'/importmap.php', "<?php\n\nreturn {$mapString};\n");
402+
$this->filesystem->mkdir($rootDir.'/assets/vendor');
403+
file_put_contents($rootDir.'/assets/vendor/cowsay.js', 'cowsay.js original contents');
404+
file_put_contents($rootDir.'/assets/app.js', 'app.js contents');
405+
406+
$this->packageResolver->expects($this->once())
407+
->method('resolvePackages')
408+
->willReturn([
409+
self::resolvedPackage('cowsay', 'https://ga.jspm.io/npm:cowsay@4.5.9/cowsay.umd.js', download: true, content: 'updated contents of cowsay.js'),
410+
])
411+
;
412+
413+
$manager->update(['cowsay']);
414+
$actualImportMap = require $rootDir.'/importmap.php';
415+
$expectedImportMap = [
416+
'lodash' => [
417+
'url' => 'https://ga.jspm.io/npm:lodash@1.2.3/lodash.js',
418+
],
419+
'cowsay' => [
420+
'url' => 'https://ga.jspm.io/npm:cowsay@4.5.9/cowsay.umd.js',
421+
'downloaded_to' => 'vendor/cowsay.js',
422+
],
423+
'bootstrap' => [
424+
'url' => 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.esm.js',
425+
'preload' => true,
426+
],
427+
'app' => [
428+
'path' => 'app.js',
429+
],
430+
];
431+
$this->assertEquals($expectedImportMap, $actualImportMap);
432+
$this->assertFileExists($rootDir.'/assets/vendor/cowsay.js');
433+
$actualContents = file_get_contents($rootDir.'/assets/vendor/cowsay.js');
434+
$this->assertSame('updated contents of cowsay.js', $actualContents);
435+
}
436+
379437
public function testDownloadMissingPackages()
380438
{
381439
$rootDir = __DIR__.'/../fixtures/download';

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy