Skip to content

Commit 7e3211a

Browse files
Merge pull request #47 from sheafui/feat/init
Feat/init
2 parents c671609 + 44a66af commit 7e3211a

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

src/Commands/SheafInitCommand.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Sheaf\Cli\Services\PackageInitializationService;
66
use Sheaf\Cli\Support\InitializationConfig;
77
use Illuminate\Console\Command;
8+
use Sheaf\Cli\Services\ComponentHttpClient;
89

910
use function Laravel\Prompts\confirm;
1011
use function Laravel\Prompts\text;
@@ -64,6 +65,7 @@ public function handle()
6465
if ($result) {
6566
$this->displaySuccess($configuration);
6667
$this->displayNextStep($configuration);
68+
$this->subscribeToNewsletter();
6769
return Command::SUCCESS;
6870
}
6971

@@ -206,6 +208,31 @@ protected function validateCssFileName(?string $input): ?string
206208
return null;
207209
}
208210

211+
/**
212+
* Ask user to subscribe to the news letter
213+
*/
214+
protected function subscribeToNewsletter()
215+
{
216+
217+
$email = text(
218+
label: 'Get notified when we release new components',
219+
placeholder: 'your.email@example.com',
220+
hint: 'No spam, just useful updates',
221+
validate: fn($input) => $input && !filter_var($input, FILTER_VALIDATE_EMAIL)
222+
? 'Please enter a valid email address'
223+
: null
224+
);
225+
226+
if (empty($email)) {
227+
return;
228+
}
229+
try {
230+
new ComponentHttpClient()->subscribeUserToNewsletter($email);
231+
$this->info("✓ Thanks for subscribing! We'll keep you updated with new components and features.");
232+
} catch (\Throwable $th) {
233+
$this->warn("We couldn't subscribe your email right now, but you can always subscribe later at https://sheafui.dev");
234+
}
235+
}
209236

210237
/**
211238
* Display the Sheaf package banner

src/Services/ComponentHttpClient.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public function __construct()
1717
$this->token = SheafConfig::getUserToken();
1818
}
1919

20-
public function fetchComponentFilesPath(string $name) {
20+
public function fetchComponentFilesPath(string $name)
21+
{
2122
$url = "{$this->baseUrl}/api/cli/components/$name/files";
2223

2324
$response = Http::asJson()->get($url);
@@ -33,7 +34,6 @@ public function fetchComponentFilesPath(string $name) {
3334
'success' => true,
3435
'data' => $response->collect()
3536
];
36-
3737
}
3838
public function fetchResources(string $componentName)
3939
{
@@ -59,4 +59,18 @@ public function fetchResources(string $componentName)
5959
];
6060
}
6161

62+
public function subscribeUserToNewsletter(string $email)
63+
{
64+
try {
65+
$url = "{$this->baseUrl}/api/cli/newsletter/subscribe?email={$email}";
66+
67+
$result = Http::asJson()->get($url);
68+
69+
if (!$result['success']) {
70+
throw new Exception("Failed to subscribe to the news letter");
71+
}
72+
} catch (\Throwable $th) {
73+
throw new Exception("Failed to subscribe to the news letter");
74+
}
75+
}
6276
}

tests/Feature/Commands/InitCommandTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
$command->expectsQuestion("The $mainJsFile is not exists, do you want to create it?", true);
4040
}
4141

42+
$command->expectsQuestion("Get notified when we release new components", null);
43+
4244
$command->assertExitCode(0);
4345

4446
$command->run();

0 commit comments

Comments
 (0)