Skip to content
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

Installed NPM package(s) check #30

Merged
merged 9 commits into from
Sep 26, 2023
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ class AppServiceProvider extends ServiceProvider
}
```

You can now run the checks using the `ok:check` Artisan command:

```bash
php artisan ok:check
```

## Available checks

✅ **Cache Check**: Check if reading and writing to the cache is possible.
Expand All @@ -102,6 +108,8 @@ class AppServiceProvider extends ServiceProvider

✅ **NPM Audit Check**: Checks if there are any security vulnerabilities in your npm dependencies.

✅ **NPM Installed Package Check**: Checks if a certain npm package is installed.

✅ **Queue Check**: Checks if the queue is running.

✅ **Route Cache Check**: Checks if routes are cached.
Expand Down
61 changes: 61 additions & 0 deletions src/Checks/NpmPackageInstalledCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Vormkracht10\LaravelOK\Checks;

use Symfony\Component\Process\Process;
use Vormkracht10\LaravelOK\Checks\Base\Check;
use Vormkracht10\LaravelOK\Checks\Base\Result;

class NpmPackageInstalledCheck extends Check
{
protected array $shouldHave = [];

protected array $with = [];

public function shouldHave(array $packages): self
{
$this->shouldHave = $packages;

return $this;
}

public function with(array $data): self
{
$this->with = $data;

return $this;
}

public function run(): Result
{
$result = Result::new();

$installedPackages = $this->data();

$missingPackages = array_diff($this->shouldHave, array_keys($installedPackages));

if (count($missingPackages) === 0) {
return $result->ok();
}

$missingPackages = implode(', ', $missingPackages);

return $result->failed("The following packages are missing: {$missingPackages}");
}

protected function data()
{
if (count($this->with) > 0) {
return $this->with['dependencies'];
}

$process = new Process(['npm', 'list', '--depth=0', '--json']);
$process->run();

$output = $process->getOutput();

$json = json_decode($output, true);

return $json['dependencies'];
}
}
39 changes: 39 additions & 0 deletions tests/Checks/NpmPackageInstalledCheckTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Vormkracht10\LaravelOK\Checks\Base\Result;
use Vormkracht10\LaravelOK\Checks\NpmPackageInstalledCheck;
use Vormkracht10\LaravelOK\Enums\Status;

it('returns ok if all packages are installed', function () {
$check = new NpmPackageInstalledCheck();
$check->shouldHave(['lodash', 'axios']);

$installedPackages = [
'dependencies' => [
'lodash' => '1.2.3',
'axios' => '4.5.6',
],
];

$result = $check->with($installedPackages)->run();

expect($result)->toBeInstanceOf(Result::class)
->status->toBe(Status::OK);
});

it('returns failed if some packages are missing', function () {
$check = new NpmPackageInstalledCheck();
$check->shouldHave(['lodash', 'axios']);

$installedPackages = [
'dependencies' => [
'lodash' => '1.2.3',
],
];

$result = $check->with($installedPackages)->run();

expect($result)->toBeInstanceOf(Result::class)
->status->toBe(Status::FAILED)
->getMessage()->toBe('The following packages are missing: axios');
});