/
ImportMapRequireCommand.php
169 lines (134 loc) · 6.44 KB
/
ImportMapRequireCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\AssetMapper\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\AssetMapper\AssetMapperInterface;
use Symfony\Component\AssetMapper\ImportMap\ImportMapEntry;
use Symfony\Component\AssetMapper\ImportMap\ImportMapManager;
use Symfony\Component\AssetMapper\ImportMap\PackageRequireOptions;
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\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* @experimental
*
* @author Kévin Dunglas <kevin@dunglas.dev>
*/
#[AsCommand(name: 'importmap:require', description: 'Requires JavaScript packages')]
final class ImportMapRequireCommand extends Command
{
public function __construct(
private readonly ImportMapManager $importMapManager,
private readonly AssetMapperInterface $assetMapper,
private readonly string $projectDir,
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('packages', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'The packages to add')
->addOption('download', 'd', InputOption::VALUE_NONE, 'Download packages locally')
->addOption('preload', 'p', InputOption::VALUE_NONE, 'Preload packages')
->addOption('path', null, InputOption::VALUE_REQUIRED, 'The local path where the package lives relative to the project root')
->setHelp(<<<'EOT'
The <info>%command.name%</info> command adds packages to <comment>importmap.php</comment> usually
by finding a CDN URL for the given package and version.
For example:
<info>php %command.full_name% lodash --preload</info>
<info>php %command.full_name% "lodash@^4.15"</info>
You can also require specific paths of a package:
<info>php %command.full_name% "chart.js/auto"</info>
Or download one package/file, but alias its name in your import map:
<info>php %command.full_name% "vue/dist/vue.esm-bundler.js=vue"</info>
The <info>preload</info> option will set the <info>preload</info> option in the importmap,
which will tell the browser to preload the package. This should be used for all
critical packages that are needed on page load.
The <info>download</info> option will download the package locally and point the
importmap to it. Use this if you want to avoid using a CDN or if you want to
ensure that the package is available even if the CDN is down.
Sometimes, a package may require other packages and multiple new items may be added
to the import map.
You can also require multiple packages at once:
<info>php %command.full_name% "lodash@^4.15" "@hotwired/stimulus"</info>
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$packageList = $input->getArgument('packages');
$path = null;
if ($input->getOption('path')) {
if (\count($packageList) > 1) {
$io->error('The "--path" option can only be used when you require a single package.');
return Command::FAILURE;
}
$path = $input->getOption('path');
if (!is_file($path)) {
$path = $this->projectDir.'/'.$path;
if (!is_file($path)) {
$io->error(sprintf('The path "%s" does not exist.', $input->getOption('path')));
return Command::FAILURE;
}
}
}
$packages = [];
foreach ($packageList as $packageName) {
$parts = ImportMapManager::parsePackageName($packageName);
if (null === $parts) {
$io->error(sprintf('Package "%s" is not a valid package name format. Use the format PACKAGE@VERSION - e.g. "lodash" or "lodash@^4"', $packageName));
return Command::FAILURE;
}
$packages[] = new PackageRequireOptions(
$parts['package'],
$parts['version'] ?? null,
$input->getOption('download'),
$input->getOption('preload'),
$parts['alias'] ?? $parts['package'],
isset($parts['registry']) && $parts['registry'] ? $parts['registry'] : null,
$path,
);
}
if ($input->getOption('download')) {
$io->warning(sprintf('The --download option is experimental. It should work well with the default %s provider but check your browser console for 404 errors.', ImportMapManager::PROVIDER_JSDELIVR_ESM));
}
$newPackages = $this->importMapManager->require($packages);
if (1 === \count($newPackages)) {
$newPackage = $newPackages[0];
$message = sprintf('Package "%s" added to importmap.php', $newPackage->importName);
if ($newPackage->isDownloaded && null !== $downloadedAsset = $this->assetMapper->getAsset($newPackage->path)) {
$application = $this->getApplication();
if ($application instanceof Application) {
$projectDir = $application->getKernel()->getProjectDir();
$downloadedPath = $downloadedAsset->sourcePath;
if (str_starts_with($downloadedPath, $projectDir)) {
$downloadedPath = substr($downloadedPath, \strlen($projectDir) + 1);
}
$message .= sprintf(' and downloaded locally to "%s"', $downloadedPath);
}
}
$message .= '.';
} else {
$names = array_map(fn (ImportMapEntry $package) => $package->importName, $newPackages);
$message = sprintf('%d new packages (%s) added to the importmap.php!', \count($newPackages), implode(', ', $names));
}
$messages = [$message];
if (1 === \count($newPackages)) {
$messages[] = sprintf('Use the new package normally by importing "%s".', $newPackages[0]->importName);
}
$io->success($messages);
return Command::SUCCESS;
}
}