mindscreen/yarnlock

一个解析和解释 yarn.lock 文件的包

v1.1.0 2024-02-26 19:35 UTC

This package is auto-updated.

Last update: 2024-08-29 19:22:30 UTC


README

CircleCI codecov

一个用于解析和评估 yarn.lock 格式的 php 包。

基本用法

<?php
use Mindscreen\YarnLock;

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

$allPackages = $yarnLock->getPackages();
$hasBabelCore = $yarnLock->hasPackage('babel-core', '^6.0.0');
$babelCorePackages = $yarnLock->getPackagesByName('babel-core');
$babelCoreDependencies = $babelCorePackages[0]->getDependencies();

包深度

如果您不只想获取所有包,只想获取直接依赖项加上一层间接依赖,您需要做更多的额外工作。

<?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);