Skip to content

Commit 20486fa

Browse files
imliampushpak1300
andauthored
Dynamic NPM Package Runner (#145)
* Add a helper to decide whether to use npm, pnpm, yarn or bun when running scripts * Calculate the node package manager upfront and memoise it * Fix tests * Update to use Roster's nodePackageManager function * Run linter * Fix test * test: remove redundant GuidelineAssistTest and add lockfile-based package manager guideline tests Signed-off-by: Pushpak Chhajed <[email protected]> * use nullsafe operator Signed-off-by: Pushpak Chhajed <[email protected]> * Fix phpstan error Signed-off-by: Pushpak Chhajed <[email protected]> --------- Signed-off-by: Pushpak Chhajed <[email protected]> Co-authored-by: Pushpak Chhajed <[email protected]> Co-authored-by: Pushpak Chhajed <[email protected]>
1 parent 707c749 commit 20486fa

File tree

6 files changed

+35
-3
lines changed

6 files changed

+35
-3
lines changed

.ai/foundation.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
- Do not change the application's dependencies without approval.
2828

2929
## Frontend Bundling
30-
- 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.
30+
- 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.
3131

3232
## Replies
3333
- Be concise in your explanations - focus on what's important rather than explaining obvious details.

.ai/laravel/core.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,4 @@
3939
- 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.
4040

4141
### Vite Error
42-
- 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`.
42+
- 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`.

src/BoostServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function register(): void
3636
$lockFiles = [
3737
base_path('composer.lock'),
3838
base_path('package-lock.json'),
39+
base_path('bun.lock'),
3940
base_path('bun.lockb'),
4041
base_path('pnpm-lock.yaml'),
4142
base_path('yarn.lock'),

src/Install/GuidelineAssist.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Database\Eloquent\Model;
88
use Laravel\Boost\Install\Assists\Inertia;
9+
use Laravel\Roster\Enums\NodePackageManager;
910
use Laravel\Roster\Enums\Packages;
1011
use Laravel\Roster\Roster;
1112
use ReflectionClass;
@@ -168,4 +169,9 @@ public function inertia(): Inertia
168169
{
169170
return new Inertia($this->roster);
170171
}
172+
173+
public function nodePackageManager(): string
174+
{
175+
return ($this->roster->nodePackageManager() ?? NodePackageManager::NPM)->value;
176+
}
171177
}

src/Install/GuidelineComposer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ protected function guideline(string $path, bool $thirdParty = false): array
311311

312312
protected function processBoostSnippets(string $content): string
313313
{
314-
return preg_replace_callback('/(?<!@)@boostsnippet\(\s*(?P<nameQuote>[\'"])(?P<name>[^\1]*?)\1(?:\s*,\s*(?P<langQuote>[\'"])(?P<lang>[^\3]*?)\3)?\s*\)(?P<content>.*?)@endboostsnippet/s', function ($matches): string {
314+
return preg_replace_callback('/(?<!@)@boostsnippet\(\s*(?P<nameQuote>[\'"])(?P<name>[^\1]*?)\1(?:\s*,\s*(?P<langQuote>[\'"])(?P<lang>[^\3]*?)\3)?\s*\)(?P<content>.*?)@endboostsnippet/s', function (array $matches): string {
315315
$name = $matches['name'];
316316
$lang = empty($matches['lang']) ? 'html' : $matches['lang'];
317317
$snippetContent = $matches['content'];

tests/Feature/Install/GuidelineComposerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
use Laravel\Boost\Install\GuidelineComposer;
66
use Laravel\Boost\Install\GuidelineConfig;
77
use Laravel\Boost\Install\Herd;
8+
use Laravel\Roster\Enums\NodePackageManager;
89
use Laravel\Roster\Enums\Packages;
910
use Laravel\Roster\Package;
1011
use Laravel\Roster\PackageCollection;
1112
use Laravel\Roster\Roster;
1213

1314
beforeEach(function (): void {
1415
$this->roster = Mockery::mock(Roster::class);
16+
$this->nodePackageManager = NodePackageManager::NPM;
17+
$this->roster->shouldReceive('nodePackageManager')->andReturnUsing(
18+
fn (): NodePackageManager => $this->nodePackageManager
19+
);
20+
1521
$this->herd = Mockery::mock(Herd::class);
1622
$this->herd->shouldReceive('isInstalled')->andReturn(false)->byDefault();
1723

@@ -357,3 +363,22 @@
357363
->toContain('=== phpunit/core rules ===')
358364
->not->toContain('=== pest/core rules ===');
359365
});
366+
367+
test('includes correct package manager commands in guidelines based on lockfile', function (NodePackageManager $packageManager, string $expectedCommand): void {
368+
$packages = new PackageCollection([
369+
new Package(Packages::LARAVEL, 'laravel/framework', '11.0.0'),
370+
]);
371+
$this->nodePackageManager = $packageManager;
372+
$this->roster->shouldReceive('packages')->andReturn($packages);
373+
374+
$guidelines = $this->composer->compose();
375+
376+
expect($guidelines)
377+
->toContain("{$expectedCommand} run build")
378+
->toContain("{$expectedCommand} run dev");
379+
})->with([
380+
'npm' => [NodePackageManager::NPM, 'npm'],
381+
'pnpm' => [NodePackageManager::PNPM, 'pnpm'],
382+
'yarn' => [NodePackageManager::YARN, 'yarn'],
383+
'bun' => [NodePackageManager::BUN, 'bun'],
384+
]);

0 commit comments

Comments
 (0)