diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index f02fcbf8..97852247 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -6,14 +6,18 @@ use Native\Electron\Traits\Installer; use function Laravel\Prompts\confirm; +use function Laravel\Prompts\info; use function Laravel\Prompts\intro; +use function Laravel\Prompts\note; use function Laravel\Prompts\outro; class InstallCommand extends Command { use Installer; - protected $signature = 'native:install {--force : Overwrite existing files by default} {--installer=npm : The package installer to use: npm, yarn or pnpm}'; + protected $signature = 'native:install + {--force : Overwrite existing files by default} + {--installer=npm : The package installer to use: npm, yarn or pnpm}'; protected $description = 'Install all of the NativePHP resources'; @@ -26,9 +30,15 @@ public function handle(): void $this->call('vendor:publish', ['--tag' => 'nativephp-provider']); $this->call('vendor:publish', ['--tag' => 'nativephp-config']); + $this->installComposerScript(); + $installer = $this->getInstaller($this->option('installer'), $withoutInteraction); - $this->installNPMDependencies(force: $this->option('force'), installer: $installer, withoutInteraction: $withoutInteraction); + $this->installNPMDependencies( + force: $this->option('force'), + installer: $installer, + withoutInteraction: $withoutInteraction + ); $shouldPromptForServe = ! $withoutInteraction && ! $this->option('force'); @@ -38,4 +48,28 @@ public function handle(): void outro('NativePHP scaffolding installed successfully.'); } + + private function installComposerScript() + { + info('Installing `composer native:dev` script alias...'); + + $composer = json_decode(file_get_contents(base_path('composer.json'))); + throw_unless($composer, RuntimeException::class, "composer.json couldn't be parsed"); + + $composerScripts = $composer->scripts ?? (object) []; + + $composerScripts->{'native:dev'} = [ + 'Composer\\Config::disableProcessTimeout', + 'npx concurrently -c "#93c5fd,#c4b5fd" "php artisan native:serve --no-interaction" "npm run dev" --names=app,vite' + ]; + + data_set($composer, 'scripts', $composerScripts); + + file_put_contents( + base_path('composer.json'), + json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL + ); + + note('native:dev script installed!'); + } }