2
2
3
3
namespace Log1x \EjectBlocks \Console \Commands ;
4
4
5
+ use Exception ;
5
6
use WP_Filesystem_Base ;
6
7
use Illuminate \Support \Str ;
7
8
use Illuminate \Filesystem \Filesystem ;
8
9
9
10
use function Roots \asset ;
11
+ use function Roots \public_path ;
10
12
11
13
class EjectBlocksCommand extends Command
12
14
{
@@ -62,9 +64,12 @@ class EjectBlocksCommand extends Command
62
64
/**
63
65
* The plugin assets location.
64
66
*
65
- * @var string
67
+ * @var string|array
66
68
*/
67
- protected $ js = 'scripts/ ' ;
69
+ protected $ js = [
70
+ 'scripts ' ,
71
+ 'js ' ,
72
+ ];
68
73
69
74
/**
70
75
* The plugin assets.
@@ -113,7 +118,7 @@ public function handle()
113
118
114
119
$ this ->task ('Setting up plugin ' , function () {
115
120
$ this ->label = $ this ->anticipate ('Plugin name? ' , [], $ this ->label );
116
- $ this ->js = Str::finish ($ this ->anticipate ('Plugin assets location? ' , [], $ this ->js ), '/ ' );
121
+ $ this ->js = Str::finish ($ this ->anticipate ('Plugin assets location? ' , [], $ this ->findAssets () ), '/ ' );
117
122
$ this ->assets = explode (
118
123
', ' ,
119
124
$ this ->anticipate ('Plugin assets? ' , [], implode (', ' , $ this ->assets ))
@@ -122,7 +127,7 @@ public function handle()
122
127
123
128
$ this ->task ('Verifying permissions ' , function () {
124
129
if (! $ this ->verifyPermissions ()) {
125
- return $ this -> error ('Unable to write to ' . $ this ->plugins );
130
+ throw new Exception ('Unable to write to ' . $ this ->plugins );
126
131
}
127
132
});
128
133
@@ -132,24 +137,26 @@ public function handle()
132
137
133
138
$ this ->task ('Verifying editor assets ' , function () {
134
139
if (! $ this ->verifyAssets ()) {
135
- return $ this -> error ('Editor assets missing: ' . $ this ->assets ->implode (', ' ));
140
+ throw new Exception ('Editor assets missing: ' . $ this ->assets ->implode (', ' ));
136
141
}
137
142
});
138
143
139
144
$ this ->task ('Verifying editor manifest ' , function () {
140
145
if (! $ this ->verifyManifest ()) {
141
- return $ this -> error ('Asset manifest missing: ' . $ this ->manifest );
146
+ throw new Exception ('Asset manifest missing: ' . $ this ->manifest );
142
147
}
143
148
});
144
149
145
150
$ this ->task ('Checking for webpack manifest ' , function () {
146
151
if (! $ this ->verifyManifestJs ()) {
147
- return ;
152
+ return false ;
148
153
}
149
154
});
150
155
151
156
$ this ->task ('Creating plugin directory ' , function () {
152
- return $ this ->createDirectory ();
157
+ if (! $ this ->createDirectory ()) {
158
+ throw new Exception ('The operation has been canceled. ' );
159
+ }
153
160
});
154
161
155
162
$ this ->task ('Generating plugin loader ' , function () {
@@ -174,10 +181,24 @@ public function handle()
174
181
return $ this ->exec ('wp plugin activate ' . $ this ->label );
175
182
}, 'using WP-CLI... ' );
176
183
177
-
178
184
return $ this ->summary ();
179
185
}
180
186
187
+ /**
188
+ * Return existing asset directories.
189
+ *
190
+ * @return void
191
+ */
192
+ public function findAssets ()
193
+ {
194
+ return collect ($ this ->js )
195
+ ->map (function ($ directory ) {
196
+ if ($ this ->files ->exists (public_path ($ directory ))) {
197
+ return Str::finish ($ directory , '/ ' );
198
+ }
199
+ })->implode ('' ) ?? 'scripts/ ' ;
200
+ }
201
+
181
202
/**
182
203
* Check that we have permission to create a directory.
183
204
*
@@ -248,7 +269,7 @@ public function createDirectory()
248
269
$ this ->files ->isDirectory ($ this ->path ) &&
249
270
! $ this ->confirm ('A plugin containing block assets already exists. Do you wish to overwrite it? ' )
250
271
) {
251
- return false ;
272
+ return false ;
252
273
}
253
274
254
275
$ this ->files ->deleteDirectory ($ this ->path );
0 commit comments