forked from tonysm/importmap-laravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstallCommand.php
175 lines (147 loc) · 5.76 KB
/
InstallCommand.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace Tonysm\ImportmapLaravel\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Process;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Process\PhpExecutableFinder;
use Tonysm\ImportmapLaravel\Actions\FixJsImportPaths;
use Tonysm\ImportmapLaravel\Actions\ReplaceOrAppendTags;
#[AsCommand('importmap:install')]
class InstallCommand extends Command
{
public $signature = 'importmap:install';
public $description = 'Installs the package.';
public function handle(): int
{
File::ensureDirectoryExists(resource_path('js'));
$this->convertLocalImportsFromUsingDots();
$this->publishImportmapFiles();
$this->importDependenciesFromNpm();
$this->updateAppLayouts();
$this->deleteNpmRelatedFiles();
$this->configureIgnoredFolder();
$this->runStorageLinkCommand();
$this->newLine();
$this->components->info('Importmap Laravel was installed succesfully.');
return self::SUCCESS;
}
private function deleteNpmRelatedFiles(): void
{
$files = [
'package.json',
'package-lock.json',
'webpack.mix.js',
'postcss.config.js',
'vite.config.js',
];
collect($files)
->map(fn ($file) => base_path($file))
->filter(fn ($file) => File::exists($file))
->each(fn ($file) => File::delete($file));
}
private function publishImportmapFiles(): void
{
File::copy(dirname(__DIR__, 2).implode(DIRECTORY_SEPARATOR, ['', 'stubs', 'routes', 'importmap.php']), base_path(implode(DIRECTORY_SEPARATOR, ['routes', 'importmap.php'])));
File::copy(dirname(__DIR__, 2).implode(DIRECTORY_SEPARATOR, ['', 'stubs', 'jsconfig.json']), base_path('jsconfig.json'));
}
private function convertLocalImportsFromUsingDots(): void
{
(new FixJsImportPaths(rtrim(resource_path('js'), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR))();
}
private function importDependenciesFromNpm(): void
{
if (! File::exists($packageJsonFile = base_path('package.json'))) {
return;
}
$filteredOutDependencies = [
'@tailwindcss/forms',
'@tailwindcss/typography',
'autoprefixer',
'laravel-vite-plugin',
'postcss',
'tailwindcss',
'vite',
];
$packageJson = json_decode(File::get($packageJsonFile), true);
$dependencies = collect(array_replace($packageJson['dependencies'] ?? [], $packageJson['devDependencies'] ?? []))
->filter(fn ($_version, $package): bool => ! in_array($package, $filteredOutDependencies))
->filter(function ($_version, $package): bool {
if ($package !== 'axios') {
return true;
}
$this->output->warning('It seems you are using axios. Axios is not compatible with importmaps, so we are skipping its installation.');
return false;
})
// Axios has an issue with importmaps, so we'll hardcode the version for now...
->map(fn ($version, $package): string => $package === 'axios' ? '[email protected]' : "\"{$package}@{$version}\"");
if (trim($dependencies->join('')) === '') {
return;
}
Process::forever()->run(array_merge([
$this->phpBinary(),
'artisan',
'importmap:pin',
], $dependencies->all()), function ($_type, $output): void {
$this->output->write($output);
});
}
private function updateAppLayouts(): void
{
$this->existingLayoutFiles()->each(fn ($file) => File::put(
$file,
(new ReplaceOrAppendTags)(File::get($file)),
));
}
private function existingLayoutFiles()
{
return collect(['app', 'guest'])
->map(fn ($file) => resource_path("views/layouts/{$file}.blade.php"))
->filter(fn ($file) => File::exists($file));
}
private function configureIgnoredFolder(): void
{
if (Str::contains(File::get(base_path('.gitignore')), 'public/js')) {
return;
}
File::append(base_path('.gitignore'), "\n/public/js\n");
}
private function runStorageLinkCommand(): void
{
if ($this->components->confirm('To be able to serve your assets in development, the resource/js folder will be symlinked to your public/js. Would you like to do that now?', true)) {
if ($this->usingSail() && ! env('LARAVEL_SAIL')) {
Process::forever()->run([
'./vendor/bin/sail',
'up',
'-d',
], function ($_type, $output): void {
$this->output->write($output);
});
Process::forever()->run([
'./vendor/bin/sail',
'artisan',
'storage:link',
], function ($_type, $output): void {
$this->output->write($output);
});
} else {
Process::forever()->run([
$this->phpBinary(),
'artisan',
'storage:link',
], function ($_type, $output): void {
$this->output->write($output);
});
}
}
}
private function usingSail(): bool
{
return file_exists(base_path('docker-compose.yml')) && str_contains(file_get_contents(base_path('composer.json')), 'laravel/sail');
}
private function phpBinary(): string
{
return (new PhpExecutableFinder)->find(false) ?: 'php';
}
}