diff --git a/composer.json b/composer.json index 200845f..d763d70 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "ext-json": "*", "guzzlehttp/guzzle": "^7.5", "mschop/pathogen": "^0.7.1", - "siketyan/yarn-lock": "^0.1.1", + "siketyan/yarn-lock": "^0.2.0", "symfony/config": "^6.2", "symfony/console": "^6.2", "symfony/dependency-injection": "^6.2", diff --git a/composer.lock b/composer.lock index 054cf70..2f4a27d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bb6e32cb3ddf24ea1709e84fe0517b57", + "content-hash": "eb11eea2ff9ec7a2a34de9566453dca6", "packages": [ { "name": "guzzlehttp/guzzle", @@ -704,20 +704,21 @@ }, { "name": "siketyan/yarn-lock", - "version": "v0.1.1", + "version": "v0.2.0", "source": { "type": "git", "url": "https://github.com/siketyan/php-yarn-lock.git", - "reference": "2d4b01d5b1bd332484906a608d238a62e4e3172a" + "reference": "f992b0bc83a382e4a0db1d256ec5f5947449901b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/siketyan/php-yarn-lock/zipball/2d4b01d5b1bd332484906a608d238a62e4e3172a", - "reference": "2d4b01d5b1bd332484906a608d238a62e4e3172a", + "url": "https://api.github.com/repos/siketyan/php-yarn-lock/zipball/f992b0bc83a382e4a0db1d256ec5f5947449901b", + "reference": "f992b0bc83a382e4a0db1d256ec5f5947449901b", "shasum": "" }, "require": { - "php": "^7.4 || ^8.0" + "php": "^7.4|^8.0", + "symfony/yaml": "^5|^6" }, "require-dev": { "friendsofphp/php-cs-fixer": "^2.16", @@ -735,16 +736,26 @@ ], "authors": [ { - "name": "Naoki Ikeguchi", - "email": "root@siketyan.dev" + "name": "Natsuki Ikeguchi", + "email": "me@s6n.jp" } ], "description": "A Parser Library for yarn.lock in PHP.", "support": { "issues": "https://github.com/siketyan/php-yarn-lock/issues", - "source": "https://github.com/siketyan/php-yarn-lock/tree/v0.1.1" + "source": "https://github.com/siketyan/php-yarn-lock/tree/v0.2.0" }, - "time": "2020-12-02T14:24:17+00:00" + "funding": [ + { + "url": "https://github.com/siketyan", + "type": "github" + }, + { + "url": "https://www.patreon.com/siketyan", + "type": "patreon" + } + ], + "time": "2023-05-27T07:37:24+00:00" }, { "name": "symfony/config", diff --git a/src/Scanner/Yarn/YarnLockParser.php b/src/Scanner/Yarn/YarnLockParser.php index a77a68e..5871c95 100644 --- a/src/Scanner/Yarn/YarnLockParser.php +++ b/src/Scanner/Yarn/YarnLockParser.php @@ -8,6 +8,7 @@ use Siketyan\Loxcan\Model\DependencyCollection; use Siketyan\Loxcan\Model\Package; use Siketyan\Loxcan\Versioning\SemVer\SemVerVersionParser; +use Siketyan\YarnLock\ConstraintInterface; use Siketyan\YarnLock\YarnLock; class YarnLockParser @@ -20,14 +21,15 @@ public function __construct( public function parse(?string $lock): DependencyCollection { - $packages = YarnLock::parse($lock ?? ''); + $packages = YarnLock::parsePackages(YarnLock::parse($lock ?? '')); $dependencies = []; - foreach ($packages as $names => $package) { - $version = $package['version']; + foreach ($packages as $package) { + $version = $package->getVersion(); - foreach (explode(',', $names) as $name) { - $name = substr($name, 0, strrpos($name, '@', -1)); + /** @var ConstraintInterface $constraint */ + foreach ($package->getConstraints()->all() as $constraint) { + $name = $constraint->getName(); $pkg = $this->packagePool->get($name); if (!$pkg instanceof Package) { diff --git a/tests/Scanner/Yarn/YarnLockParserTest.php b/tests/Scanner/Yarn/YarnLockParserTest.php index d716320..76c7ffc 100644 --- a/tests/Scanner/Yarn/YarnLockParserTest.php +++ b/tests/Scanner/Yarn/YarnLockParserTest.php @@ -18,11 +18,19 @@ class YarnLockParserTest extends TestCase use ProphecyTrait; private const CONTENTS = <<<'EOS' - @foo/bar@^1.2: - version "1.2.3-dev" + # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. + # yarn lockfile v1 - baz@*: - version "3.2.1" + + "@types/node@^18": + version "18.16.16" + resolved "https://registry.yarnpkg.com/@types/node/-/node-18.16.16.tgz#3b64862856c7874ccf7439e6bab872d245c86d8e" + integrity sha512-NpaM49IGQQAUlBhHMF82QH80J08os4ZmyF9MkpCzWAGuOHqE4gTEbhzd7L3l5LmWuZ6E0OiC1FweQ4tsiW35+g== + + typescript@^5: + version "5.0.4" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.0.4.tgz#b217fd20119bd61a94d4011274e0ab369058da3b" + integrity sha512-cW9T5W9xY37cc+jfEnaUvX91foxtHkza3Nw3wkoF4sSlKn0MONdkdEndig/qPBWXNkmplh3NzayQzCiHM4/hqw== EOS; private ObjectProphecy $packagePool; @@ -46,12 +54,12 @@ public function test(): void $fooBarVersion = $this->prophesize(SemVerVersion::class)->reveal(); $barBazVersion = $this->prophesize(SemVerVersion::class)->reveal(); - $this->packagePool->get('@foo/bar')->willReturn(null); - $this->packagePool->get('baz')->willReturn($cache); + $this->packagePool->get('@types/node')->willReturn(null); + $this->packagePool->get('typescript')->willReturn($cache); $this->packagePool->add(Argument::type(Package::class))->shouldBeCalledOnce(); - $this->versionParser->parse('1.2.3-dev')->willReturn($fooBarVersion); - $this->versionParser->parse('3.2.1')->willReturn($barBazVersion); + $this->versionParser->parse('18.16.16')->willReturn($fooBarVersion); + $this->versionParser->parse('5.0.4')->willReturn($barBazVersion); $collection = $this->parser->parse(self::CONTENTS); $dependencies = $collection->getDependencies(); @@ -59,7 +67,7 @@ public function test(): void $this->assertCount(2, $dependencies); $this->assertContainsOnlyInstancesOf(Dependency::class, $dependencies); - $this->assertSame('@foo/bar', $dependencies[0]->getPackage()->getName()); + $this->assertSame('@types/node', $dependencies[0]->getPackage()->getName()); $this->assertSame($fooBarVersion, $dependencies[0]->getVersion()); $this->assertSame($cache, $dependencies[1]->getPackage());