Skip to content

Commit eac587f

Browse files
authored
Merge pull request #18 from hotwired-laravel/strada
Support Strada
2 parents 4db4287 + 1f8092e commit eac587f

File tree

14 files changed

+149
-31
lines changed

14 files changed

+149
-31
lines changed

src/Commands/Concerns/InstallsForImportmap.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ protected function registerImportmapPins()
4646
{
4747
$this->components->task('pinning JS dependency (importmap)', function () {
4848
$this->callSilently('importmap:pin', [
49-
'packages' => ['@hotwired/stimulus'],
49+
'packages' => collect($this->jsPackages())
50+
->map(fn ($package, $version) => "{$package}@{$version}")
51+
->all(),
5052
]);
5153

5254
// Publishes the `@hotwired/stimulus-loading` package to public/

src/Commands/Concerns/InstallsForNode.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,12 @@ protected function updateNpmPackagesForNode()
5353
{
5454
$this->components->task('registering NPM dependency', function () {
5555
$this->updateNodePackages(function ($packages) {
56-
return [
57-
'@hotwired/stimulus' => '^3.1.0',
58-
] + $packages;
56+
return array_merge(
57+
$packages,
58+
$this->jsPackages(),
59+
);
5960
});
6061

61-
$this->afterMessages[] = '<fg=white>Run: `<fg=yellow>npm install && npm run dev</>`</>';
62-
6362
return true;
6463
});
6564
}

src/Commands/InstallCommand.php

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use Illuminate\Console\Command;
66
use Illuminate\Support\Facades\File;
7+
use RuntimeException;
8+
use Symfony\Component\Process\Process;
79

810
class InstallCommand extends Command
911
{
1012
use Concerns\InstallsForImportmap;
1113
use Concerns\InstallsForNode;
1214

13-
public $signature = 'stimulus:install';
15+
public $signature = 'stimulus:install {--strada : Sets up Strada as well.}';
1416

1517
public $description = 'Installs the Stimulus Laravel package.';
1618

@@ -24,21 +26,54 @@ public function handle(): int
2426
$this->installsForImportmaps();
2527
} else {
2628
$this->installsForNode();
27-
}
2829

29-
if (! empty($this->afterMessages)) {
30-
$this->newLine();
31-
$this->components->info('After Notes and Next Steps');
32-
$this->components->bulletList($this->afterMessages);
33-
} else {
34-
$this->components->info('Done');
30+
if (file_exists(base_path('pnpm-lock.yaml'))) {
31+
$this->runCommands(['pnpm install', 'pnpm run build']);
32+
} elseif (file_exists(base_path('yarn.lock'))) {
33+
$this->runCommands(['yarn install', 'yarn run build']);
34+
} else {
35+
$this->runCommands(['npm install', 'npm run build']);
36+
}
3537
}
3638

39+
$this->newLine();
40+
$this->components->info('Done');
3741
$this->newLine();
3842

3943
return self::SUCCESS;
4044
}
4145

46+
protected function jsPackages(): array
47+
{
48+
return array_merge(
49+
['@hotwired/stimulus' => '^3.1.0'],
50+
$this->hasOption('strada') ? ['@hotwired/strada' => '^1.0.0-beta1'] : [],
51+
);
52+
}
53+
54+
/**
55+
* Run the given commands.
56+
*
57+
* @param array $commands
58+
* @return void
59+
*/
60+
protected function runCommands($commands)
61+
{
62+
$process = Process::fromShellCommandline(implode(' && ', $commands), null, null, null, null);
63+
64+
if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
65+
try {
66+
$process->setTty(true);
67+
} catch (RuntimeException $e) {
68+
$this->output->writeln(' <bg=yellow;fg=black> WARN </> '.$e->getMessage().PHP_EOL);
69+
}
70+
}
71+
72+
$process->run(function ($type, $line) {
73+
$this->output->write(' '.$line);
74+
});
75+
}
76+
4277
protected function usingImportmaps(): bool
4378
{
4479
return File::exists($this->importmapsFile());

src/Commands/MakeCommand.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use HotwiredLaravel\StimulusLaravel\StimulusGenerator;
66
use Illuminate\Console\Command;
77
use Illuminate\Support\Facades\File;
8+
use Illuminate\Support\Facades\Process;
89

910
class MakeCommand extends Command
1011
{
@@ -26,6 +27,14 @@ public function handle(StimulusGenerator $generator): int
2627
$this->components->task('regenerating manifest', function () {
2728
return $this->callSilently(ManifestCommand::class);
2829
});
30+
31+
if (file_exists(base_path('pnpm-lock.yaml'))) {
32+
Process::forever()->path(base_path())->run(['pnpm', 'run', 'build']);
33+
} elseif (file_exists(base_path('yarn.lock'))) {
34+
Process::forever()->path(base_path())->run(['yarn', 'run', 'build']);
35+
} else {
36+
Process::forever()->path(base_path())->run(['npm', 'run', 'build']);
37+
}
2938
}
3039

3140
$this->newLine();

src/Commands/ManifestCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function handle(Manifest $generator)
3131
// Run that command whenever you add a new controller or create them with
3232
// `php artisan stimulus:make controllerName`
3333
34-
import { application } from '../libs/stimulus'
34+
import { Stimulus } from '../libs/stimulus'
3535
3636
{$manifest}
3737
JS);

src/Commands/StradaMakeCommand.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace HotwiredLaravel\StimulusLaravel\Commands;
4+
5+
use HotwiredLaravel\StimulusLaravel\StimulusGenerator;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\File;
8+
use Illuminate\Support\Facades\Process;
9+
10+
class StradaMakeCommand extends Command
11+
{
12+
public $signature = 'strada:make
13+
{name : The Strada Component name (without bridge prefix.}
14+
{--prefix=bridge : The component prefix.}
15+
{--bridge-name= : The name of the native component.}';
16+
17+
public $description = 'Makes a new Strada Component.';
18+
19+
public function handle(StimulusGenerator $generator): int
20+
{
21+
$this->components->info('Making Strada Component');
22+
23+
$this->components->task('creating strada component', function () use ($generator) {
24+
$generator->createStrada($this->option('prefix'), $this->argument('name'), $this->option('bridge-name'));
25+
26+
return true;
27+
});
28+
29+
if (! File::exists(base_path('routes/importmap.php'))) {
30+
$this->components->task('regenerating manifest', function () {
31+
return $this->callSilently(ManifestCommand::class);
32+
});
33+
34+
if (file_exists(base_path('pnpm-lock.yaml'))) {
35+
Process::forever()->path(base_path())->run(['pnpm', 'run', 'build']);
36+
} elseif (file_exists(base_path('yarn.lock'))) {
37+
Process::forever()->path(base_path())->run(['yarn', 'run', 'build']);
38+
} else {
39+
Process::forever()->path(base_path())->run(['npm', 'run', 'build']);
40+
}
41+
}
42+
43+
$this->newLine();
44+
$this->components->info('Done');
45+
46+
return self::SUCCESS;
47+
}
48+
}

src/Manifest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function generateFrom(string $controllersPath): Collection
3030
return <<<JS
3131
3232
import {$controllerClassName} from '{$join(['.', $modulePath])}'
33-
application.register('{$tagName}', {$controllerClassName})
33+
Stimulus.register('{$tagName}', {$controllerClassName})
3434
JS;
3535
});
3636
}

