Skip to content

Commit

Permalink
Supports NpmJs Releases
Browse files Browse the repository at this point in the history
  • Loading branch information
SRWieZ committed Sep 20, 2024
1 parent 1458568 commit c5d330b
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions src/whatsdiff.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,12 @@ function printDiff(array $diff, $type = 'composer'): void
foreach ($diff as $package => $infos) {
if ($infos['from'] !== null && $infos['to'] !== null) {
if (Comparator::greaterThan($infos['to'], $infos['from'])) {
$releases = $type === 'composer' ? getNewReleases($package, $infos['from'], $infos['to']) : [];
$releases = match ($type) {
'composer' => getComposerReleases($package, $infos['from'], $infos['to']),
'npmjs' => getNpmjsReleases($package, $infos['from'], $infos['to']),
default => [],
};

$nbReleases = count($releases);

echo "\033[36m↑\033[0m ".str_pad($package, $maxStrLen).' : '.str_pad(
Expand All @@ -239,7 +244,7 @@ function printDiff(array $diff, $type = 'composer'): void
}
}

function getNewReleases(string $package, string $from, string $to): array
function getComposerReleases(string $package, string $from, string $to): array
{
$packageInfos = file_get_contents('https://repo.packagist.org/p2/'.$package.'.json');
$packageInfos = json_decode($packageInfos, associative: true);
Expand Down Expand Up @@ -271,6 +276,39 @@ function getNewReleases(string $package, string $from, string $to): array
return $returnVersions;
}


function getNpmjsReleases(string $package, string $from, string $to): array
{
$packageInfos = file_get_contents('https://registry.npmjs.org/'.urlencode($package));
$packageInfos = json_decode($packageInfos, associative: true);

$versions = $packageInfos['versions'];

$returnVersions = [];

$foundTo = false;
$foundFrom = false;
foreach ($versions as $infos) {
if ($infos['version'] === $from) {
$foundFrom = true;
}

if ($infos['version'] === $to) {
$foundTo = true;
}

if (Comparator::greaterThan($infos['version'], $from) && Comparator::lessThan($infos['version'], $to)) {
$returnVersions[] = $infos['version'];
}

if ($foundFrom && $foundTo) {
break;
}
}

return $returnVersions;
}

if ($command === 'help' || $options['show_version']) {
showHelp();
exit;
Expand Down Expand Up @@ -308,6 +346,8 @@ function getNewReleases(string $package, string $from, string $to): array


echo PHP_EOL.'----------'.PHP_EOL.PHP_EOL;


$filename = 'package-lock.json';

$commitLogs = gitLogOfFile($filename);
Expand Down Expand Up @@ -335,7 +375,8 @@ function getNewReleases(string $package, string $from, string $to): array
printDiff(diffPackageLockPackages($last, $previous), type: 'npmjs');
}

// getNewReleases('laravel/framework', 'v11.19.0', 'v11.22.0');
// getNewReleases('srwiez/svgtinyps-cli', 'v1.0', 'v1.3');
// getComposerReleases('laravel/framework', 'v11.19.0', 'v11.22.0');
// getComposerReleases('srwiez/svgtinyps-cli', 'v1.0', 'v1.3');
// dump(getNpmjsReleases('alpinejs', '3.10.5', '3.14.1'));

exit(0);

0 comments on commit c5d330b

Please sign in to comment.