-
Notifications
You must be signed in to change notification settings - Fork 0
/
StarterKitPostInstall.php
102 lines (85 loc) · 2.69 KB
/
StarterKitPostInstall.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
<?php
use Laravel\Prompts\Prompt;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\error;
use function Laravel\Prompts\spin;
/*
* This file contains the post-install hooks for the Tidal Starter Kit.
*
* The following methods are from Peak, and are used with permission:
*
* - applyInteractivity
* - installNodeDependencies
* - run
* - withSpinner
* - withoutSpinner
*
* Check out Peak at https://github.com/studio1902/statamic-peak
*
* Thanks!
*/
class StarterKitPostInstall
{
protected bool $interactive = true;
public function handle($console)
{
$this->applyInteractivity($console);
$this->installNodeDependencies();
}
protected function applyInteractivity($console): void
{
$this->interactive = ! $console->option('no-interaction');
Prompt::interactive($this->interactive);
}
protected function installNodeDependencies(): void
{
if (! confirm(label: 'Do you want to install npm dependencies?', default: true)) {
return;
}
$this->run(
command: 'npm i',
processingMessage: 'Installing npm dependencies...',
successMessage: 'npm dependencies installed.',
);
}
protected function run(string $command, string $processingMessage = '', string $successMessage = '', string $errorMessage = null, bool $tty = false, bool $spinner = true, int $timeout = 120): bool
{
$process = new Process(explode(' ', $command));
$process->setTimeout($timeout);
if ($tty) {
$process->setTty(true);
}
try {
$spinner ?
$this->withSpinner(
fn () => $process->mustRun(),
$processingMessage,
$successMessage
) :
$this->withoutSpinner(
fn () => $process->mustRun(),
$successMessage
);
return true;
} catch (ProcessFailedException $exception) {
error($errorMessage ?? $exception->getMessage());
return false;
}
}
protected function withSpinner(callable $callback, string $processingMessage = '', string $successMessage = ''): void
{
spin($callback, $processingMessage);
if ($successMessage) {
info("[✓] $successMessage");
}
}
protected function withoutSpinner(callable $callback, string $successMessage = ''): void
{
$callback();
if ($successMessage) {
info("[✓] $successMessage");
}
}
}