Skip to content

[AssetMapper] Allow specifying packages to update with importmap:update #51523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/AssetMapper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

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

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@

namespace Symfony\Component\AssetMapper\Command;

use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry;
use Symfony\Component\AssetMapper\ImportMap\ImportMapManager;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* @author Kévin Dunglas <kevin@dunglas.dev>
*/
#[AsCommand(name: 'importmap:update', description: 'Updates all JavaScript packages to their latest versions')]
#[AsCommand(name: 'importmap:update', description: 'Updates JavaScript packages to their latest versions')]
final class ImportMapUpdateCommand extends Command
{
public function __construct(
Expand All @@ -33,21 +35,37 @@ public function __construct(
protected function configure(): void
{
$this
->addArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'List of packages\' names')
->setHelp(<<<'EOT'
The <info>%command.name%</info> command will update all from the 3rd part packages
in <comment>importmap.php</comment> to their latest version, including downloaded packages.

<info>php %command.full_name%</info>

Or specific packages only:

<info>php %command.full_name% <packages></info>
EOT
);
)
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$packages = $input->getArgument('packages');

$io = new SymfonyStyle($input, $output);
$this->importMapManager->update();
$updatedPackages = $this->importMapManager->update($packages);

$io->success('Updated all packages in importmap.php.');
if (0 < \count($packages)) {
$io->success(sprintf(
'Updated %s package%s in importmap.php.',
implode(', ', array_map(static fn (ImportMapEntry $entry): string => $entry->importName, $updatedPackages)),
1 < \count($updatedPackages) ? 's' : '',
));
} else {
$io->success('Updated all packages in importmap.php.');
}

return Command::SUCCESS;
}
Expand Down
16 changes: 9 additions & 7 deletions src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function getImportMapJson(): string
*/
public function require(array $packages): array
{
return $this->updateImportMapConfig(false, $packages, []);
return $this->updateImportMapConfig(false, $packages, [], []);
}

/**
Expand All @@ -98,15 +98,17 @@ public function require(array $packages): array
*/
public function remove(array $packages): void
{
$this->updateImportMapConfig(false, [], $packages);
$this->updateImportMapConfig(false, [], $packages, []);
}

/**
* Updates all existing packages to the latest version.
* Updates either all existing packages or the specified ones to the latest version.
*
* @return ImportMapEntry[]
*/
public function update(): array
public function update(array $packages = []): array
{
return $this->updateImportMapConfig(true, [], []);
return $this->updateImportMapConfig(true, [], [], $packages);
}

/**
Expand Down Expand Up @@ -190,7 +192,7 @@ private function buildImportMapJson(): void
*
* @return ImportMapEntry[]
*/
private function updateImportMapConfig(bool $update, array $packagesToRequire, array $packagesToRemove): array
private function updateImportMapConfig(bool $update, array $packagesToRequire, array $packagesToRemove, array $packagesToUpdate): array
{
$currentEntries = $this->loadImportMapEntries();

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

if ($update) {
foreach ($currentEntries as $importName => $entry) {
if (null === $entry->url) {
if (null === $entry->url || (0 !== \count($packagesToUpdate) && !\in_array($importName, $packagesToUpdate, true))) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,64 @@ public function testUpdate()
$this->assertSame('contents of cowsay.js', $actualContents);
}

public function testUpdateWithSpecificPackages()
{
$rootDir = __DIR__.'/../fixtures/importmaps_for_writing';
$manager = $this->createImportMapManager(['assets' => ''], $rootDir);

$map = [
'lodash' => [
'url' => 'https://ga.jspm.io/npm:lodash@1.2.3/lodash.js',
],
'cowsay' => [
'url' => 'https://ga.jspm.io/npm:cowsay@4.5.6/cowsay.umd.js',
'downloaded_to' => 'vendor/cowsay.js',
],
'bootstrap' => [
'url' => 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.esm.js',
'preload' => true,
],
'app' => [
'path' => 'app.js',
],
];
$mapString = var_export($map, true);
file_put_contents($rootDir.'/importmap.php', "<?php\n\nreturn {$mapString};\n");
$this->filesystem->mkdir($rootDir.'/assets/vendor');
file_put_contents($rootDir.'/assets/vendor/cowsay.js', 'cowsay.js original contents');
file_put_contents($rootDir.'/assets/app.js', 'app.js contents');

$this->packageResolver->expects($this->once())
->method('resolvePackages')
->willReturn([
self::resolvedPackage('cowsay', 'https://ga.jspm.io/npm:cowsay@4.5.9/cowsay.umd.js', download: true, content: 'updated contents of cowsay.js'),
])
;

$manager->update(['cowsay']);
$actualImportMap = require $rootDir.'/importmap.php';
$expectedImportMap = [
'lodash' => [
'url' => 'https://ga.jspm.io/npm:lodash@1.2.3/lodash.js',
],
'cowsay' => [
'url' => 'https://ga.jspm.io/npm:cowsay@4.5.9/cowsay.umd.js',
'downloaded_to' => 'vendor/cowsay.js',
],
'bootstrap' => [
'url' => 'https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.esm.js',
'preload' => true,
],
'app' => [
'path' => 'app.js',
],
];
$this->assertEquals($expectedImportMap, $actualImportMap);
$this->assertFileExists($rootDir.'/assets/vendor/cowsay.js');
$actualContents = file_get_contents($rootDir.'/assets/vendor/cowsay.js');
$this->assertSame('updated contents of cowsay.js', $actualContents);
}

public function testDownloadMissingPackages()
{
$rootDir = __DIR__.'/../fixtures/download';
Expand Down
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