generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathInstallsForImportmap.php
72 lines (60 loc) · 2.5 KB
/
InstallsForImportmap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace HotwiredLaravel\StimulusLaravel\Commands\Concerns;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
/**
* @mixin \HotwiredLaravel\StimulusLaravel\Commands\InstallCommand
*/
trait InstallsForImportmap
{
protected function installsForImportmaps()
{
$this->publishJsFilesForImportmaps();
$this->registerImportmapPins();
}
protected function publishJsFilesForImportmaps()
{
File::ensureDirectoryExists(resource_path('js/controllers'));
File::ensureDirectoryExists(resource_path('js/libs'));
File::copy(__DIR__.'/../../../stubs/resources/js/libs/stimulus.js', resource_path('js/libs/stimulus.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/controllers/hello_controller.js', resource_path('js/controllers/hello_controller.js'));
File::copy(__DIR__.'/../../../stubs/resources/js/controllers/index-importmap.js', resource_path('js/controllers/index.js'));
$libsIndexFile = resource_path('js/libs/index.js');
$libsIndexSourceFile = __DIR__.'/../../../stubs/resources/js/libs/index-importmap.js';
if (File::exists($libsIndexFile)) {
$importLine = trim(File::get($libsIndexSourceFile));
if (! str_contains(File::get($libsIndexFile), $importLine)) {
File::append($libsIndexFile, PHP_EOL.$importLine.PHP_EOL);
}
} else {
File::copy($libsIndexSourceFile, $libsIndexFile);
}
}
protected function registerImportmapPins()
{
$dependencies = collect($this->jsPackages())
->map(fn ($version, $package): string => "{$package}@{$version}")
->values()
->all();
Process::forever()->run(array_merge([
$this->phpBinary(),
'artisan',
'importmap:pin',
], $dependencies), function ($_type, $output): void {
$this->output->write($output);
});
// Publishes the `@hotwired/stimulus-loading` package to public/vendor
Process::forever()->run([
$this->phpBinary(),
'artisan',
'vendor:publish',
'--tag',
'stimulus-laravel-assets',
], function ($_type, $output): void {
$this->output->write($output);
});
File::append($this->importmapsFile(), <<<'IMPORTMAP'
Importmap::pin("@hotwired/stimulus-loading", to: "vendor/stimulus-laravel/stimulus-loading.js", preload: true);
IMPORTMAP);
}
}