src/StimulusGenerator.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ public function __construct(private ?string $targetFolder = null)
1212
$this->targetFolder ??= rtrim(resource_path('js/controllers'), '/');
1313
}
1414

15-
public function create(string $name): array
15+
public function create(string $name, string $stub = null, callable $replacementsCallback = null): array
1616
{
17+
$replacementsCallback ??= fn ($replacements) => $replacements;
1718
$controllerName = $this->controllerName($name);
1819
$targetFile = $this->targetFolder.'/'.$controllerName.'_controller.js';
1920

2021
File::ensureDirectoryExists(dirname($targetFile));
2122

23+
$replacements = $replacementsCallback([
24+
'[attribute]' => $attributeName = $this->attributeName($name),
25+
]);
26+
2227
File::put(
2328
$targetFile,
24-
str_replace('[attribute]', $attributeName = $this->attributeName($name), File::get(__DIR__.'/../stubs/controller.stub')),
29+
str_replace(array_keys($replacements), array_values($replacements), File::get($stub ?: __DIR__.'/../stubs/controller.stub')),
2530
);
2631

2732
return [
@@ -31,6 +36,16 @@ public function create(string $name): array
3136
];
3237
}
3338

39+
public function createStrada(string $prefix, string $name, string $bridgeName = null): array
40+
{
41+
return $this->create("$prefix/$name", stub: __DIR__.'/../stubs/strada.stub', replacementsCallback: function (array $replacements) use ($bridgeName) {
42+
return array_merge(
43+
$replacements,
44+
['[bridge-name]' => $bridgeName ?? (string) Str::of($replacements['[attribute]'])->afterLast('--')],
45+
);
46+
});
47+
}
48+
3449
private function controllerName(string $name): string
3550
{
3651
return Str::of($name)->replace('_controller', '')->snake('_');

src/StimulusLaravelServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function configurePackage(Package $package): void
2222
->hasCommands([
2323
Commands\InstallCommand::class,
2424
Commands\MakeCommand::class,
25+
Commands\StradaMakeCommand::class,
2526
Commands\CoreMakeCommand::class,
2627
Commands\PublishCommand::class,
2728
Commands\ManifestCommand::class,
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { application } from 'libs/stimulus'
1+
import { Stimulus } from 'libs/stimulus'
22

33
// Eager load all controllers defined in the import map under controllers/**/*_controller
44
import { eagerLoadControllersFrom } from '@hotwired/stimulus-loading'
5-
eagerLoadControllersFrom('controllers', application)
5+
eagerLoadControllersFrom('controllers', Stimulus)

0 commit comments

Comments
 (0)