|
4 | 4 |
|
5 | 5 | use Illuminate\Console\Command;
|
6 | 6 | use Illuminate\Filesystem\Filesystem;
|
| 7 | +use Illuminate\Support\Str; |
7 | 8 |
|
8 | 9 | class TurboInstallCommand extends Command
|
9 | 10 | {
|
10 |
| - public $signature = 'turbo:install {--jet}'; |
| 11 | + public $signature = 'turbo:install {--jet} {--stimulus : To install Stimulus or not.}'; |
11 | 12 | public $description = 'Install the Turbo resources';
|
12 | 13 |
|
13 | 14 | public function handle()
|
14 | 15 | {
|
15 | 16 | $this->updateNodePackages(function ($packages) {
|
16 | 17 | return [
|
17 | 18 | '@hotwired/turbo' => '^7.0.0-beta.3',
|
18 |
| - 'stimulus' => '^2.0.0', |
19 |
| - '@stimulus/webpack-helpers' => '^2.0.0', |
20 | 19 | 'laravel-echo' => '^1.10.0',
|
21 | 20 | 'pusher-js' => '^7.0.2',
|
22 | 21 | ] + $packages;
|
23 | 22 | });
|
24 | 23 |
|
| 24 | + if ($this->option('stimulus')) { |
| 25 | + $this->updateNodePackages(function ($packages) { |
| 26 | + return [ |
| 27 | + 'stimulus' => '^2.0.0', |
| 28 | + '@stimulus/webpack-helpers' => '^2.0.0', |
| 29 | + ] + $packages; |
| 30 | + }); |
| 31 | + } |
| 32 | + |
25 | 33 | if ($this->option('jet')) {
|
26 | 34 | $this->updateNodePackages(function ($packages) {
|
27 | 35 | return [
|
28 | 36 | 'alpinejs' => '^2.8.0',
|
| 37 | + 'alpine-turbo-drive-adapter' => '^1.0.1', |
29 | 38 | ] + $packages;
|
30 | 39 | });
|
31 | 40 |
|
32 | 41 | if ((new Filesystem())->exists(resource_path('views/layouts/app.blade.php'))) {
|
33 |
| - (new Filesystem())->put( |
34 |
| - resource_path('views/layouts/app.blade.php'), |
35 |
| - str_replace( |
36 |
| - ' @livewireScripts', |
37 |
| - " @livewireScripts\n" . ' <script src="https://cdn.jsdelivr.net/gh/livewire/turbolinks@v0.1.x/dist/livewire-turbolinks.js" data-turbolinks-eval="false" data-turbo-eval="false"></script>', |
38 |
| - (new Filesystem())->get(resource_path('views/layouts/app.blade.php')) |
39 |
| - ) |
40 |
| - ); |
| 42 | + $this->updateJetstreamLayouts(); |
41 | 43 | }
|
42 | 44 | }
|
43 | 45 |
|
44 | 46 | // JS scaffold...
|
45 |
| - (new Filesystem())->ensureDirectoryExists(resource_path('js/controllers')); |
46 | 47 | (new Filesystem())->ensureDirectoryExists(resource_path('js/elements'));
|
| 48 | + |
47 | 49 | copy(__DIR__ . '/../../stubs/resources/js/app.js', resource_path('js/app.js'));
|
| 50 | + |
| 51 | + $this->replaceJsStub( |
| 52 | + resource_path('js/app.js'), |
| 53 | + '//=inject-alpine', |
| 54 | + $this->option('jet') |
| 55 | + ? (new Filesystem())->get(__DIR__ . '/../../stubs/resources/js/alpine.js') |
| 56 | + : '' |
| 57 | + ); |
| 58 | + |
| 59 | + $this->replaceJsStub( |
| 60 | + resource_path('js/app.js'), |
| 61 | + '//=inject-stimulus', |
| 62 | + $this->option('stimulus') |
| 63 | + ? (new Filesystem())->get(__DIR__ . '/../../stubs/resources/js/stimulus.js') |
| 64 | + : '' |
| 65 | + ); |
| 66 | + |
48 | 67 | copy(__DIR__ . '/../../stubs/resources/js/bootstrap.js', resource_path('js/bootstrap.js'));
|
49 | 68 | copy(__DIR__ . '/../../stubs/resources/js/echo.js', resource_path('js/echo.js'));
|
50 | 69 | copy(__DIR__ . '/../../stubs/resources/js/elements/turbo-echo-stream-tag.js', resource_path('js/elements/turbo-echo-stream-tag.js'));
|
51 |
| - copy(__DIR__ . '/../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js')); |
| 70 | + |
| 71 | + if ($this->option('stimulus')) { |
| 72 | + (new Filesystem())->ensureDirectoryExists(resource_path('js/controllers')); |
| 73 | + copy(__DIR__ . '/../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js')); |
| 74 | + } |
52 | 75 |
|
53 | 76 | $this->info('Turbo Laravel scaffolding installed successfully.');
|
54 | 77 | $this->comment('Please execute the "npm install && npm run dev" command to build your assets.');
|
@@ -83,4 +106,51 @@ protected static function updateNodePackages(callable $callback, $dev = true)
|
83 | 106 | json_encode($packages, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) . PHP_EOL
|
84 | 107 | );
|
85 | 108 | }
|
| 109 | + |
| 110 | + private function replaceJsStub(string $inFile, string $lookFor, string $replaceWith): void |
| 111 | + { |
| 112 | + (new Filesystem())->put( |
| 113 | + $inFile, |
| 114 | + (string)Str::of((new Filesystem())->get($inFile)) |
| 115 | + ->replace($lookFor, $replaceWith |
| 116 | + ) |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + private function updateJetstreamLayouts(): void |
| 121 | + { |
| 122 | + (new Filesystem())->put( |
| 123 | + resource_path('views/layouts/app.blade.php'), |
| 124 | + str_replace( |
| 125 | + ' @livewireScripts', |
| 126 | + " @livewireScripts\n" . ' <script src="https://cdn.jsdelivr.net/gh/livewire/turbolinks@v0.1.x/dist/livewire-turbolinks.js" data-turbolinks-eval="false" data-turbo-eval="false"></script>', |
| 127 | + (new Filesystem())->get(resource_path('views/layouts/app.blade.php')) |
| 128 | + ) |
| 129 | + ); |
| 130 | + |
| 131 | + // Also add the Livewire scripts to the guest layout. This is done because |
| 132 | + // Livewire and Alpine don't seem to play well with Turbo Drive when it |
| 133 | + // was already started, as app.js is loaded in the guests layout too. |
| 134 | + |
| 135 | + (new Filesystem())->put( |
| 136 | + resource_path('views/layouts/guest.blade.php'), |
| 137 | + str_replace( |
| 138 | + ' <link rel="stylesheet" href="{{ mix(\'css/app.css\') }}">', |
| 139 | + ' <link rel="stylesheet" href="{{ mix(\'css/app.css\') }}">' . |
| 140 | + "\n @livewireStyles", |
| 141 | + (new Filesystem())->get(resource_path('views/layouts/guest.blade.php')) |
| 142 | + ) |
| 143 | + ); |
| 144 | + |
| 145 | + (new Filesystem())->put( |
| 146 | + resource_path('views/layouts/guest.blade.php'), |
| 147 | + str_replace( |
| 148 | + ' </body>', |
| 149 | + " @livewireScripts\n" . |
| 150 | + ' <script src="https://cdn.jsdelivr.net/gh/livewire/turbolinks@v0.1.x/dist/livewire-turbolinks.js" data-turbolinks-eval="false" data-turbo-eval="false"></script>' . |
| 151 | + "\n </body>", |
| 152 | + (new Filesystem())->get(resource_path('views/layouts/guest.blade.php')) |
| 153 | + ) |
| 154 | + ); |
| 155 | + } |
86 | 156 | }
|
0 commit comments