Skip to content

Commit 201df47

Browse files
duncanmccleanjackmcdadejasonvarga
authored
UX improvements (#81)
Co-authored-by: Jack McDade <jack@jackmcdade.com> Co-authored-by: Jason Varga <jason@pixelfear.com>
1 parent 304a0b2 commit 201df47

2 files changed

Lines changed: 91 additions & 98 deletions

File tree

src/Concerns/RunsCommands.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,33 @@ trait RunsCommands
99
/**
1010
* Run the given command.
1111
*
12-
* @param string $command
13-
* @param bool $disableOutput
12+
* @param string $command
13+
* @param string|null $workingPath
14+
* @param bool $disableOutput
1415
* @return Process
1516
*/
16-
protected function runCommand($command, $disableOutput = false)
17+
protected function runCommand(string $command, string $workingPath = null, bool $disableOutput = false)
1718
{
18-
return $this->runCommands([$command], $disableOutput);
19+
return $this->runCommands([$command], $workingPath, $disableOutput);
1920
}
2021

2122
/**
2223
* Run the given commands.
2324
*
24-
* @param array $commands
25-
* @param bool $disableOutput
25+
* @param array $commands
26+
* @param string|null $workingPath
27+
* @param bool $disableOutput
2628
* @return Process
2729
*/
28-
protected function runCommands($commands, $disableOutput = false)
30+
protected function runCommands(array $commands, string $workingPath = null, bool $disableOutput = false)
2931
{
3032
if (! $this->output->isDecorated()) {
3133
$commands = array_map(function ($value) {
32-
if (substr($value, 0, 5) === 'chmod') {
34+
if (str_starts_with($value, 'chmod')) {
35+
return $value;
36+
}
37+
38+
if (str_starts_with($value, 'git')) {
3339
return $value;
3440
}
3541

@@ -39,29 +45,33 @@ protected function runCommands($commands, $disableOutput = false)
3945

4046
if ($this->input->getOption('quiet')) {
4147
$commands = array_map(function ($value) {
42-
if (substr($value, 0, 5) === 'chmod') {
48+
if (str_starts_with($value, 'chmod')) {
49+
return $value;
50+
}
51+
52+
if (str_starts_with($value, 'git')) {
4353
return $value;
4454
}
4555

4656
return $value.' --quiet';
4757
}, $commands);
4858
}
4959

50-
$process = Process::fromShellCommandline(implode(' && ', $commands), null, null, null, null);
60+
$process = Process::fromShellCommandline(implode(' && ', $commands), $workingPath);
5161

5262
if ('\\' !== DIRECTORY_SEPARATOR && file_exists('/dev/tty') && is_readable('/dev/tty')) {
5363
try {
5464
$process->setTty(true);
5565
} catch (RuntimeException $e) {
56-
$this->output->writeln('Warning: '.$e->getMessage());
66+
$this->output->writeln(' <bg=yellow;fg=black> WARN </> '.$e->getMessage().PHP_EOL);
5767
}
5868
}
5969

6070
if ($disableOutput) {
6171
$process->disableOutput()->run();
6272
} else {
6373
$process->run(function ($type, $line) {
64-
$this->output->write($line);
74+
$this->output->write(' '.$line);
6575
});
6676
}
6777

src/NewCommand.php

Lines changed: 69 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Symfony\Component\Console\Style\SymfonyStyle;
2323

2424
use function Laravel\Prompts\confirm;
25+
use function Laravel\Prompts\error;
26+
use function Laravel\Prompts\intro;
2527
use function Laravel\Prompts\multiselect;
2628
use function Laravel\Prompts\select;
2729
use function Laravel\Prompts\suggest;
@@ -36,8 +38,12 @@ class NewCommand extends Command
3638
const GITHUB_LATEST_RELEASE_ENDPOINT = 'https://api.github.com/repos/statamic/cli/releases/latest';
3739
const STATAMIC_API_URL = 'https://statamic.com/api/v1/';
3840

41+
/** @var InputInterface */
3942
public $input;
43+
44+
/** @var OutputInterface */
4045
public $output;
46+
4147
public $relativePath;
4248
public $absolutePath;
4349
public $name;
@@ -53,7 +59,6 @@ class NewCommand extends Command
5359
public $baseInstallSuccessful;
5460
public $shouldUpdateCliToVersion = false;
5561
public $makeUser = false;
56-
public $spreadJoy = false;
5762

5863
/**
5964
* Configure the command options.
@@ -111,22 +116,27 @@ protected function interact(InputInterface $input, OutputInterface $output)
111116
*/
112117
protected function execute(InputInterface $input, OutputInterface $output)
113118
{
114-
$this
115-
->processArguments()
116-
->validateArguments()
117-
->askForRepo()
118-
->validateStarterKitLicense()
119-
->askToInstallAddons()
120-
->askToMakeSuperUser()
121-
->askToSpreadJoy()
122-
->readySetGo()
123-
->installBaseProject()
124-
->installStarterKit()
125-
->makeSuperUser()
126-
->installAddons()
127-
->notifyIfOldCliVersion()
128-
->showSuccessMessage()
129-
->showPostInstallInstructions();
119+
try {
120+
$this
121+
->processArguments()
122+
->validateArguments()
123+
->askForRepo()
124+
->validateStarterKitLicense()
125+
->askToInstallAddons()
126+
->askToMakeSuperUser()
127+
->askToSpreadJoy()
128+
->installBaseProject()
129+
->installStarterKit()
130+
->makeSuperUser()
131+
->installAddons()
132+
->notifyIfOldCliVersion()
133+
->showSuccessMessage()
134+
->showPostInstallInstructions();
135+
} catch (RuntimeException $e) {
136+
$this->showError($e->getMessage());
137+
138+
return 1;
139+
}
130140

131141
return 0;
132142
}
@@ -209,9 +219,9 @@ protected function notifyIfOldCliVersion()
209219
}
210220

211221
$this->output->write(PHP_EOL);
212-
$this->output->write("<comment>This is an old version of the Statamic CLI Tool, please upgrade to {$this->shouldUpdateCliToVersion}!</comment>".PHP_EOL);
213-
$this->output->write('<comment>If you have a global composer installation, you may upgrade by running the following command:</comment>'.PHP_EOL);
214-
$this->output->write('<comment>composer global update statamic/cli</comment>'.PHP_EOL);
222+
$this->output->write(" <comment>This is an old version of the Statamic CLI Tool, please upgrade to {$this->shouldUpdateCliToVersion}!</comment>".PHP_EOL);
223+
$this->output->write(' <comment>If you have a global composer installation, you may upgrade by running the following command:</comment>'.PHP_EOL);
224+
$this->output->write(' <comment>composer global update statamic/cli</comment>'.PHP_EOL);
215225

216226
return $this;
217227
}
@@ -294,15 +304,9 @@ protected function validateArguments()
294304
*/
295305
protected function showStatamicTitleArt()
296306
{
297-
$this->output->write(PHP_EOL."<fg=#FF269E> $$\ $$\ $$\
298-
$$ | $$ | \__|
299-
$$$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\$$$$\ $$\ $$$$$$$\
300-
$$ _____|\_$$ _| \____$$\\_$$ _| \____$$\ $$ _$$ _$$\ $$ |$$ _____|
301-
\$$$$$$\ $$ | $$$$$$$ | $$ | $$$$$$$ |$$ / $$ / $$ |$$ |$$ /
302-
\____$$\ $$ |$$\ $$ __$$ | $$ |$$\ $$ __$$ |$$ | $$ | $$ |$$ |$$ |
303-
$$$$$$$ | \\$$$$ |\\$$$$$$$ | \\$$$$ |\\$$$$$$$ |$$ | $$ | $$ |$$ |\\$$$$$$$\
304-
\_______/ \____/ \_______| \____/ \_______|\__| \__| \__|\__| \_______|
305-
</>".PHP_EOL);
307+
$this->output->write(PHP_EOL.'<fg=#D4FF4C>
308+
█▀ ▀█▀ ▄▀█ ▀█▀ ▄▀█ █▀▄▀█ █ █▀▀
309+
▄█ ░█░ █▀█ ░█░ █▀█ █░▀░█ █ █▄▄</>'.PHP_EOL.PHP_EOL);
306310

307311
return $this;
308312
}
@@ -416,7 +420,7 @@ protected function validateStarterKitLicense()
416420
*/
417421
protected function confirmUnlistedKit()
418422
{
419-
if (! confirm('Starter kit not found on Statamic Marketplace! Install unlisted starter kit?')) {
423+
if (! confirm('Starter kit not found on Statamic Marketplace. Install unlisted starter kit?')) {
420424
return $this->exitInstallation();
421425
}
422426

@@ -448,34 +452,6 @@ protected function confirmSingleSiteLicense()
448452
return $this;
449453
}
450454

451-
/**
452-
* Final confirmation
453-
*
454-
* @return $this
455-
*/
456-
protected function readySetGo()
457-
{
458-
if (! $this->input->isInteractive()) {
459-
return $this;
460-
}
461-
462-
if (! confirm('Ready?', yes: "Yes, let's do this!", no: 'No, shut it down.')) {
463-
return $this->exitInstallation();
464-
}
465-
466-
if ($this->spreadJoy) {
467-
if (PHP_OS_FAMILY == 'Darwin') {
468-
exec('open https://github.com/statamic/cms');
469-
} elseif (PHP_OS_FAMILY == 'Windows') {
470-
exec('start https://github.com/statamic/cms');
471-
} elseif (PHP_OS_FAMILY == 'Linux') {
472-
exec('xdg-open https://github.com/statamic/cms');
473-
}
474-
}
475-
476-
return $this;
477-
}
478-
479455
/**
480456
* Install base project.
481457
*
@@ -627,7 +603,8 @@ protected function askToMakeSuperUser()
627603

628604
$this->makeUser = confirm('Create a super user?', false);
629605

630-
$this->output->write($this->makeUser
606+
$this->output->write(
607+
$this->makeUser
631608
? " Great. You'll be prompted for details after installation."
632609
: ' No problem. You can create one later with <comment>php please make:user</comment>.'
633610
);
@@ -648,6 +625,9 @@ protected function makeSuperUser()
648625
return $this;
649626
}
650627

628+
$this->output->write(PHP_EOL.PHP_EOL);
629+
intro("Let's create your super user account.");
630+
651631
// Since Windows cannot TTY, we'll capture their input here and make a user.
652632
if (PHP_OS_FAMILY === 'Windows') {
653633
return $this->makeSuperUserInWindows();
@@ -758,13 +738,11 @@ protected function validatePassword($password)
758738
*/
759739
protected function showSuccessMessage()
760740
{
761-
$this->output->writeln(PHP_EOL."<info>[✔] Statamic has been successfully installed into the <comment>{$this->relativePath}</comment> directory.</info>");
762-
763-
if (! $this->spreadJoy) {
764-
$this->output->writeln('Spread some joy and star our GitHub repo! https://github.com/statamic/cms');
765-
}
766-
767-
$this->output->writeln('Build something rad!');
741+
$this->output->writeln(PHP_EOL.' <info>[✔] Statamic was installed successfully!</info>'.PHP_EOL);
742+
$this->output->writeln(' You may now enter your project directory using <comment>cd '.$this->relativePath.'</comment>,'.PHP_EOL);
743+
$this->output->writeln(' The documentation is always available at <info>statamic.dev</info> and you can ');
744+
$this->output->writeLn(' join the community on Discord at <info>statamic.com/discord</info> anytime.'.PHP_EOL);
745+
$this->output->writeLn(' Now go — it\'s time to create something wonderful! 🌟'.PHP_EOL);
768746

769747
return $this;
770748
}
@@ -801,14 +779,20 @@ protected function askToSpreadJoy()
801779
}
802780

803781
$response = select('Would you like to spread the joy of Statamic by starring the repo?', [
804-
$yes = "Absolutely. I'll star it while you finish installing.",
782+
$yes = "Absolutely",
805783
$no = 'Maybe later',
806784
], $no);
807785

808-
if ($this->spreadJoy = $response === $yes) {
809-
$this->output->write(' Awesome. The browser will open when the installation begins.');
786+
if ($response === $yes) {
787+
if (PHP_OS_FAMILY == 'Darwin') {
788+
exec('open https://github.com/statamic/cms');
789+
} elseif (PHP_OS_FAMILY == 'Windows') {
790+
exec('start https://github.com/statamic/cms');
791+
} elseif (PHP_OS_FAMILY == 'Linux') {
792+
exec('xdg-open https://github.com/statamic/cms');
793+
}
810794
} else {
811-
$this->output->write(' You can star the GitHub repo at any time if you change your mind.');
795+
$this->output->write(' No problem. You can do it at <info>github.com/statamic/cms</info> anytime.');
812796
}
813797

814798
$this->output->write(PHP_EOL.PHP_EOL);
@@ -952,22 +936,6 @@ protected function getStarterKitLicense()
952936
return text('Please enter your license key', required: true);
953937
}
954938

955-
/**
956-
* Exit installation.
957-
*
958-
* @return \stdClass
959-
*/
960-
protected function exitInstallation()
961-
{
962-
return new class
963-
{
964-
public function __call($method, $args)
965-
{
966-
return $this;
967-
}
968-
};
969-
}
970-
971939
private function searchStarterKits($value)
972940
{
973941
$kits = $this->getStarterKits();
@@ -1008,4 +976,19 @@ private function normalizeStarterKitSelection($kit)
1008976

1009977
return array_search($kit, $this->getStarterKits());
1010978
}
979+
980+
private function showError(string $message): void
981+
{
982+
$whitespace = '';
983+
984+
for ($i = 0; $i < strlen($message); $i++) {
985+
$whitespace .= ' ';
986+
}
987+
988+
$this->output->write(PHP_EOL);
989+
$this->output->write(" <bg=red> {$whitespace} </>".PHP_EOL);
990+
$this->output->write(" <bg=red> {$message} </>".PHP_EOL);
991+
$this->output->write(" <bg=red> {$whitespace} </>".PHP_EOL);
992+
$this->output->write(PHP_EOL);
993+
}
1011994
}

0 commit comments

Comments
 (0)