Skip to content

Commit

Permalink
Merge pull request #97 from siketyan/build/yarn-lock-berry
Browse files Browse the repository at this point in the history
build(deps): Support Yarn Berry
  • Loading branch information
siketyan authored May 27, 2023
2 parents 82d617e + 81b7fa6 commit 4fe25f0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
31 changes: 21 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 4fe25f0

Please sign in to comment.