Skip to content

Dynamic NPM Package Runner #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion .ai/foundation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
- Do not change the application's dependencies without approval.

## Frontend Bundling
- If the user doesn't see a frontend change reflected in the UI, it could mean they need to run `npm run build`, `npm run dev`, or `composer run dev`. Ask them.
- If the user doesn't see a frontend change reflected in the UI, it could mean they need to run `{{ $assist->nodePackageManager() }} run build`, `{{ $assist->nodePackageManager() }} run dev`, or `composer run dev`. Ask them.

## Replies
- Be concise in your explanations - focus on what's important rather than explaining obvious details.
Expand Down
2 changes: 1 addition & 1 deletion .ai/laravel/core.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
- When creating tests, make use of `php artisan make:test [options] <name>` to create a feature test, and pass `--unit` to create a unit test. Most tests should be feature tests.

### Vite Error
- If you receive an "Illuminate\Foundation\ViteException: Unable to locate file in Vite manifest" error, you can run `npm run build` or ask the user to run `npm run dev` or `composer run dev`.
- If you receive an "Illuminate\Foundation\ViteException: Unable to locate file in Vite manifest" error, you can run `{{ $assist->nodePackageManager() }} run build` or ask the user to run `{{ $assist->nodePackageManager() }} run dev` or `composer run dev`.
14 changes: 14 additions & 0 deletions src/Install/GuidelineAssist.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,20 @@ class GuidelineAssist

protected static array $classes = [];

protected string $nodePackageManager = 'npm';

public function __construct()
{
$this->modelPaths = $this->discover(fn ($reflection) => ($reflection->isSubclassOf(Model::class) && ! $reflection->isAbstract()));
$this->controllerPaths = $this->discover(fn (ReflectionClass $reflection) => (stripos($reflection->getName(), 'controller') !== false || stripos($reflection->getNamespaceName(), 'controller') !== false));
$this->enumPaths = $this->discover(fn ($reflection) => $reflection->isEnum());
$this->nodePackageManager = match (true) {
file_exists(base_path('package-lock.json')) => 'npm',
file_exists(base_path('pnpm-lock.yaml')) => 'pnpm',
file_exists(base_path('yarn.lock')) => 'yarn',
file_exists(base_path('bun.lockb')) => 'bun',
default => 'npm',
};
}

/**
Expand Down Expand Up @@ -156,4 +165,9 @@ public function enumContents(): string

return file_get_contents(current($this->enumPaths));
}

public function nodePackageManager(): string
{
return $this->nodePackageManager;
}
}
68 changes: 68 additions & 0 deletions tests/Feature/Install/GuidelineAssistTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

use Laravel\Boost\Install\GuidelineAssist;

beforeEach(function () {
$lockFiles = [
base_path('package-lock.json'),
base_path('pnpm-lock.yaml'),
base_path('yarn.lock'),
base_path('bun.lockb'),
];

foreach ($lockFiles as $file) {
if (file_exists($file)) {
unlink($file);
}
}
});

afterEach(function () {
$lockFiles = [
base_path('package-lock.json'),
base_path('pnpm-lock.yaml'),
base_path('yarn.lock'),
base_path('bun.lockb'),
];

foreach ($lockFiles as $file) {
if (file_exists($file)) {
unlink($file);
}
}
});

test('it detects npm when package-lock.json exists', function () {
file_put_contents(base_path('package-lock.json'), '{}');

$assist = new GuidelineAssist();
expect($assist->nodePackageManager())->toBe('npm');
});

test('it detects pnpm when pnpm-lock.yaml exists', function () {
file_put_contents(base_path('pnpm-lock.yaml'), 'lockfileVersion: 6.0');

$assist = new GuidelineAssist();
expect($assist->nodePackageManager())->toBe('pnpm');
});

test('it detects yarn when yarn.lock exists', function () {
file_put_contents(base_path('yarn.lock'), '# This is yarn lock file');

$assist = new GuidelineAssist();
expect($assist->nodePackageManager())->toBe('yarn');
});

test('it detects bun when bun.lockb exists', function () {
file_put_contents(base_path('bun.lockb'), 'bun binary lock file');

$assist = new GuidelineAssist();
expect($assist->nodePackageManager())->toBe('bun');
});

test('it defaults to npm when no lock files exist', function () {
$assist = new GuidelineAssist();
expect($assist->nodePackageManager())->toBe('npm');
});
Loading