From bbe2e0e5ea51ae8802d6bb1b10f39ac88b484ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Mon, 21 Jan 2019 13:36:25 +0100 Subject: [PATCH 1/4] Ensure `Package::$resolves` is set. --- src/Package.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Package.php b/src/Package.php index dd28001..6cc1062 100644 --- a/src/Package.php +++ b/src/Package.php @@ -59,7 +59,7 @@ class Package * * @var Package[] */ - protected $resolves; + protected $resolves = []; /** * Depth in the dependency tree. Only initialized once the YarnLock computes @@ -208,9 +208,6 @@ public function getResolves() */ public function addResolves(Package $package) { - if ($this->resolves === null) { - $this->resolves = []; - } if (in_array($package, $this->resolves)) { return; } From aa463f01aba06f9c3cf68bdf9f0d7ba3e7623e0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Mon, 21 Jan 2019 13:36:58 +0100 Subject: [PATCH 2/4] Ensure depth is calculated when using `getPackagesByDepth()`. --- src/YarnLock.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/YarnLock.php b/src/YarnLock.php index 25ca7e5..4b8c060 100644 --- a/src/YarnLock.php +++ b/src/YarnLock.php @@ -133,6 +133,7 @@ public function getPackagesByName($packageName) */ public function getPackagesByDepth($start, $end = 0) { + $this->calculateDepth(); if ($end === 0 || ($end !== null && $end < $start)) { $end = $start + 1; } From 5558b05c0081c506deeea6483b87e895f388e09a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Mon, 21 Jan 2019 13:44:20 +0100 Subject: [PATCH 3/4] Fix the namespace in the readme. --- readme.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 74e9874..df40c23 100644 --- a/readme.md +++ b/readme.md @@ -6,9 +6,10 @@ A php-package for parsing and evaluating the [yarn.lock](https://yarnpkg.com/lang/en/docs/yarn-lock/) format. ## Basic Usage + ```php getDependencies(); ``` ## Package Depth + If you maybe don't just want all packages but only the direct dependencies plus one level of indirection, you have to go a little extra mile: + ```php dependencies; // get these packages from the yarn lock-file @@ -35,4 +39,4 @@ $yarnLock->calculateDepth($rootDependencies); // get the first two levels; the second argument is the exclusive upper limit $yarnLock->getPackagesByDepth(0, 2); -``` \ No newline at end of file +``` From 3c77397dd51012ea12d397130c76eecbbfb8b578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Kochen?= Date: Mon, 21 Jan 2019 13:46:31 +0100 Subject: [PATCH 4/4] Fix the depth query example in the readme. --- readme.md | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/readme.md b/readme.md index df40c23..6572e90 100644 --- a/readme.md +++ b/readme.md @@ -21,22 +21,11 @@ $babelCoreDependencies = $babelCorePackages[0]->getDependencies(); ## Package Depth -If you maybe don't just want all packages but only the direct dependencies plus one level of indirection, you have to go a little extra mile: +You can also query packages by depth: ```php -dependencies; -// get these packages from the yarn lock-file -$rootDependencies = array_map(function($packageName, $packageVersion) use ($yarnLock) { - return $yarnLock->getPackage($packageName, $packageVersion); -}, array_keys($packageDependencies), array_values($packageDependencies)); -// some of our dependencies might be used by other dependencies deeper down the tree so -// they wouldn't appear in the top levels, if we wouldn't explicitly set them there. -$yarnLock->calculateDepth($rootDependencies); - -// get the first two levels; the second argument is the exclusive upper limit -$yarnLock->getPackagesByDepth(0, 2); +// Get just the direct dependencies. +$directDependencies = $yarnLock->getPackagesByDepth(0); +// Get the first two levels of dependencies. +$firstTwoLevels = $yarnLock->getPackagesByDepth(0, 2); ```