3
3
namespace Mary \Console \Commands ;
4
4
5
5
use Illuminate \Console \Command ;
6
+ use Illuminate \Support \Facades \File ;
6
7
use Illuminate \Support \Facades \Process ;
8
+ use Illuminate \Support \Str ;
9
+ use function Laravel \Prompts \select ;
7
10
8
11
class MaryInstallCommand extends Command
9
12
{
@@ -13,34 +16,189 @@ class MaryInstallCommand extends Command
13
16
14
17
public function handle ()
15
18
{
16
- $ this ->info ("\n\n 🔨 Installing Mary... \n\n " );
19
+ $ this ->info ("🔨 Mary installer " );
17
20
18
- $ directories = Process::tty ()->run ('mkdir -p app/Livewire && mkdir -p resources/views/components/layouts ' );
19
- $ this ->info ($ directories ->output ());
21
+ $ this ->warn ('
22
+ It will set up:
23
+ - Livewire
24
+ - Tailwind + daisyUI
25
+ - Default layout
26
+ - Welcome component
27
+ - Route to Welcome ' );
20
28
21
- $ livewire = Process::tty ()->run ('composer require livewire/livewire ' );
22
- $ this ->info ($ livewire ->output ());
29
+ /**
30
+ * Install Volt ?
31
+ */
32
+ $ shouldInstallVolt = $ this ->askForVolt ();
23
33
24
- $ js = Process::tty ()->run ('yarn add -D tailwindcss daisyui@latest postcss autoprefixer && npx tailwindcss init -p ' );
25
- $ this ->info ($ js ->output ());
34
+ /**
35
+ * Yarn or Npm ?
36
+ */
37
+ $ packageManagerCommand = $ this ->askForPackageInstaller ();
26
38
27
- $ layout = Process::tty ()->run ('cp ' .__DIR__ .'/../../../stubs/app.blade.php resources/views/components/layouts/ ' );
28
- $ this ->info ($ layout ->output ());
39
+ /**
40
+ * Check for existing STARTER KIT packages
41
+ */
42
+ $ composerJson = File::get (base_path () . "/composer.json " );
43
+ $ targets = ['breeze ' , 'jetstream ' , 'genesis ' ];
44
+ $ this ->checkForExistingPackages ($ composerJson , $ targets );
29
45
30
- $ css = Process::tty ()->run ('cp ' .__DIR__ .'/../../../stubs/app.css resources/css/ ' );
31
- $ this ->info ($ css ->output ());
46
+ /**
47
+ * Check for existing JS packages
48
+ */
49
+ $ packageJson = File::get (base_path () . "/package.json " );
50
+ $ targets = ['tailwindcss ' , 'daisyui ' ];
51
+ $ this ->checkForExistingPackages ($ packageJson , $ targets );
32
52
33
- $ tailwindConfig = Process::tty ()->run ('cp ' .__DIR__ .'/../../../stubs/tailwind.config.js . ' );
34
- $ this ->info ($ tailwindConfig ->output ());
53
+ /**
54
+ * Check for stubs
55
+ */
56
+ $ this ->checkForStubs ();
35
57
36
- $ welcome = Process::tty ()->run ('cp ' .__DIR__ .'/../../../stubs/Welcome.php app/Livewire/ ' );
37
- $ this ->info ($ welcome ->output ());
58
+ /**
59
+ * Install Livewire
60
+ */
61
+ $ this ->info ("\nInstalling Livewire... \n" );
38
62
39
- $ route = Process::tty ()->run ('cp ' .__DIR__ .'/../../../stubs/web.php routes/ ' );
40
- $ this ->info ($ route ->output ());
63
+ Process::run ('composer require livewire/livewire ' , function (string $ type , string $ output ) {
64
+ echo $ output ;
65
+ })->throw ();
41
66
42
- $ this -> info ( ' 🌟 Done! ' );
43
- $ this ->info ("\n\n ==> Run `yarn dev` \n \n" );
67
+ if ( $ shouldInstallVolt == ' Yes ' ) {
68
+ $ this ->info ("\n Installing Livewire Volt... \n" );
44
69
70
+ Process::run ('composer require livewire/volt ' , function (string $ type , string $ output ) {
71
+ echo $ output ;
72
+ })->throw ();
73
+ }
74
+
75
+ /**
76
+ * Install daisyUI + Tailwind
77
+ */
78
+ $ this ->info ("\nInstalling daisyUI + Tailwind... \n" );
79
+
80
+ Process::run ("$ packageManagerCommand tailwindcss daisyui@latest postcss autoprefixer && npx tailwindcss init -p " , function (string $ type , string $ output ) {
81
+ echo $ output ;
82
+ })->throw ();
83
+
84
+ /**
85
+ * Copy all stubs
86
+ */
87
+ $ this ->copyStubs ();
88
+
89
+ $ this ->info ("\n🌟 Done! Run `yarn dev or npm run dev` \n" );
90
+ }
91
+
92
+ /**
93
+ * Check for existing packages
94
+ */
95
+ public function checkForExistingPackages (string $ content , array $ targets ): void
96
+ {
97
+ collect ($ targets )->each (function (string $ target ) use ($ content ) {
98
+ if (Str::of ($ content )->contains ($ target )) {
99
+ $ this ->error ("Automatic install works only for brand-new Laravel projects. " );
100
+ $ this ->warn ("Detected: " . $ target );
101
+
102
+ exit ;
103
+ }
104
+ });
105
+ }
106
+
107
+ /**
108
+ * Check for existing stubs
109
+ */
110
+ public function checkForStubs (): void
111
+ {
112
+ collect ([
113
+ [
114
+ 'path ' => 'resources/views/components/layouts ' ,
115
+ 'name ' => 'app.blade.php '
116
+ ],
117
+ [
118
+ 'path ' => 'app/Livewire/ ' ,
119
+ 'name ' => 'Welcome.php ' ,
120
+ ],
121
+ [
122
+ 'path ' => '' ,
123
+ 'name ' => 'tailwind.config.js ' ,
124
+ ]
125
+ ])->each (function (array $ item ) {
126
+ $ file = base_path () . '/ ' . $ item ['path ' ] . $ item ['name ' ];
127
+
128
+ if (File::exists ($ file )) {
129
+ $ this ->error ("Automatic install works only for brand-new Laravel projects. " );
130
+ $ this ->warn ('Detected: ' . $ file );
131
+
132
+ exit ;
133
+ }
134
+ });
135
+ }
136
+
137
+ public function copyStubs (): void
138
+ {
139
+ $ this ->info ("Copying stubs... \n" );
140
+
141
+ Process::run ('mkdir -p app/Livewire && mkdir -p resources/views/components/layouts ' , function (string $ type , string $ output ) {
142
+ echo $ output ;
143
+ })->throw ();
144
+
145
+ Process::run ('cp ' . __DIR__ . '/../../../stubs/app.blade.php resources/views/components/layouts/ ' , function (string $ type , string $ output ) {
146
+ echo $ output ;
147
+ })->throw ();
148
+
149
+ Process::run ('cp ' . __DIR__ . '/../../../stubs/app.css resources/css/ ' , function (string $ type , string $ output ) {
150
+ echo $ output ;
151
+ })->throw ();
152
+
153
+ Process::run ('cp ' . __DIR__ . '/../../../stubs/tailwind.config.js . ' , function (string $ type , string $ output ) {
154
+ echo $ output ;
155
+ })->throw ();
156
+
157
+ Process::run ('cp ' . __DIR__ . '/../../../stubs/Welcome.php app/Livewire/ ' , function (string $ type , string $ output ) {
158
+ echo $ output ;
159
+ })->throw ();
160
+
161
+ Process::run ('cp ' . __DIR__ . '/../../../stubs/web.php routes/ ' , function (string $ type , string $ output ) {
162
+ echo $ output ;
163
+ })->throw ();
164
+ }
165
+
166
+ public function askForPackageInstaller (): string
167
+ {
168
+ $ yarn = Process::run ('which yarn ' )->output ();
169
+ $ npm = Process::run ('which npm ' )->output ();
170
+
171
+ $ options = [];
172
+
173
+ if (Str::of ($ yarn )->isNotEmpty ()) {
174
+ $ options = array_merge ($ options , ['yarn add -D ' => 'yarn ' ]);
175
+ }
176
+
177
+ if (Str::of ($ npm )->isNotEmpty ()) {
178
+ $ options = array_merge ($ options , ['npm install --dev ' => 'npm ' ,]);
179
+ }
180
+
181
+ if (count ($ options ) == 0 ) {
182
+ $ this ->error ("You need yarn or npm installed. " );
183
+
184
+ exit ;
185
+ }
186
+
187
+ return select (
188
+ label: 'Install with ... ' ,
189
+ options: $ options
190
+ );
191
+ }
192
+
193
+ /**
194
+ * Also install Volt?
195
+ */
196
+ public function askForVolt (): string
197
+ {
198
+ return select (
199
+ 'Also install `livewire/volt` ? ' ,
200
+ ['Yes ' , 'No ' ],
201
+ hint: 'No matter what is your choice, it always installs `livewire/livewire` '
202
+ );
45
203
}
46
204
}
0 commit comments