From a8f36324b0917271bbe78791d24e56ea5b13b064 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Fri, 24 Nov 2023 15:35:02 -0300 Subject: [PATCH 1/8] Rename the application module exported by libs/stimulus to Stimulus --- src/Commands/ManifestCommand.php | 2 +- src/Manifest.php | 2 +- stubs/resources/js/controllers/index-importmap.js | 4 ++-- stubs/resources/js/controllers/index-node.js | 4 ++-- stubs/resources/js/libs/stimulus.js | 6 +++--- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Commands/ManifestCommand.php b/src/Commands/ManifestCommand.php index ac6f881..3720a7f 100644 --- a/src/Commands/ManifestCommand.php +++ b/src/Commands/ManifestCommand.php @@ -31,7 +31,7 @@ public function handle(Manifest $generator) // Run that command whenever you add a new controller or create them with // `php artisan stimulus:make controllerName` - import { application } from '../libs/stimulus' + import { Stimulus } from '../libs/stimulus' {$manifest} JS); diff --git a/src/Manifest.php b/src/Manifest.php index c78e325..376ccff 100644 --- a/src/Manifest.php +++ b/src/Manifest.php @@ -30,7 +30,7 @@ public function generateFrom(string $controllersPath): Collection return << Date: Fri, 24 Nov 2023 17:00:14 -0300 Subject: [PATCH 2/8] Run npm install && npm run build after installing with Node --- src/Commands/Concerns/InstallsForNode.php | 2 -- src/Commands/InstallCommand.php | 42 +++++++++++++++++++---- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/Commands/Concerns/InstallsForNode.php b/src/Commands/Concerns/InstallsForNode.php index 7989f4f..51d6e39 100644 --- a/src/Commands/Concerns/InstallsForNode.php +++ b/src/Commands/Concerns/InstallsForNode.php @@ -58,8 +58,6 @@ protected function updateNpmPackagesForNode() ] + $packages; }); - $this->afterMessages[] = 'Run: `npm install && npm run dev`'; - return true; }); } diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index 2152d7b..50424ea 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -4,6 +4,8 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\File; +use Symfony\Component\Process\Process; +use RuntimeException; class InstallCommand extends Command { @@ -24,21 +26,47 @@ public function handle(): int $this->installsForImportmaps(); } else { $this->installsForNode(); - } - if (! empty($this->afterMessages)) { - $this->newLine(); - $this->components->info('After Notes and Next Steps'); - $this->components->bulletList($this->afterMessages); - } else { - $this->components->info('Done'); + if (file_exists(base_path('pnpm-lock.yaml'))) { + $this->runCommands(['pnpm install', 'pnpm run build']); + } elseif (file_exists(base_path('yarn.lock'))) { + $this->runCommands(['yarn install', 'yarn run build']); + } else { + $this->runCommands(['npm install', 'npm run build']); + } } + $this->newLine(); + $this->components->info('Done'); $this->newLine(); return self::SUCCESS; } + + /** + * Run the given commands. + * + * @param array $commands + * @return void + */ + protected function runCommands($commands) + { + $process = Process::fromShellCommandline(implode(' && ', $commands), null, null, null, null); + + if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) { + try { + $process->setTty(true); + } catch (RuntimeException $e) { + $this->output->writeln(' WARN '.$e->getMessage().PHP_EOL); + } + } + + $process->run(function ($type, $line) { + $this->output->write(' '.$line); + }); + } + protected function usingImportmaps(): bool { return File::exists($this->importmapsFile()); From f11d914c41db10a49b3f343d8a78351cc25b9121 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Fri, 24 Nov 2023 17:06:17 -0300 Subject: [PATCH 3/8] Fix still referencing application instead of the renamed Stimulus --- stubs/resources/js/libs/stimulus.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/stubs/resources/js/libs/stimulus.js b/stubs/resources/js/libs/stimulus.js index f8e02fe..1ecdf34 100644 --- a/stubs/resources/js/libs/stimulus.js +++ b/stubs/resources/js/libs/stimulus.js @@ -3,7 +3,8 @@ import { Application } from '@hotwired/stimulus' const Stimulus = Application.start() // Configure Stimulus development experience -application.debug = false -window.Stimulus = Stimulus +Stimulus.debug = false + +window.Stimulus = Stimulus export { Stimulus } From cbb14f6045e6eae17e30ee7acc8fa7fd684b9559 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Fri, 24 Nov 2023 18:23:40 -0300 Subject: [PATCH 4/8] Support Strada --- .../Concerns/InstallsForImportmap.php | 4 +- src/Commands/Concerns/InstallsForNode.php | 7 ++-- src/Commands/InstallCommand.php | 9 ++++- src/Commands/StradaMakeCommand.php | 39 +++++++++++++++++++ src/StimulusGenerator.php | 19 ++++++++- src/StimulusLaravelServiceProvider.php | 1 + stubs/strada.stub | 8 ++++ 7 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 src/Commands/StradaMakeCommand.php create mode 100644 stubs/strada.stub diff --git a/src/Commands/Concerns/InstallsForImportmap.php b/src/Commands/Concerns/InstallsForImportmap.php index d4053cb..8efc5fd 100644 --- a/src/Commands/Concerns/InstallsForImportmap.php +++ b/src/Commands/Concerns/InstallsForImportmap.php @@ -46,7 +46,9 @@ protected function registerImportmapPins() { $this->components->task('pinning JS dependency (importmap)', function () { $this->callSilently('importmap:pin', [ - 'packages' => ['@hotwired/stimulus'], + 'packages' => collect($this->jsPackages()) + ->map(fn ($package, $version) => "{$package}@{$version}") + ->all(), ]); // Publishes the `@hotwired/stimulus-loading` package to public/ diff --git a/src/Commands/Concerns/InstallsForNode.php b/src/Commands/Concerns/InstallsForNode.php index 51d6e39..1088ac6 100644 --- a/src/Commands/Concerns/InstallsForNode.php +++ b/src/Commands/Concerns/InstallsForNode.php @@ -53,9 +53,10 @@ protected function updateNpmPackagesForNode() { $this->components->task('registering NPM dependency', function () { $this->updateNodePackages(function ($packages) { - return [ - '@hotwired/stimulus' => '^3.1.0', - ] + $packages; + return array_merge( + $packages, + $this->jsPackages(), + ); }); return true; diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index 50424ea..7cc057e 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -12,7 +12,7 @@ class InstallCommand extends Command use Concerns\InstallsForImportmap; use Concerns\InstallsForNode; - public $signature = 'stimulus:install'; + public $signature = 'stimulus:install {--strada : Sets up Strada as well.}'; public $description = 'Installs the Stimulus Laravel package.'; @@ -43,6 +43,13 @@ public function handle(): int return self::SUCCESS; } + protected function jsPackages(): array + { + return array_merge( + ['@hotwired/stimulus' => '^3.1.0'], + $this->hasOption('strada') ? ['@hotwired/strada' => '^1.0.0-beta1'] : [], + ); + } /** * Run the given commands. diff --git a/src/Commands/StradaMakeCommand.php b/src/Commands/StradaMakeCommand.php new file mode 100644 index 0000000..783257f --- /dev/null +++ b/src/Commands/StradaMakeCommand.php @@ -0,0 +1,39 @@ +components->info('Making Strada Component'); + + $this->components->task('creating strada component', function () use ($generator) { + $generator->createStrada($this->option('prefix'), $this->argument('name'), $this->option('bridge-name')); + + return true; + }); + + if (! File::exists(base_path('routes/importmap.php'))) { + $this->components->task('regenerating manifest', function () { + return $this->callSilently(ManifestCommand::class); + }); + } + + $this->newLine(); + $this->components->info('Done'); + + return self::SUCCESS; + } +} diff --git a/src/StimulusGenerator.php b/src/StimulusGenerator.php index 61f7d80..b563b19 100644 --- a/src/StimulusGenerator.php +++ b/src/StimulusGenerator.php @@ -12,16 +12,21 @@ public function __construct(private ?string $targetFolder = null) $this->targetFolder ??= rtrim(resource_path('js/controllers'), '/'); } - public function create(string $name): array + public function create(string $name, string $stub = null, callable $replacementsCallback = null): array { + $replacementsCallback ??= fn ($replacements) => $replacements; $controllerName = $this->controllerName($name); $targetFile = $this->targetFolder.'/'.$controllerName.'_controller.js'; File::ensureDirectoryExists(dirname($targetFile)); + $replacements = $replacementsCallback([ + '[attribute]' => $attributeName = $this->attributeName($name), + ]); + File::put( $targetFile, - str_replace('[attribute]', $attributeName = $this->attributeName($name), File::get(__DIR__.'/../stubs/controller.stub')), + str_replace(array_keys($replacements), array_values($replacements), File::get($stub ?: __DIR__.'/../stubs/controller.stub')), ); return [ @@ -31,6 +36,16 @@ public function create(string $name): array ]; } + public function createStrada(string $prefix, string $name, string $bridgeName = null): array + { + return $this->create("$prefix/$name", stub: __DIR__.'/../stubs/strada.stub', replacementsCallback: function (array $replacements) use ($bridgeName) { + return array_merge( + $replacements, + ['[bridge-name]' => $bridgeName ?? (string) Str::of($replacements['[attribute]'])->afterLast('/')], + ); + }); + } + private function controllerName(string $name): string { return Str::of($name)->replace('_controller', '')->snake('_'); diff --git a/src/StimulusLaravelServiceProvider.php b/src/StimulusLaravelServiceProvider.php index cf23ac2..d35ab77 100644 --- a/src/StimulusLaravelServiceProvider.php +++ b/src/StimulusLaravelServiceProvider.php @@ -22,6 +22,7 @@ public function configurePackage(Package $package): void ->hasCommands([ Commands\InstallCommand::class, Commands\MakeCommand::class, + Commands\StradaMakeCommand::class, Commands\CoreMakeCommand::class, Commands\PublishCommand::class, Commands\ManifestCommand::class, diff --git a/stubs/strada.stub b/stubs/strada.stub new file mode 100644 index 0000000..4253580 --- /dev/null +++ b/stubs/strada.stub @@ -0,0 +1,8 @@ +import { BridgeComponent, BridgeElement } from "@hotwired/strada" + +// Connects to data-controller="[attribute]" +export default class extends BridgeComponent { + static component = "[bridge-name]" + + // +} From fcc8470a1ad765753d22c32abcacfed8358db23f Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Fri, 24 Nov 2023 18:35:41 -0300 Subject: [PATCH 5/8] Build after creating the controller/component --- src/Commands/MakeCommand.php | 9 +++++++++ src/Commands/StradaMakeCommand.php | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Commands/MakeCommand.php b/src/Commands/MakeCommand.php index 96962c2..4fee4a6 100644 --- a/src/Commands/MakeCommand.php +++ b/src/Commands/MakeCommand.php @@ -5,6 +5,7 @@ use HotwiredLaravel\StimulusLaravel\StimulusGenerator; use Illuminate\Console\Command; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Process; class MakeCommand extends Command { @@ -26,6 +27,14 @@ public function handle(StimulusGenerator $generator): int $this->components->task('regenerating manifest', function () { return $this->callSilently(ManifestCommand::class); }); + + if (file_exists(base_path('pnpm-lock.yaml'))) { + Process::forever()->path(base_path())->run(['pnpm', 'run', 'build']); + } elseif (file_exists(base_path('yarn.lock'))) { + Process::forever()->path(base_path())->run(['yarn', 'run', 'build']); + } else { + Process::forever()->path(base_path())->run(['npm', 'run', 'build']); + } } $this->newLine(); diff --git a/src/Commands/StradaMakeCommand.php b/src/Commands/StradaMakeCommand.php index 783257f..0baadb3 100644 --- a/src/Commands/StradaMakeCommand.php +++ b/src/Commands/StradaMakeCommand.php @@ -5,6 +5,7 @@ use HotwiredLaravel\StimulusLaravel\StimulusGenerator; use Illuminate\Console\Command; use Illuminate\Support\Facades\File; +use Illuminate\Support\Facades\Process; class StradaMakeCommand extends Command { @@ -29,6 +30,14 @@ public function handle(StimulusGenerator $generator): int $this->components->task('regenerating manifest', function () { return $this->callSilently(ManifestCommand::class); }); + + if (file_exists(base_path('pnpm-lock.yaml'))) { + Process::forever()->path(base_path())->run(['pnpm', 'run', 'build']); + } elseif (file_exists(base_path('yarn.lock'))) { + Process::forever()->path(base_path())->run(['yarn', 'run', 'build']); + } else { + Process::forever()->path(base_path())->run(['npm', 'run', 'build']); + } } $this->newLine(); From cc97d0b38a2621b8a32f403f7671cd101dcdb1bd Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Fri, 24 Nov 2023 18:38:09 -0300 Subject: [PATCH 6/8] Fix bridge component name --- src/StimulusGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StimulusGenerator.php b/src/StimulusGenerator.php index b563b19..14e9d4e 100644 --- a/src/StimulusGenerator.php +++ b/src/StimulusGenerator.php @@ -41,7 +41,7 @@ public function createStrada(string $prefix, string $name, string $bridgeName = return $this->create("$prefix/$name", stub: __DIR__.'/../stubs/strada.stub', replacementsCallback: function (array $replacements) use ($bridgeName) { return array_merge( $replacements, - ['[bridge-name]' => $bridgeName ?? (string) Str::of($replacements['[attribute]'])->afterLast('/')], + ['[bridge-name]' => $bridgeName ?? (string) Str::of($replacements['[attribute]'])->afterLast('--')], ); }); } From 8e88ac04f2a5c50a372cec9a634a26d15b0c396a Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Fri, 24 Nov 2023 18:42:29 -0300 Subject: [PATCH 7/8] Pint --- src/Commands/InstallCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Commands/InstallCommand.php b/src/Commands/InstallCommand.php index 7cc057e..f94c732 100644 --- a/src/Commands/InstallCommand.php +++ b/src/Commands/InstallCommand.php @@ -4,8 +4,8 @@ use Illuminate\Console\Command; use Illuminate\Support\Facades\File; -use Symfony\Component\Process\Process; use RuntimeException; +use Symfony\Component\Process\Process; class InstallCommand extends Command { From 1f8092eaafa0e06df1ac88e1737e4f57ad4f66c5 Mon Sep 17 00:00:00 2001 From: Tony Messias Date: Fri, 24 Nov 2023 18:44:23 -0300 Subject: [PATCH 8/8] Fix tests --- tests/ManifestTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/ManifestTest.php b/tests/ManifestTest.php index 32c8b19..00f0540 100644 --- a/tests/ManifestTest.php +++ b/tests/ManifestTest.php @@ -19,7 +19,7 @@ public function generates_controllers_imports_given_a_path() <<<'JS' import HelloController from './hello_controller' - application.register('hello', HelloController) + Stimulus.register('hello', HelloController) JS, $manifest, ); @@ -28,7 +28,7 @@ public function generates_controllers_imports_given_a_path() <<<'JS' import Nested__DeepController from './nested/deep_controller' - application.register('nested--deep', Nested__DeepController) + Stimulus.register('nested--deep', Nested__DeepController) JS, $manifest, ); @@ -37,7 +37,7 @@ public function generates_controllers_imports_given_a_path() <<<'JS' import CoffeeController from './coffee_controller' - application.register('coffee', CoffeeController) + Stimulus.register('coffee', CoffeeController) JS, $manifest, ); @@ -46,7 +46,7 @@ public function generates_controllers_imports_given_a_path() <<<'JS' import TypeScriptController from './type_script_controller' - application.register('type-script', TypeScriptController) + Stimulus.register('type-script', TypeScriptController) JS, $manifest, ); @@ -55,7 +55,7 @@ public function generates_controllers_imports_given_a_path() <<<'JS' import Index from './index' - application.register('index', Index) + Stimulus.register('index', Index) JS, $manifest, );