Skip to content

Commit

Permalink
Merge pull request #113 from innocenzi/feat/improve-vite-solution
Browse files Browse the repository at this point in the history
Improve Vite solution provider
  • Loading branch information
freekmurze authored Oct 14, 2022
2 parents c7ba427 + 61b80c5 commit f2336fc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
use Illuminate\Support\Str;
use Spatie\Ignition\Contracts\BaseSolution;
use Spatie\Ignition\Contracts\HasSolutionsForThrowable;
use Spatie\Ignition\Contracts\Solution;
use Throwable;

class MissingViteManifestSolutionProvider implements HasSolutionsForThrowable
{
protected array $links = [
'Asset bundling with Vite' => 'https://laravel.com/docs/9.x/vite#running-vite',
];

public function canSolve(Throwable $throwable): bool
{
return Str::startsWith($throwable->getMessage(), 'Vite manifest not found');
Expand All @@ -17,8 +22,34 @@ public function canSolve(Throwable $throwable): bool
public function getSolutions(Throwable $throwable): array
{
return [
BaseSolution::create('Missing Vite Manifest File')
->setSolutionDescription('Did you forget to run `npm install && npm run dev`?'),
$this->getSolution(),
];
}

public function getSolution(): Solution
{
/** @var string */
$baseCommand = collect([
'pnpm-lock.yaml' => 'pnpm',
'yarn.lock' => 'yarn',
])->first(fn ($_, $lockfile) => file_exists(base_path($lockfile)), 'npm run');

return app()->environment('local')
? $this->getLocalSolution($baseCommand)
: $this->getProductionSolution($baseCommand);
}

protected function getLocalSolution(string $baseCommand): Solution
{
return BaseSolution::create('Start the development server')
->setSolutionDescription("Run `{$baseCommand} dev` in your terminal and refresh the page.")
->setDocumentationLinks($this->links);
}

protected function getProductionSolution(string $baseCommand): Solution
{
return BaseSolution::create('Build the production assets')
->setSolutionDescription("Run `{$baseCommand} build` in your deployment script.")
->setDocumentationLinks($this->links);
}
}
36 changes: 34 additions & 2 deletions tests/Solutions/ViteManifestNotFoundSolutionProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,42 @@
expect($canSolve)->toBeTrue();
});

it('can recommend running npm install and npm run dev', function () {
it('recommends running `npm run dev` in a local environment', function () {
app()->detectEnvironment(fn () => 'local');

/** @var \Spatie\Ignition\Contracts\Solution $solution */
$solution = app(MissingViteManifestSolutionProvider::class)
->getSolutions(new Exception('Vite manifest not found at: public/build/manifest.json'))[0];


expect(Str::contains($solution->getSolutionDescription(), 'Run `npm run dev` in your terminal and refresh the page.'))->toBeTrue();
});

it('recommends running `npm run build` in a production environment', function () {
app()->detectEnvironment(fn () => 'production');

/** @var \Spatie\Ignition\Contracts\Solution $solution */
$solution = app(MissingViteManifestSolutionProvider::class)
->getSolutions(new Exception('Vite manifest not found at: public/build/manifest.json'))[0];

expect(Str::contains($solution->getSolutionDescription(), 'Did you forget to run `npm install && npm run dev`?'))->toBeTrue();

expect(Str::contains($solution->getSolutionDescription(), 'Run `npm run build` in your deployment script.'))->toBeTrue();
});

it('detects the package manager and adapts the recommended command', function (string $lockfile, string $command) {
app()->detectEnvironment(fn () => 'local');

file_put_contents(base_path($lockfile), '');

/** @var \Spatie\Ignition\Contracts\Solution $solution */
$solution = app(MissingViteManifestSolutionProvider::class)
->getSolutions(new Exception('Vite manifest not found at: public/build/manifest.json'))[0];

expect(Str::contains($solution->getSolutionDescription(), "Run `{$command}` in your terminal and refresh the page."))->toBeTrue();

unlink(base_path($lockfile));
})->with([
['pnpm-lock.yaml', 'pnpm dev'],
['yarn.lock', 'yarn dev'],
['package-lock.json', 'npm run dev']
]);

0 comments on commit f2336fc

Please sign in to comment.