33namespace HotwiredLaravel \StimulusLaravel \Commands \Concerns ;
44
55use Illuminate \Support \Facades \File ;
6+ use Illuminate \Support \Facades \Process ;
67
78/**
89 * @mixin \HotwiredLaravel\StimulusLaravel\Commands\InstallCommand
@@ -17,51 +18,55 @@ protected function installsForImportmaps()
1718
1819 protected function publishJsFilesForImportmaps ()
1920 {
20- $ this ->components ->task ('publishing JS files ' , function () {
21- File::ensureDirectoryExists (resource_path ('js/controllers ' ));
22- File::ensureDirectoryExists (resource_path ('js/libs ' ));
21+ File::ensureDirectoryExists (resource_path ('js/controllers ' ));
22+ File::ensureDirectoryExists (resource_path ('js/libs ' ));
2323
24- File::copy (__DIR__ .'/../../../stubs/resources/js/libs/stimulus.js ' , resource_path ('js/libs/stimulus.js ' ));
25- File::copy (__DIR__ .'/../../../stubs/resources/js/controllers/hello_controller.js ' , resource_path ('js/controllers/hello_controller.js ' ));
26- File::copy (__DIR__ .'/../../../stubs/resources/js/controllers/index-importmap.js ' , resource_path ('js/controllers/index.js ' ));
24+ File::copy (__DIR__ .'/../../../stubs/resources/js/libs/stimulus.js ' , resource_path ('js/libs/stimulus.js ' ));
25+ File::copy (__DIR__ .'/../../../stubs/resources/js/controllers/hello_controller.js ' , resource_path ('js/controllers/hello_controller.js ' ));
26+ File::copy (__DIR__ .'/../../../stubs/resources/js/controllers/index-importmap.js ' , resource_path ('js/controllers/index.js ' ));
2727
28- $ libsIndexFile = resource_path ('js/libs/index.js ' );
29- $ libsIndexSourceFile = __DIR__ .'/../../../stubs/resources/js/libs/index-importmap.js ' ;
28+ $ libsIndexFile = resource_path ('js/libs/index.js ' );
29+ $ libsIndexSourceFile = __DIR__ .'/../../../stubs/resources/js/libs/index-importmap.js ' ;
3030
31- if (File::exists ($ libsIndexFile )) {
32- $ importLine = trim (File::get ($ libsIndexSourceFile ));
31+ if (File::exists ($ libsIndexFile )) {
32+ $ importLine = trim (File::get ($ libsIndexSourceFile ));
3333
34- if (! str_contains (File::get ($ libsIndexFile ), $ importLine )) {
35- File::append ($ libsIndexFile , PHP_EOL .$ importLine .PHP_EOL );
36- }
37- } else {
38- File::copy ($ libsIndexSourceFile , $ libsIndexFile );
34+ if (! str_contains (File::get ($ libsIndexFile ), $ importLine )) {
35+ File::append ($ libsIndexFile , PHP_EOL .$ importLine .PHP_EOL );
3936 }
40-
41- return true ;
42- });
37+ } else {
38+ File:: copy ( $ libsIndexSourceFile , $ libsIndexFile ) ;
39+ }
4340 }
4441
4542 protected function registerImportmapPins ()
4643 {
47- $ this ->components ->task ('pinning JS dependency (importmap) ' , function () {
48- $ this ->callSilently ('importmap:pin ' , [
49- 'packages ' => collect ($ this ->jsPackages ())
50- ->map (fn ($ version , $ package ) => "{$ package }@ {$ version }" )
51- ->values ()
52- ->all (),
53- ]);
54-
55- // Publishes the `@hotwired/stimulus-loading` package to public/
56- $ this ->callSilently ('vendor:publish ' , [
57- '--tag ' => 'stimulus-laravel-assets ' ,
58- ]);
44+ $ dependencies = collect ($ this ->jsPackages ())
45+ ->map (fn ($ version , $ package ) => "{$ package }@ {$ version }" )
46+ ->values ()
47+ ->all ();
5948
60- File::append ($ this ->importmapsFile (), <<<'IMPORTMAP'
61- Importmap::pin("@hotwired/stimulus-loading", to: "vendor/stimulus-laravel/stimulus-loading.js", preload: true);
62- IMPORTMAP);
49+ Process::forever ()->run (array_merge ([
50+ $ this ->phpBinary (),
51+ 'artisan ' ,
52+ 'importmap:pin ' ,
53+ ], $ dependencies ), function ($ _type , $ output ) {
54+ $ this ->output ->write ($ output );
55+ });
6356
64- return true ;
57+ // Publishes the `@hotwired/stimulus-loading` package to public/vendor
58+ Process::forever ()->run ([
59+ $ this ->phpBinary (),
60+ 'artisan ' ,
61+ 'vendor:publish ' ,
62+ '--tag ' ,
63+ 'stimulus-laravel-assets ' ,
64+ ], function ($ _type , $ output ) {
65+ $ this ->output ->write ($ output );
6566 });
67+
68+ File::append ($ this ->importmapsFile (), <<<'IMPORTMAP'
69+ Importmap::pin("@hotwired/stimulus-loading", to: "vendor/stimulus-laravel/stimulus-loading.js", preload: true);
70+ IMPORTMAP);
6671 }
6772}
0 commit comments