Skip to content
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ $roster->uses(Packages::INERTIA);

// Check if a particular version of a package is in use
$roster->usesVersion(Packages::INERTIA, '2.0.0', '>=');

// Detect which JavaScript package manager is in use
$packageManager = $roster->nodePackageManager();
```

## Contributing
Expand Down
26 changes: 26 additions & 0 deletions src/Enums/NodePackageManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Laravel\Roster\Enums;

use Laravel\Roster\Scanners\BunPackageLock;
use Laravel\Roster\Scanners\NpmPackageLock;
use Laravel\Roster\Scanners\PnpmPackageLock;
use Laravel\Roster\Scanners\YarnPackageLock;

enum NodePackageManager: string
{
case NPM = 'npm';
case PNPM = 'pnpm';
case YARN = 'yarn';
case BUN = 'bun';

public function scanner(string $path): NpmPackageLock|PnpmPackageLock|YarnPackageLock|BunPackageLock
{
return match ($this) {
self::NPM => new NpmPackageLock($path),
self::PNPM => new PnpmPackageLock($path),
self::YARN => new YarnPackageLock($path),
self::BUN => new BunPackageLock($path),
};
}
}
16 changes: 14 additions & 2 deletions src/Roster.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Collection;
use Laravel\Roster\Enums\Approaches;
use Laravel\Roster\Enums\NodePackageManager;
use Laravel\Roster\Enums\Packages;
use Laravel\Roster\Scanners\Composer;
use Laravel\Roster\Scanners\DirectoryStructure;
Expand All @@ -24,6 +25,8 @@ class Roster

protected PackageCollection $packages;

protected ?NodePackageManager $nodePackageManager = null;

public function __construct()
{
$this->approaches = collect();
Expand Down Expand Up @@ -116,6 +119,11 @@ public function approach(Approaches $approach): ?Approach
return $this->approaches->first(fn (Approach $item) => $item->approach()->value === $approach->value);
}

public function nodePackageManager(): ?NodePackageManager
{
return $this->nodePackageManager;
}

public function json(): string
{
return json_encode([
Expand All @@ -126,6 +134,7 @@ public function json(): string
'name' => $package->name(),
'version' => $package->version(),
])->toArray(),
'nodePackageManager' => $this->nodePackageManager?->value,
], JSON_PRETTY_PRINT) ?: '{}';
}

Expand All @@ -138,14 +147,17 @@ public static function scan(?string $basePath = null): self
->scan()
->each(fn ($item) => $roster->add($item));

(new PackageLock($basePath))
->scan()
$packageLock = new PackageLock($basePath);

$packageLock->scan()
->each(fn ($item) => $roster->add($item));

(new DirectoryStructure($basePath))
->scan()
->each(fn ($item) => $roster->add($item));

$roster->nodePackageManager = $packageLock->detect();

return $roster;
}
}
24 changes: 15 additions & 9 deletions src/Scanners/PackageLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Roster\Scanners;

use Illuminate\Support\Collection;
use Laravel\Roster\Enums\NodePackageManager;

class PackageLock
{
Expand All @@ -16,20 +17,25 @@ public function __construct(protected string $path) {}
*/
public function scan(): Collection
{
// Priority order: npm -> pnpm -> yarn -> bun
$scanners = [
new NpmPackageLock($this->path),
new PnpmPackageLock($this->path),
new YarnPackageLock($this->path),
new BunPackageLock($this->path),
];

foreach ($scanners as $scanner) {
foreach (NodePackageManager::cases() as $case) {
$scanner = $case->scanner($this->path);
if ($scanner->canScan()) {
return $scanner->scan();
}
}

return collect();
}

public function detect(): NodePackageManager
{
foreach (NodePackageManager::cases() as $case) {
$scanner = $case->scanner($this->path);
if ($scanner->canScan()) {
return $case;
}
}

return NodePackageManager::NPM;
}
}
46 changes: 46 additions & 0 deletions tests/Unit/RosterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Laravel\Roster\Approach;
use Laravel\Roster\Enums\Approaches;
use Laravel\Roster\Enums\NodePackageManager;
use Laravel\Roster\Enums\Packages;
use Laravel\Roster\Package;
use Laravel\Roster\Roster;
Expand Down Expand Up @@ -86,3 +87,48 @@
expect($package->rawName())->toBe('pestphp/pest');
expect($package->name())->toBe('PEST');
});

describe('node package manager detection', function () {
beforeEach(function () {
$this->path = __DIR__.'/../fixtures/fog/';
$this->lockFiles = [
'package-lock.json' => $this->path.'package-lock.json',
'pnpm-lock.yaml' => $this->path.'pnpm-lock.yaml',
'yarn.lock' => $this->path.'yarn.lock',
'bun.lock' => $this->path.'bun.lock',
];
});

afterEach(function () {
foreach ($this->lockFiles as $file) {
$tempPath = $file.'.bac';
if (file_exists($tempPath)) {
rename($tempPath, $file);
}
}
});

it('can detect :manager as node package manager', function (string $requiredFile, NodePackageManager $expected) {
foreach ($this->lockFiles as $fileName => $filePath) {
if ($fileName !== $requiredFile && file_exists($filePath)) {
rename($filePath, $filePath.'.bac');
}
}

$roster = Roster::scan($this->path);

expect($roster->nodePackageManager())->toBe($expected);
})->with([
'npm' => ['package-lock.json', NodePackageManager::NPM],
'pnpm' => ['pnpm-lock.yaml', NodePackageManager::PNPM],
'yarn' => ['yarn.lock', NodePackageManager::YARN],
'bun' => ['bun.lock', NodePackageManager::BUN],
]);

it('defaults to npm when no lock files exist', function () {
$path = __DIR__.'/../fixtures/phpunit/';
$roster = Roster::scan($path);

expect($roster->nodePackageManager())->toBe(NodePackageManager::NPM);
});
});