Skip to content
This repository was archived by the owner on Feb 17, 2021. It is now read-only.

Commit d8bb1a9

Browse files
committed
Clean up task()
Add a clearLine() method Intelligently check for existence of assets in `scripts/` or `js/` and set the default accordingly Remove error() method override as its no longer needed Add exceptions to properly bail
1 parent c1eebdc commit d8bb1a9

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

src/Console/Commands/Command.php

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,6 @@
77

88
class Command extends CommandBase
99
{
10-
/**
11-
* Write a string as error output.
12-
*
13-
* @param string $string
14-
* @param int|string|null $verbosity
15-
* @return false
16-
*/
17-
public function error($string, $verbosity = null)
18-
{
19-
$this->line('');
20-
$this->line($string, 'error', $verbosity);
21-
$this->line('');
22-
return false;
23-
}
24-
2510
/**
2611
* Execute a process and return the status to console.
2712
*
@@ -62,26 +47,36 @@ protected function task($title, $task = null, $status = '...')
6247
$this->output->write("$title: <comment>{$status}</comment>");
6348

6449
try {
65-
$status = $task() === false ? false : true;
50+
$status = $task() !== false;
6651
} catch (\Exception $e) {
67-
$status = false;
68-
}
52+
$this->clearLine()->line(
53+
$title . ': ' . ($status ? '<info>✔</info>' : '<red>x</red>')
54+
);
6955

70-
if ($this->output->isDecorated()) {
71-
$this->output->write("\x0D");
72-
$this->output->write("\x1B[2K");
73-
} else {
74-
$this->output->writeln('');
56+
throw $e;
7557
}
7658

77-
$this->output->writeln(
59+
$this->clearLine()->line(
7860
$title . ': ' . ($status ? '<info>✔</info>' : '<red>x</red>')
7961
);
62+
}
8063

81-
if (! $status) {
82-
exit;
64+
/**
65+
* Clear the current line in console.
66+
*
67+
* @return mixed
68+
*/
69+
public function clearLine()
70+
{
71+
if (! $this->output->isDecorated()) {
72+
$this->output->writeln('');
73+
74+
return $this;
8375
}
8476

85-
return $status;
77+
$this->output->write("\x0D");
78+
$this->output->write("\x1B[2K");
79+
80+
return $this;
8681
}
8782
}

src/Console/Commands/EjectBlocksCommand.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Log1x\EjectBlocks\Console\Commands;
44

5+
use Exception;
56
use WP_Filesystem_Base;
67
use Illuminate\Support\Str;
78
use Illuminate\Filesystem\Filesystem;
89

910
use function Roots\asset;
11+
use function Roots\public_path;
1012

1113
class EjectBlocksCommand extends Command
1214
{
@@ -62,9 +64,12 @@ class EjectBlocksCommand extends Command
6264
/**
6365
* The plugin assets location.
6466
*
65-
* @var string
67+
* @var string|array
6668
*/
67-
protected $js = 'scripts/';
69+
protected $js = [
70+
'scripts',
71+
'js',
72+
];
6873

6974
/**
7075
* The plugin assets.
@@ -113,7 +118,7 @@ public function handle()
113118

114119
$this->task('Setting up plugin', function () {
115120
$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()), '/');
117122
$this->assets = explode(
118123
', ',
119124
$this->anticipate('Plugin assets?', [], implode(', ', $this->assets))
@@ -122,7 +127,7 @@ public function handle()
122127

123128
$this->task('Verifying permissions', function () {
124129
if (! $this->verifyPermissions()) {
125-
return $this->error('Unable to write to ' . $this->plugins);
130+
throw new Exception('Unable to write to ' . $this->plugins);
126131
}
127132
});
128133

@@ -132,24 +137,26 @@ public function handle()
132137

133138
$this->task('Verifying editor assets', function () {
134139
if (! $this->verifyAssets()) {
135-
return $this->error('Editor assets missing: ' . $this->assets->implode(', '));
140+
throw new Exception('Editor assets missing: ' . $this->assets->implode(', '));
136141
}
137142
});
138143

139144
$this->task('Verifying editor manifest', function () {
140145
if (! $this->verifyManifest()) {
141-
return $this->error('Asset manifest missing: ' . $this->manifest);
146+
throw new Exception('Asset manifest missing: ' . $this->manifest);
142147
}
143148
});
144149

145150
$this->task('Checking for webpack manifest', function () {
146151
if (! $this->verifyManifestJs()) {
147-
return;
152+
return false;
148153
}
149154
});
150155

151156
$this->task('Creating plugin directory', function () {
152-
return $this->createDirectory();
157+
if (! $this->createDirectory()) {
158+
throw new Exception('The operation has been canceled.');
159+
}
153160
});
154161

155162
$this->task('Generating plugin loader', function () {
@@ -174,10 +181,24 @@ public function handle()
174181
return $this->exec('wp plugin activate ' . $this->label);
175182
}, 'using WP-CLI...');
176183

177-
178184
return $this->summary();
179185
}
180186

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+
181202
/**
182203
* Check that we have permission to create a directory.
183204
*
@@ -248,7 +269,7 @@ public function createDirectory()
248269
$this->files->isDirectory($this->path) &&
249270
! $this->confirm('A plugin containing block assets already exists. Do you wish to overwrite it?')
250271
) {
251-
return false;
272+
return false;
252273
}
253274

254275
$this->files->deleteDirectory($this->path);

0 commit comments

Comments
 (0)