Skip to content

Commit 4d57c41

Browse files
jmschefabpot
authored andcommitted
[AssetMapper] Allow specifying packages to update with importmap:update
1 parent 73a6b4b commit 4d57c41

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
@@ -88,7 +88,7 @@ public function getImportMapJson(): string
8888
*/
8989
public function require(array $packages): array
9090
{
91-
return $this->updateImportMapConfig(false, $packages, []);
91+
return $this->updateImportMapConfig(false, $packages, [], []);
9292
}
9393

9494
/**
@@ -98,15 +98,17 @@ public function require(array $packages): array
9898
*/
9999
public function remove(array $packages): void
100100
{
101-
$this->updateImportMapConfig(false, [], $packages);
101+
$this->updateImportMapConfig(false, [], $packages, []);
102102
}
103103

104104
/**
105-
* Updates all existing packages to the latest version.
105+
* Updates either all existing packages or the specified ones to the latest version.
106+
*
107+
* @return ImportMapEntry[]
106108
*/
107-
public function update(): array
109+
public function update(array $packages = []): array
108110
{
109-
return $this->updateImportMapConfig(true, [], []);
111+
return $this->updateImportMapConfig(true, [], [], $packages);
110112
}
111113

112114
/**
@@ -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
@@ -373,6 +373,64 @@ public function testUpdate()
373373
$this->assertSame('contents of cowsay.js', $actualContents);
374374
}
375375

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