Skip to content

Commit

Permalink
feat: Use package parsing provided in siketyan/yarn-lock
Browse files Browse the repository at this point in the history
  • Loading branch information
siketyan committed May 27, 2023
1 parent 9a02718 commit 81b7fa6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
12 changes: 7 additions & 5 deletions src/Scanner/Yarn/YarnLockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
26 changes: 17 additions & 9 deletions tests/Scanner/Yarn/YarnLockParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,20 +54,20 @@ 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();

$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());
Expand Down

0 comments on commit 81b7fa6

Please sign in to comment.