44
55use Illuminate \Console \Command ;
66use Illuminate \Support \Facades \File ;
7+ use RuntimeException ;
8+ use Symfony \Component \Process \Process ;
79
810class 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 ());
0 commit comments