Skip to content

Commit

Permalink
Merge pull request #49 from tonysm/tm/update-cmd
Browse files Browse the repository at this point in the history
New `importmap:update` command and store CDN URL with package name and version in vendor comment
  • Loading branch information
tonysm authored Feb 4, 2024
2 parents e0988fe + b700aae commit 0cfdcb1
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/Commands/AuditCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Tonysm\ImportmapLaravel\Npm;
use Tonysm\ImportmapLaravel\VulnerablePackage;

#[AsCommand('importmap:audit')]
class AuditCommand extends Command
{
public $signature = 'importmap:audit';
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/ClearCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Symfony\Component\Console\Attribute\AsCommand;
use Tonysm\ImportmapLaravel\Importmap;
use Tonysm\ImportmapLaravel\Manifest;

#[AsCommand('importmap:clear')]
class ClearCacheCommand extends Command
{
public $signature = 'importmap:clear';
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Terminal;
use Tonysm\ImportmapLaravel\Actions\FixJsImportPaths;
use Tonysm\ImportmapLaravel\Events\FailedToFixImportStatement;

#[AsCommand('importmap:install')]
class InstallCommand extends Command
{
public $signature = 'importmap:install';
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/JsonCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Tonysm\ImportmapLaravel\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Tonysm\ImportmapLaravel\AssetResolver;
use Tonysm\ImportmapLaravel\Importmap;

#[AsCommand('importmap:json')]
class JsonCommand extends Command
{
public $signature = 'importmap:json';
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/OptimizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Tonysm\ImportmapLaravel\FileDigest;
use Tonysm\ImportmapLaravel\Importmap;
use Tonysm\ImportmapLaravel\Manifest;

#[AsCommand('importmap:optimize')]
class OptimizeCommand extends Command
{
public $signature = 'importmap:optimize';
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/OutdatedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Tonysm\ImportmapLaravel\Npm;
use Tonysm\ImportmapLaravel\OutdatedPackage;

#[AsCommand('importmap:outdated')]
class OutdatedCommand extends Command
{
public $signature = 'importmap:outdated';
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/PackagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Tonysm\ImportmapLaravel\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Tonysm\ImportmapLaravel\Npm;
use Tonysm\ImportmapLaravel\PackageVersion;

#[AsCommand('importmap:packages')]
class PackagesCommand extends Command
{
public $signature = 'importmap:packages';
Expand Down
4 changes: 3 additions & 1 deletion src/Commands/PinCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Symfony\Component\Console\Attribute\AsCommand;
use Tonysm\ImportmapLaravel\Packager;

#[AsCommand('importmap:pin')]
class PinCommand extends Command
{
/**
Expand Down Expand Up @@ -82,7 +84,7 @@ private function importPackages(Packager $packager, Collection $imports): void
private function pattern(string $package): string
{
return sprintf(
'#^Importmap::pin\("%s".*$#',
'#Importmap::pin\([\'\"]%s[\'\"].*$#',
preg_quote($package),
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/UnpinCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Symfony\Component\Console\Attribute\AsCommand;
use Tonysm\ImportmapLaravel\Packager;

#[AsCommand('importmap:unpin')]
class UnpinCommand extends Command
{
/**
Expand Down
36 changes: 36 additions & 0 deletions src/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Tonysm\ImportmapLaravel\Commands;

use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;
use Tonysm\ImportmapLaravel\Npm;

#[AsCommand('importmap:update')]
class UpdateCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'importmap:update';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update outdated pinned packages.';

public function handle(Npm $npm)
{
if (count($outdatedPackages = $npm->outdatedPackages()) > 0) {
$this->call('importmap:pin', [
'packages' => $outdatedPackages->pluck('name')->all(),
]);
} else {
$this->components->info('No oudated packages found.');
}
}
}
3 changes: 2 additions & 1 deletion src/ImportmapLaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function configurePackage(Package $package): void
->hasCommand(Commands\UnpinCommand::class)
->hasCommand(Commands\OutdatedCommand::class)
->hasCommand(Commands\AuditCommand::class)
->hasCommand(Commands\PackagesCommand::class);
->hasCommand(Commands\PackagesCommand::class)
->hasCommand(Commands\UpdateCommand::class);
}

public function packageRegistered()
Expand Down
2 changes: 1 addition & 1 deletion src/Npm.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function findPackagesFromCdnMatches(string $content)

private function findPackagesFromLocalMatches(string $content)
{
preg_match_all('/^Importmap::pin\(["\']([^"\']*)["\'].*\)\; \/\/.*@(\d+\.\d+\.\d+(?:[^\s]*)).*\r?$/m', $content, $matches);
preg_match_all('/^Importmap::pin\(.+\)\;\s*\/\/\s*(.+?)@(.+?)\s+.*\r?$/m', $content, $matches);

if (count($matches) !== 3) {
return collect();
Expand Down
14 changes: 12 additions & 2 deletions src/Packager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function import(array $packages, string $env, string $from)
'install' => $packages,
'flattenScope' => true,
'env' => ['browser', 'module', $env],
'provider' => $from,
'provider' => $this->normalizeProvider($from),
]);

return match ($response->status()) {
Expand All @@ -55,10 +55,12 @@ public function vendoredPinFor(string $package, string $url): string
$version = $this->extractPackageVersionFrom($url);

return sprintf(
'Importmap::pin("%s", to: "%s"); // %s',
'Importmap::pin("%s", to: "%s"); // %s%s downloaded from %s',
$package,
Str::after($this->vendoredPackageName($package), 'resources'),
$package,
$version,
$url,
);
}

Expand Down Expand Up @@ -138,4 +140,12 @@ private function handleFailureResponse(Response $response)

throw Exceptions\ImportmapException::withUnexpectedResponseCode($response->status());
}

private function normalizeProvider(string $provider): string
{
return match ($provider) {
'jspm' => 'jspm.io',
default => $provider,
};
}
}
2 changes: 1 addition & 1 deletion tests/fixtures/npm/audit-importmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
use Tonysm\ImportmapLaravel\Facades\Importmap;

Importmap::pin('is-svg', to: 'https://cdn.skypack.dev/[email protected]', preload: true);
Importmap::pin('lodash', to: '/js/vendor/lodash.js'); // @4.17.12
Importmap::pin('lodash', to: '/js/vendor/lodash.js'); // lodash@4.17.12
2 changes: 1 addition & 1 deletion tests/fixtures/npm/outdated-importmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
use Tonysm\ImportmapLaravel\Facades\Importmap;

Importmap::pin('is-svg', to: 'https://cdn.skypack.dev/[email protected]', preload: true);
Importmap::pin('lodash', to: '/js/vendor/lodash.js'); // @4.0.0
Importmap::pin('lodash', to: '/js/vendor/lodash.js'); // lodash@4.0.0

0 comments on commit 0cfdcb1

Please sign in to comment.