From 67a030dff341470c3ef1af579aa2d8c90d3a1e23 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Tue, 26 Sep 2023 13:59:39 +0200 Subject: [PATCH 1/8] Check: wip --- src/Checks/NpmPackageInstalledCheck.php | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/Checks/NpmPackageInstalledCheck.php diff --git a/src/Checks/NpmPackageInstalledCheck.php b/src/Checks/NpmPackageInstalledCheck.php new file mode 100644 index 0000000..3ae3681 --- /dev/null +++ b/src/Checks/NpmPackageInstalledCheck.php @@ -0,0 +1,51 @@ +shouldHave = $packages; + + return $this; + } + + public function run(): Result + { + $result = Result::new(); + + $installedPackages = $this->getInstalledPackages(); + + dd($installedPackages); + + $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}"); + } + + private function getInstalledPackages() + { + $process = Process::fromShellCommandline('npm list --depth=0 --json'); + + $process->run(); + + $output = $process->getOutput(); + + $json = json_decode($output, true); + + return $json['dependencies']; + } +} From 590fa8158216572ef5a118bd7d543121b45cd936 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Tue, 26 Sep 2023 14:26:39 +0200 Subject: [PATCH 2/8] wip --- src/Checks/NpmPackageInstalledCheck.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Checks/NpmPackageInstalledCheck.php b/src/Checks/NpmPackageInstalledCheck.php index 3ae3681..bc7a9d4 100644 --- a/src/Checks/NpmPackageInstalledCheck.php +++ b/src/Checks/NpmPackageInstalledCheck.php @@ -23,8 +23,6 @@ public function run(): Result $installedPackages = $this->getInstalledPackages(); - dd($installedPackages); - $missingPackages = array_diff($this->shouldHave, array_keys($installedPackages)); if (count($missingPackages) === 0) { @@ -38,9 +36,7 @@ public function run(): Result private function getInstalledPackages() { - $process = Process::fromShellCommandline('npm list --depth=0 --json'); - - $process->run(); + $process = Process::run('npm list --depth=0 --json'); $output = $process->getOutput(); From b22a1f60dbff16f70ad5be12454f19d31961339b Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Tue, 26 Sep 2023 14:32:54 +0200 Subject: [PATCH 3/8] wip --- src/Checks/NpmPackageInstalledCheck.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Checks/NpmPackageInstalledCheck.php b/src/Checks/NpmPackageInstalledCheck.php index bc7a9d4..41bf66f 100644 --- a/src/Checks/NpmPackageInstalledCheck.php +++ b/src/Checks/NpmPackageInstalledCheck.php @@ -2,7 +2,7 @@ namespace Vormkracht10\LaravelOK\Checks; -use Illuminate\Support\Facades\Process; +use Symfony\Component\Process\Process; use Vormkracht10\LaravelOK\Checks\Base\Check; use Vormkracht10\LaravelOK\Checks\Base\Result; @@ -36,7 +36,8 @@ public function run(): Result private function getInstalledPackages() { - $process = Process::run('npm list --depth=0 --json'); + $process = new Process(['npm', 'list', '--depth=0', '--json']); + $process->run(); $output = $process->getOutput(); From 3199566b2bd420e3916b025c4f7d6ec648a264f8 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Tue, 26 Sep 2023 14:50:57 +0200 Subject: [PATCH 4/8] wip --- src/Checks/NpmPackageInstalledCheck.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Checks/NpmPackageInstalledCheck.php b/src/Checks/NpmPackageInstalledCheck.php index 41bf66f..70d475d 100644 --- a/src/Checks/NpmPackageInstalledCheck.php +++ b/src/Checks/NpmPackageInstalledCheck.php @@ -10,6 +10,8 @@ class NpmPackageInstalledCheck extends Check { protected array $shouldHave = []; + protected array $with = []; + public function shouldHave(array $packages): self { $this->shouldHave = $packages; @@ -21,7 +23,7 @@ public function run(): Result { $result = Result::new(); - $installedPackages = $this->getInstalledPackages(); + $installedPackages = $this->data(); $missingPackages = array_diff($this->shouldHave, array_keys($installedPackages)); @@ -34,15 +36,11 @@ public function run(): Result return $result->failed("The following packages are missing: {$missingPackages}"); } - private function getInstalledPackages() + protected function data() { - $process = new Process(['npm', 'list', '--depth=0', '--json']); - $process->run(); - - $output = $process->getOutput(); - - $json = json_decode($output, true); - - return $json['dependencies']; + return $this->with ?? json_decode( + new Process(['npm', 'list', '--depth=0', '--json'])->run()->getOutput(), + true, + )['dependencies']; } } From 97b28f845135b839e9547fe124b6b69116534ff0 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Tue, 26 Sep 2023 14:57:54 +0200 Subject: [PATCH 5/8] Add tests --- src/Checks/NpmPackageInstalledCheck.php | 23 +++++++++-- tests/Checks/NpmPackageInstalledCheckTest.php | 40 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 tests/Checks/NpmPackageInstalledCheckTest.php diff --git a/src/Checks/NpmPackageInstalledCheck.php b/src/Checks/NpmPackageInstalledCheck.php index 70d475d..f08ab2a 100644 --- a/src/Checks/NpmPackageInstalledCheck.php +++ b/src/Checks/NpmPackageInstalledCheck.php @@ -19,6 +19,13 @@ public function shouldHave(array $packages): self return $this; } + public function with(array $data): self + { + $this->with = $data; + + return $this; + } + public function run(): Result { $result = Result::new(); @@ -38,9 +45,17 @@ public function run(): Result protected function data() { - return $this->with ?? json_decode( - new Process(['npm', 'list', '--depth=0', '--json'])->run()->getOutput(), - true, - )['dependencies']; + 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']; } } diff --git a/tests/Checks/NpmPackageInstalledCheckTest.php b/tests/Checks/NpmPackageInstalledCheckTest.php new file mode 100644 index 0000000..783335b --- /dev/null +++ b/tests/Checks/NpmPackageInstalledCheckTest.php @@ -0,0 +1,40 @@ +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'); +}); \ No newline at end of file From fb96be945e49eb141461d5153b7bbaca4ab52f4a Mon Sep 17 00:00:00 2001 From: Baspa Date: Tue, 26 Sep 2023 12:58:27 +0000 Subject: [PATCH 6/8] Fix styling --- tests/Checks/NpmPackageInstalledCheckTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Checks/NpmPackageInstalledCheckTest.php b/tests/Checks/NpmPackageInstalledCheckTest.php index 783335b..35b528a 100644 --- a/tests/Checks/NpmPackageInstalledCheckTest.php +++ b/tests/Checks/NpmPackageInstalledCheckTest.php @@ -1,6 +1,5 @@ toBeInstanceOf(Result::class) ->status->toBe(Status::FAILED) ->getMessage()->toBe('The following packages are missing: axios'); -}); \ No newline at end of file +}); From 413a0f2f8c9ca03c7177293dcbb7315388203e05 Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Tue, 26 Sep 2023 15:03:38 +0200 Subject: [PATCH 7/8] Update README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 1c095e2..0e942d4 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,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. From b0c44dcbfcf30515d09903e01f4c25070a5f366c Mon Sep 17 00:00:00 2001 From: Bas van Dinther Date: Tue, 26 Sep 2023 15:04:18 +0200 Subject: [PATCH 8/8] Update README --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 0e942d4..c84518c 100644 --- a/README.md +++ b/README.md @@ -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.