Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<?php
use Mindscreen\YarnLock;
use Mindscreen\YarnLock\YarnLock;

$yarnLock = YarnLock::fromString(file_get_contents('yarn.lock'));

Expand All @@ -19,20 +20,12 @@ $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
<?php
use Mindscreen\YarnLock;
// read the dependencies from the package.json file
$packageDependencies = (json_decode(file_get_contents('package.json')))->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);
```
5 changes: 1 addition & 4 deletions src/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Package
*
* @var Package[]
*/
protected $resolves;
protected $resolves = [];

/**
* Depth in the dependency tree. Only initialized once the YarnLock computes
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/YarnLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down