diff --git a/src/Console/Commands/SetupCpVite.php b/src/Console/Commands/SetupCpVite.php index 8c4350c339a..bdc7f34ed7c 100644 --- a/src/Console/Commands/SetupCpVite.php +++ b/src/Console/Commands/SetupCpVite.php @@ -46,7 +46,7 @@ public function handle(): void private function installDependencies(): self { - spin( + $result = spin( callback: function () { $packageJsonPath = base_path('package.json'); $contents = File::json($packageJsonPath); @@ -63,13 +63,18 @@ private function installDependencies(): self File::put($packageJsonPath, json_encode($contents, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); - Process::path(base_path())->run('npm install', function (string $type, string $buffer) { - echo $buffer; - }); + return Process::path(base_path())->run('npm install'); }, message: 'Installing dependencies...' ); + if ($result->failed()) { + $this->line($result->errorOutput() ?: $result->output()); + $this->components->error('Failed to install dependencies. You need to run "npm install" manually.'); + + return $this; + } + $this->components->info('Installed dependencies'); return $this; diff --git a/tests/Console/Commands/SetupCpViteTest.php b/tests/Console/Commands/SetupCpViteTest.php index 40f410bfd21..738de96f92d 100644 --- a/tests/Console/Commands/SetupCpViteTest.php +++ b/tests/Console/Commands/SetupCpViteTest.php @@ -174,6 +174,24 @@ public function boot(): void PHP, $this->files->get(app_path('Providers/AppServiceProvider.php'))); } + #[Test] + public function it_shows_error_when_npm_install_fails_but_continues() + { + Process::fake([ + '*' => Process::result( + output: '', + errorOutput: 'npm ERR! code ERESOLVE', + exitCode: 1, + ), + ]); + + $this + ->artisan('statamic:setup-cp-vite') + ->expectsOutputToContain('Failed to install dependencies') + ->expectsOutputToContain('npm ERR! code ERESOLVE') + ->expectsOutputToContain('Added cp:dev and cp:build scripts to package.json'); + } + private function makeNecessaryFiles(): void { $this->files->put(app_path('Providers/AppServiceProvider.php'), <<<'PHP'