From 27b7455d895019f43db9979d06347413ebaf9419 Mon Sep 17 00:00:00 2001 From: Eser DENIZ Date: Thu, 19 Sep 2024 19:59:34 +0200 Subject: [PATCH] Added NPM package-lock.json support --- composer.json | 3 +- composer.lock | 555 +++------------------------------------------- src/whatsdiff.php | 139 ++++++++++-- 3 files changed, 145 insertions(+), 552 deletions(-) diff --git a/composer.json b/composer.json index 576cb13..85c3511 100644 --- a/composer.json +++ b/composer.json @@ -25,8 +25,7 @@ "pestphp/pest": "^2.24", "laravel/pint": "^1.13", "humbug/box": "^4.3", - "nunomaduro/collision": "^7.0|^8.0", - "spatie/ray": "^1.41" + "nunomaduro/collision": "^7.0|^8.0" }, "config": { "allow-plugins": { diff --git a/composer.lock b/composer.lock index 7456f90..1661264 100644 --- a/composer.lock +++ b/composer.lock @@ -4,28 +4,28 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "2a1e8c6b9f1a93e83c9488f19bd32e38", + "content-hash": "4611e275d6220bba494385c5ddccfe71", "packages": [ { "name": "composer/semver", - "version": "3.4.2", + "version": "3.4.3", "source": { "type": "git", "url": "https://github.com/composer/semver.git", - "reference": "c51258e759afdb17f1fd1fe83bc12baaef6309d6" + "reference": "4313d26ada5e0c4edfbd1dc481a92ff7bff91f12" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/composer/semver/zipball/c51258e759afdb17f1fd1fe83bc12baaef6309d6", - "reference": "c51258e759afdb17f1fd1fe83bc12baaef6309d6", + "url": "https://api.github.com/repos/composer/semver/zipball/4313d26ada5e0c4edfbd1dc481a92ff7bff91f12", + "reference": "4313d26ada5e0c4edfbd1dc481a92ff7bff91f12", "shasum": "" }, "require": { "php": "^5.3.2 || ^7.0 || ^8.0" }, "require-dev": { - "phpstan/phpstan": "^1.4", - "symfony/phpunit-bridge": "^4.2 || ^5" + "phpstan/phpstan": "^1.11", + "symfony/phpunit-bridge": "^3 || ^7" }, "type": "library", "extra": { @@ -69,7 +69,7 @@ "support": { "irc": "ircs://irc.libera.chat:6697/composer", "issues": "https://github.com/composer/semver/issues", - "source": "https://github.com/composer/semver/tree/3.4.2" + "source": "https://github.com/composer/semver/tree/3.4.3" }, "funding": [ { @@ -85,7 +85,7 @@ "type": "tidelift" } ], - "time": "2024-07-12T11:35:52+00:00" + "time": "2024-09-19T14:15:21+00:00" }, { "name": "guzzlehttp/guzzle", @@ -414,16 +414,16 @@ }, { "name": "illuminate/collections", - "version": "v11.23.4", + "version": "v11.23.5", "source": { "type": "git", "url": "https://github.com/illuminate/collections.git", - "reference": "9845d4b4894f10cedd6635d3d06caead59151358" + "reference": "cbea9d7a82984bbc1a9376498533cc77513f9a09" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/collections/zipball/9845d4b4894f10cedd6635d3d06caead59151358", - "reference": "9845d4b4894f10cedd6635d3d06caead59151358", + "url": "https://api.github.com/repos/illuminate/collections/zipball/cbea9d7a82984bbc1a9376498533cc77513f9a09", + "reference": "cbea9d7a82984bbc1a9376498533cc77513f9a09", "shasum": "" }, "require": { @@ -465,11 +465,11 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2024-09-11T20:37:13+00:00" + "time": "2024-09-12T14:50:04+00:00" }, { "name": "illuminate/conditionable", - "version": "v11.23.4", + "version": "v11.23.5", "source": { "type": "git", "url": "https://github.com/illuminate/conditionable.git", @@ -515,16 +515,16 @@ }, { "name": "illuminate/contracts", - "version": "v11.23.4", + "version": "v11.23.5", "source": { "type": "git", "url": "https://github.com/illuminate/contracts.git", - "reference": "272aad6ca259425d50b266218be5a7955a4c8af3" + "reference": "5a4c6dcf633c1f69e1b70bbea1ef1b7d2186d3da" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/contracts/zipball/272aad6ca259425d50b266218be5a7955a4c8af3", - "reference": "272aad6ca259425d50b266218be5a7955a4c8af3", + "url": "https://api.github.com/repos/illuminate/contracts/zipball/5a4c6dcf633c1f69e1b70bbea1ef1b7d2186d3da", + "reference": "5a4c6dcf633c1f69e1b70bbea1ef1b7d2186d3da", "shasum": "" }, "require": { @@ -559,11 +559,11 @@ "issues": "https://github.com/laravel/framework/issues", "source": "https://github.com/laravel/framework" }, - "time": "2024-09-11T20:06:38+00:00" + "time": "2024-09-12T15:25:08+00:00" }, { "name": "illuminate/macroable", - "version": "v11.23.4", + "version": "v11.23.5", "source": { "type": "git", "url": "https://github.com/illuminate/macroable.git", @@ -2657,66 +2657,6 @@ ], "time": "2024-02-20T07:24:02+00:00" }, - { - "name": "brick/math", - "version": "0.12.1", - "source": { - "type": "git", - "url": "https://github.com/brick/math.git", - "reference": "f510c0a40911935b77b86859eb5223d58d660df1" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/brick/math/zipball/f510c0a40911935b77b86859eb5223d58d660df1", - "reference": "f510c0a40911935b77b86859eb5223d58d660df1", - "shasum": "" - }, - "require": { - "php": "^8.1" - }, - "require-dev": { - "php-coveralls/php-coveralls": "^2.2", - "phpunit/phpunit": "^10.1", - "vimeo/psalm": "5.16.0" - }, - "type": "library", - "autoload": { - "psr-4": { - "Brick\\Math\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "Arbitrary-precision arithmetic library", - "keywords": [ - "Arbitrary-precision", - "BigInteger", - "BigRational", - "arithmetic", - "bigdecimal", - "bignum", - "bignumber", - "brick", - "decimal", - "integer", - "math", - "mathematics", - "rational" - ], - "support": { - "issues": "https://github.com/brick/math/issues", - "source": "https://github.com/brick/math/tree/0.12.1" - }, - "funding": [ - { - "url": "https://github.com/BenMorel", - "type": "github" - } - ], - "time": "2023-11-29T23:19:16+00:00" - }, { "name": "composer/pcre", "version": "3.3.1", @@ -4015,16 +3955,16 @@ }, { "name": "nikic/php-parser", - "version": "v5.1.0", + "version": "v5.2.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "683130c2ff8c2739f4822ff7ac5c873ec529abd1" + "reference": "23c79fbbfb725fb92af9bcf41065c8e9a0d49ddb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/683130c2ff8c2739f4822ff7ac5c873ec529abd1", - "reference": "683130c2ff8c2739f4822ff7ac5c873ec529abd1", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/23c79fbbfb725fb92af9bcf41065c8e9a0d49ddb", + "reference": "23c79fbbfb725fb92af9bcf41065c8e9a0d49ddb", "shasum": "" }, "require": { @@ -4067,9 +4007,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v5.1.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v5.2.0" }, - "time": "2024-07-01T20:03:41+00:00" + "time": "2024-09-15T16:40:33+00:00" }, { "name": "nunomaduro/collision", @@ -5367,187 +5307,6 @@ }, "time": "2024-09-11T13:17:53+00:00" }, - { - "name": "ramsey/collection", - "version": "2.0.0", - "source": { - "type": "git", - "url": "https://github.com/ramsey/collection.git", - "reference": "a4b48764bfbb8f3a6a4d1aeb1a35bb5e9ecac4a5" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/ramsey/collection/zipball/a4b48764bfbb8f3a6a4d1aeb1a35bb5e9ecac4a5", - "reference": "a4b48764bfbb8f3a6a4d1aeb1a35bb5e9ecac4a5", - "shasum": "" - }, - "require": { - "php": "^8.1" - }, - "require-dev": { - "captainhook/plugin-composer": "^5.3", - "ergebnis/composer-normalize": "^2.28.3", - "fakerphp/faker": "^1.21", - "hamcrest/hamcrest-php": "^2.0", - "jangregor/phpstan-prophecy": "^1.0", - "mockery/mockery": "^1.5", - "php-parallel-lint/php-console-highlighter": "^1.0", - "php-parallel-lint/php-parallel-lint": "^1.3", - "phpcsstandards/phpcsutils": "^1.0.0-rc1", - "phpspec/prophecy-phpunit": "^2.0", - "phpstan/extension-installer": "^1.2", - "phpstan/phpstan": "^1.9", - "phpstan/phpstan-mockery": "^1.1", - "phpstan/phpstan-phpunit": "^1.3", - "phpunit/phpunit": "^9.5", - "psalm/plugin-mockery": "^1.1", - "psalm/plugin-phpunit": "^0.18.4", - "ramsey/coding-standard": "^2.0.3", - "ramsey/conventional-commits": "^1.3", - "vimeo/psalm": "^5.4" - }, - "type": "library", - "extra": { - "captainhook": { - "force-install": true - }, - "ramsey/conventional-commits": { - "configFile": "conventional-commits.json" - } - }, - "autoload": { - "psr-4": { - "Ramsey\\Collection\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Ben Ramsey", - "email": "ben@benramsey.com", - "homepage": "https://benramsey.com" - } - ], - "description": "A PHP library for representing and manipulating collections.", - "keywords": [ - "array", - "collection", - "hash", - "map", - "queue", - "set" - ], - "support": { - "issues": "https://github.com/ramsey/collection/issues", - "source": "https://github.com/ramsey/collection/tree/2.0.0" - }, - "funding": [ - { - "url": "https://github.com/ramsey", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/ramsey/collection", - "type": "tidelift" - } - ], - "time": "2022-12-31T21:50:55+00:00" - }, - { - "name": "ramsey/uuid", - "version": "4.7.6", - "source": { - "type": "git", - "url": "https://github.com/ramsey/uuid.git", - "reference": "91039bc1faa45ba123c4328958e620d382ec7088" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/ramsey/uuid/zipball/91039bc1faa45ba123c4328958e620d382ec7088", - "reference": "91039bc1faa45ba123c4328958e620d382ec7088", - "shasum": "" - }, - "require": { - "brick/math": "^0.8.8 || ^0.9 || ^0.10 || ^0.11 || ^0.12", - "ext-json": "*", - "php": "^8.0", - "ramsey/collection": "^1.2 || ^2.0" - }, - "replace": { - "rhumsaa/uuid": "self.version" - }, - "require-dev": { - "captainhook/captainhook": "^5.10", - "captainhook/plugin-composer": "^5.3", - "dealerdirect/phpcodesniffer-composer-installer": "^0.7.0", - "doctrine/annotations": "^1.8", - "ergebnis/composer-normalize": "^2.15", - "mockery/mockery": "^1.3", - "paragonie/random-lib": "^2", - "php-mock/php-mock": "^2.2", - "php-mock/php-mock-mockery": "^1.3", - "php-parallel-lint/php-parallel-lint": "^1.1", - "phpbench/phpbench": "^1.0", - "phpstan/extension-installer": "^1.1", - "phpstan/phpstan": "^1.8", - "phpstan/phpstan-mockery": "^1.1", - "phpstan/phpstan-phpunit": "^1.1", - "phpunit/phpunit": "^8.5 || ^9", - "ramsey/composer-repl": "^1.4", - "slevomat/coding-standard": "^8.4", - "squizlabs/php_codesniffer": "^3.5", - "vimeo/psalm": "^4.9" - }, - "suggest": { - "ext-bcmath": "Enables faster math with arbitrary-precision integers using BCMath.", - "ext-gmp": "Enables faster math with arbitrary-precision integers using GMP.", - "ext-uuid": "Enables the use of PeclUuidTimeGenerator and PeclUuidRandomGenerator.", - "paragonie/random-lib": "Provides RandomLib for use with the RandomLibAdapter", - "ramsey/uuid-doctrine": "Allows the use of Ramsey\\Uuid\\Uuid as Doctrine field type." - }, - "type": "library", - "extra": { - "captainhook": { - "force-install": true - } - }, - "autoload": { - "files": [ - "src/functions.php" - ], - "psr-4": { - "Ramsey\\Uuid\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "A PHP library for generating and working with universally unique identifiers (UUIDs).", - "keywords": [ - "guid", - "identifier", - "uuid" - ], - "support": { - "issues": "https://github.com/ramsey/uuid/issues", - "source": "https://github.com/ramsey/uuid/tree/4.7.6" - }, - "funding": [ - { - "url": "https://github.com/ramsey", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/ramsey/uuid", - "type": "tidelift" - } - ], - "time": "2024-04-27T21:32:50+00:00" - }, { "name": "revolt/event-loop", "version": "v1.0.6", @@ -6648,204 +6407,6 @@ }, "time": "2022-08-31T10:31:18+00:00" }, - { - "name": "spatie/backtrace", - "version": "1.6.2", - "source": { - "type": "git", - "url": "https://github.com/spatie/backtrace.git", - "reference": "1a9a145b044677ae3424693f7b06479fc8c137a9" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/spatie/backtrace/zipball/1a9a145b044677ae3424693f7b06479fc8c137a9", - "reference": "1a9a145b044677ae3424693f7b06479fc8c137a9", - "shasum": "" - }, - "require": { - "php": "^7.3|^8.0" - }, - "require-dev": { - "ext-json": "*", - "laravel/serializable-closure": "^1.3", - "phpunit/phpunit": "^9.3", - "spatie/phpunit-snapshot-assertions": "^4.2", - "symfony/var-dumper": "^5.1" - }, - "type": "library", - "autoload": { - "psr-4": { - "Spatie\\Backtrace\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Freek Van de Herten", - "email": "freek@spatie.be", - "homepage": "https://spatie.be", - "role": "Developer" - } - ], - "description": "A better backtrace", - "homepage": "https://github.com/spatie/backtrace", - "keywords": [ - "Backtrace", - "spatie" - ], - "support": { - "source": "https://github.com/spatie/backtrace/tree/1.6.2" - }, - "funding": [ - { - "url": "https://github.com/sponsors/spatie", - "type": "github" - }, - { - "url": "https://spatie.be/open-source/support-us", - "type": "other" - } - ], - "time": "2024-07-22T08:21:24+00:00" - }, - { - "name": "spatie/macroable", - "version": "2.0.0", - "source": { - "type": "git", - "url": "https://github.com/spatie/macroable.git", - "reference": "ec2c320f932e730607aff8052c44183cf3ecb072" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/spatie/macroable/zipball/ec2c320f932e730607aff8052c44183cf3ecb072", - "reference": "ec2c320f932e730607aff8052c44183cf3ecb072", - "shasum": "" - }, - "require": { - "php": "^8.0" - }, - "require-dev": { - "phpunit/phpunit": "^8.0|^9.3" - }, - "type": "library", - "autoload": { - "psr-4": { - "Spatie\\Macroable\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Freek Van der Herten", - "email": "freek@spatie.be", - "homepage": "https://spatie.be", - "role": "Developer" - } - ], - "description": "A trait to dynamically add methods to a class", - "homepage": "https://github.com/spatie/macroable", - "keywords": [ - "macroable", - "spatie" - ], - "support": { - "issues": "https://github.com/spatie/macroable/issues", - "source": "https://github.com/spatie/macroable/tree/2.0.0" - }, - "time": "2021-03-26T22:39:02+00:00" - }, - { - "name": "spatie/ray", - "version": "1.41.2", - "source": { - "type": "git", - "url": "https://github.com/spatie/ray.git", - "reference": "c44f8cfbf82c69909b505de61d8d3f2d324e93fc" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/spatie/ray/zipball/c44f8cfbf82c69909b505de61d8d3f2d324e93fc", - "reference": "c44f8cfbf82c69909b505de61d8d3f2d324e93fc", - "shasum": "" - }, - "require": { - "ext-curl": "*", - "ext-json": "*", - "php": "^7.3|^8.0", - "ramsey/uuid": "^3.0|^4.1", - "spatie/backtrace": "^1.1", - "spatie/macroable": "^1.0|^2.0", - "symfony/stopwatch": "^4.0|^5.1|^6.0|^7.0", - "symfony/var-dumper": "^4.2|^5.1|^6.0|^7.0.3" - }, - "require-dev": { - "illuminate/support": "6.x|^8.18|^9.0", - "nesbot/carbon": "^2.63", - "pestphp/pest": "^1.22", - "phpstan/phpstan": "^1.10", - "phpunit/phpunit": "^9.5", - "rector/rector": "^0.19.2", - "spatie/phpunit-snapshot-assertions": "^4.2", - "spatie/test-time": "^1.2" - }, - "bin": [ - "bin/remove-ray.sh" - ], - "type": "library", - "extra": { - "branch-alias": { - "dev-main": "1.x-dev" - } - }, - "autoload": { - "files": [ - "src/helpers.php" - ], - "psr-4": { - "Spatie\\Ray\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Freek Van der Herten", - "email": "freek@spatie.be", - "homepage": "https://spatie.be", - "role": "Developer" - } - ], - "description": "Debug with Ray to fix problems faster", - "homepage": "https://github.com/spatie/ray", - "keywords": [ - "ray", - "spatie" - ], - "support": { - "issues": "https://github.com/spatie/ray/issues", - "source": "https://github.com/spatie/ray/tree/1.41.2" - }, - "funding": [ - { - "url": "https://github.com/sponsors/spatie", - "type": "github" - }, - { - "url": "https://spatie.be/open-source/support-us", - "type": "other" - } - ], - "time": "2024-04-24T14:21:46+00:00" - }, { "name": "symfony/event-dispatcher-contracts", "version": "v3.5.0", @@ -7193,68 +6754,6 @@ ], "time": "2024-07-26T12:44:47+00:00" }, - { - "name": "symfony/stopwatch", - "version": "v7.1.1", - "source": { - "type": "git", - "url": "https://github.com/symfony/stopwatch.git", - "reference": "5b75bb1ac2ba1b9d05c47fc4b3046a625377d23d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/stopwatch/zipball/5b75bb1ac2ba1b9d05c47fc4b3046a625377d23d", - "reference": "5b75bb1ac2ba1b9d05c47fc4b3046a625377d23d", - "shasum": "" - }, - "require": { - "php": ">=8.2", - "symfony/service-contracts": "^2.5|^3" - }, - "type": "library", - "autoload": { - "psr-4": { - "Symfony\\Component\\Stopwatch\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Provides a way to profile code", - "homepage": "https://symfony.com", - "support": { - "source": "https://github.com/symfony/stopwatch/tree/v7.1.1" - }, - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2024-05-31T14:57:53+00:00" - }, { "name": "symfony/var-dumper", "version": "v7.1.4", diff --git a/src/whatsdiff.php b/src/whatsdiff.php index c419f39..898bfe1 100644 --- a/src/whatsdiff.php +++ b/src/whatsdiff.php @@ -62,7 +62,11 @@ function showHelp(): void function gitLogOfFile(string $filename): array { - $output = shell_exec("git log --pretty=format:'%h' $filename"); + $output = shell_exec("git log --pretty=format:'%h' -- '$filename'"); // TODO: escape filename + + if (is_null($output)) { + return []; + } return explode("\n", $output); } @@ -83,7 +87,13 @@ function isFileHasBeenRecentlyUpdated(string $filename): bool return [$line[1] => $line[0]]; }); - return $status->get($filename) == 'M'; + return in_array($status->get($filename), [ + // todo: read the docs + 'AM', // Added and modified + 'M', // Modified + 'A', // Added + '??' // Untracked + ]); } function getFileContentOfCommit(string $filename, string $commitHash): string @@ -97,35 +107,41 @@ function getCommitHashToCompare(array $commitLogs, bool $recentlyUpdated): array $previousHashKey = $recentlyUpdated ? 0 : 1; - if (! isset($commitLogs[$previousHashKey])) { - throw new Exception('Could not found any previous changes'); - } - - $previous = $commitLogs[$previousHashKey]; + $previous = $commitLogs[$previousHashKey] ?? null; return [$last, $previous]; } -function getFilesToCompare(string $filename, ?string $lastHash, string $previousHash): array +function getFilesToCompare(string $filename, ?string $lastHash, ?string $previousHash): array { $last = $lastHash ? getFileContentOfCommit($filename, $lastHash) : file_get_contents($filename); - $previous = getFileContentOfCommit($filename, $previousHash); + $previous = $previousHash ? getFileContentOfCommit($filename, $previousHash) : null; return [$last, $previous]; } function extractComposerPackagesVersions($composerLockContent): array { - return collect($composerLockContent['packages']) + return collect($composerLockContent['packages'] ?? []) ->merge($composerLockContent['packages-dev']) ->mapWithKeys(fn ($package) => [$package['name'] => $package['version']]) ->toArray(); } +function extractNpmjsPackagesVersions($composerLockContent): array +{ + return collect($composerLockContent['packages'] ?? []) + ->mapWithKeys(fn ($package, $key) => [ + str_replace('node_modules/', '', $key) => $package['version'] + ]) + ->filter(fn ($version, $name) => ! empty($name)) + ->toArray(); +} + function diffComposerLockPackages($last, $previous) { $last = json_decode($last, associative: true); - $previous = json_decode($previous, associative: true); + $previous = json_decode($previous ?? '{}', associative: true); $last = extractComposerPackagesVersions($last); $previous = extractComposerPackagesVersions($previous); @@ -154,6 +170,38 @@ function diffComposerLockPackages($last, $previous) ->toArray(); } +function diffPackageLockPackages($last, $previous) +{ + $last = json_decode($last, associative: true); + $previous = json_decode($previous ?? '{}', associative: true); + + $last = extractNpmjsPackagesVersions($last); + $previous = extractNpmjsPackagesVersions($previous); + + $diff = collect($previous) + ->mapWithKeys(fn ($version, $name) => [ + $name => [ + 'from' => $version, + 'to' => $last[$name] ?? null, + ] + ]); + + $newPackages = collect($last) + ->diffKeys($previous) + ->mapWithKeys(fn ($version, $name) => [ + $name => [ + 'from' => null, + 'to' => $version, + ] + ]); + + + return $diff->merge($newPackages) + ->filter(fn ($el) => $el['from'] !== $el['to']) + ->sortKeys() + ->toArray(); +} + function printDiff(array $diff): void { if (! count($diff)) { @@ -161,25 +209,32 @@ function printDiff(array $diff): void return; } + $maxStrLen = max(array_map('strlen', array_keys($diff))); - $maxStrLenVersion = max(array_map(fn ($el) => strlen($el['from']), $diff)); + $maxStrLenVersion = max(array_map( + fn ($el) => strlen($el['from']), + array_filter($diff, fn ($el) => $el['from'] !== null) + ) ?: [0]); foreach ($diff as $package => $infos) { if ($infos['from'] !== null && $infos['to'] !== null) { if (Comparator::greaterThan($infos['to'], $infos['from'])) { $releases = getNewReleases($package, $infos['from'], $infos['to']); $nbReleases = count($releases); - echo '[U] '.str_pad($package, $maxStrLen).' : '.str_pad($infos['from'], $maxStrLenVersion).' => '.$infos['to'].($nbReleases > 1 ? " ($nbReleases releases)" : "").PHP_EOL; + echo "\033[36m↑\033[0m ".str_pad($package, $maxStrLen).' : '.str_pad( + $infos['from'], + $maxStrLenVersion + ).' => '.$infos['to'].($nbReleases > 1 ? " ($nbReleases releases)" : "").PHP_EOL; } else { - echo '[D] '.str_pad($package, $maxStrLen).' : '.str_pad( + echo "\033[33m↓\033[0m ".str_pad($package, $maxStrLen).' : '.str_pad( $infos['from'], $maxStrLenVersion ).' => '.$infos['to'].PHP_EOL; } } elseif ($infos['from'] === null) { - echo '[+] '.str_pad($package, $maxStrLen).' : '.$infos['to'].PHP_EOL; + echo "\033[32m+\033[0m ".str_pad($package, $maxStrLen).' : '.$infos['to'].PHP_EOL; } elseif ($infos['to'] === null) { - echo '[-] '.str_pad($package, $maxStrLen).' : '.$infos['from'].PHP_EOL; + echo "\033[31m×\033[0m ".str_pad($package, $maxStrLen).' : '.$infos['from'].PHP_EOL; } } } @@ -228,17 +283,57 @@ function getNewReleases(string $package, string $from, string $to): array $recentlyUpdated = ! $options['ignore_last'] && isFileHasBeenRecentlyUpdated($filename); -if ($recentlyUpdated) { - echo 'Uncommited changes detected on '.$filename.PHP_EOL.PHP_EOL; +if (! $recentlyUpdated && empty($commitLogs)) { + echo 'No commit logs found for '.$filename.PHP_EOL; +} else { + + if ($recentlyUpdated) { + echo 'Uncommited changes detected on '.$filename.PHP_EOL; + } + + [$lastHash, $previousHash] = getCommitHashToCompare($commitLogs, $recentlyUpdated); + + if ($previousHash === null) { + echo "No previous commit found, $filename has just been created".PHP_EOL; + } else { + echo $filename.' changed between '.$previousHash.' and '.($lastHash ?? 'uncommited changes').PHP_EOL; + } + + echo PHP_EOL; + + [$last, $previous] = getFilesToCompare($filename, $lastHash, $previousHash); + + printDiff(diffComposerLockPackages($last, $previous)); } -[$lastHash, $previousHash] = getCommitHashToCompare($commitLogs, $recentlyUpdated); -echo 'Changes between '.$previousHash.' and '.($lastHash ?? 'uncommited changes').PHP_EOL; +echo PHP_EOL.'----------'.PHP_EOL.PHP_EOL; +$filename = 'package-lock.json'; + +$commitLogs = gitLogOfFile($filename); -[$last, $previous] = getFilesToCompare($filename, $lastHash, $previousHash); +$recentlyUpdated = ! $options['ignore_last'] && isFileHasBeenRecentlyUpdated($filename); -printDiff(diffComposerLockPackages($last, $previous)); +if (! $recentlyUpdated && empty($commitLogs)) { + echo 'No commit logs found for '.$filename.PHP_EOL; +} else { + if ($recentlyUpdated) { + echo 'Uncommited changes detected on '.$filename.PHP_EOL; + } + + [$lastHash, $previousHash] = getCommitHashToCompare($commitLogs, $recentlyUpdated); + + if ($previousHash === null) { + echo "No previous commit found, $filename has just been created".PHP_EOL; + } else { + echo $filename.' changed between '.$previousHash.' and '.($lastHash ?? 'uncommited changes').PHP_EOL; + } + + [$last, $previous] = getFilesToCompare($filename, $lastHash, $previousHash); + + echo PHP_EOL; + printDiff(diffPackageLockPackages($last, $previous)); +} // getNewReleases('laravel/framework', 'v11.19.0', 'v11.22.0'); // getNewReleases('srwiez/svgtinyps-cli', 'v1.0', 'v1.3');