Skip to content

Commit b154a63

Browse files
authored
Merge pull request #46 from RonasIT/3-86c34dha8-clerk-step
3 86c34dha8 clerk step
2 parents 93a0694 + 471b047 commit b154a63

9 files changed

+527
-25
lines changed

src/Commands/InitCommand.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Support\Str;
1313
use RonasIT\ProjectInitializator\Enums\AuthTypeEnum;
1414
use RonasIT\ProjectInitializator\Enums\RoleEnum;
15+
use RonasIT\ProjectInitializator\Enums\AppTypeEnum;
1516
use Winter\LaravelConfigWriter\ArrayFile;
1617

1718
class InitCommand extends Command implements Isolatable
@@ -69,6 +70,8 @@ class InitCommand extends Command implements Isolatable
6970

7071
protected string $appName;
7172

73+
protected AppTypeEnum $appType;
74+
7275
public function handle(): void
7376
{
7477
$this->prepareAppName();
@@ -80,7 +83,7 @@ public function handle(): void
8083
field: 'email of code owner / team lead',
8184
rules: 'required|email',
8285
);
83-
86+
8487
$this->appUrl = $this->ask('Please enter an application URL', "https://api.dev.{$kebabName}.com");
8588

8689
$envFile = (file_exists('.env')) ? '.env' : '.env.example';
@@ -96,6 +99,14 @@ public function handle(): void
9699

97100
$this->info('Project initialized successfully!');
98101

102+
$this->appType = AppTypeEnum::from(
103+
$this->choice(
104+
question: 'What type of application will your API serve?',
105+
choices: AppTypeEnum::values(),
106+
default: AppTypeEnum::Multiplatform->value,
107+
),
108+
);
109+
99110
$this->authType = AuthTypeEnum::from($this->choice(
100111
question: 'Please choose the authentication type',
101112
choices: AuthTypeEnum::values(),
@@ -105,13 +116,23 @@ public function handle(): void
105116
if ($this->authType === AuthTypeEnum::Clerk) {
106117
$this->enableClerk();
107118

108-
$this->createOrUpdateConfigFile('.env.development', '=', [
119+
$data = [
109120
'AUTH_GUARD' => 'clerk',
110-
]);
121+
'CLERK_ALLOWED_ISSUER' => '',
122+
'CLERK_SECRET_KEY' => '',
123+
'CLERK_SIGNER_KEY_PATH' => '',
124+
];
111125

112-
$this->createOrUpdateConfigFile($envFile, '=', [
113-
'AUTH_GUARD' => 'clerk',
114-
]);
126+
if ($this->appType !== AppTypeEnum::Mobile) {
127+
$data['CLERK_ALLOWED_ORIGINS'] = '';
128+
}
129+
130+
$this->createOrUpdateConfigFile('.env.development', '=', $data);
131+
$this->createOrUpdateConfigFile($envFile, '=', $data);
132+
133+
if ($envFile !== '.env.example') {
134+
$this->createOrUpdateConfigFile('.env.example', '=', $data);
135+
}
115136
}
116137

117138
if ($this->confirm('Do you want to generate an admin user?', true)) {
@@ -198,7 +219,7 @@ public function handle(): void
198219
protected function setAutoDocContactEmail(string $email): void
199220
{
200221
$config = ArrayFile::open(base_path('config/auto-doc.php'));
201-
222+
202223
$config->set('info.contact.email', $email);
203224

204225
$config->write();
@@ -235,13 +256,7 @@ protected function fillReadme(): void
235256

236257
$this->setReadmeValue($file, 'project_name', $this->appName);
237258

238-
$type = $this->choice(
239-
question: 'What type of application will your API serve?',
240-
choices: ['Mobile', 'Web', 'Multiplatform'],
241-
default: 'Multiplatform'
242-
);
243-
244-
$this->setReadmeValue($file, 'type', $type);
259+
$this->setReadmeValue($file, 'type', $this->appType->value);
245260

246261
$this->readmeContent = $file;
247262
}
@@ -300,9 +315,9 @@ protected function fillContacts(): void
300315

301316
$this->removeTag($filePart, $key);
302317
}
303-
318+
304319
$this->setReadmeValue($filePart, 'team_lead_link', $this->codeOwnerEmail);
305-
320+
306321
$this->updateReadmeFile($filePart);
307322
}
308323

@@ -398,6 +413,8 @@ protected function createOrUpdateConfigFile(string $fileName, string $separator,
398413

399414
$lines = explode("\n", $parsed);
400415

416+
$previousKey = null;
417+
401418
foreach ($data as $key => $value) {
402419
$value = $this->addQuotes($value);
403420

@@ -409,14 +426,27 @@ protected function createOrUpdateConfigFile(string $fileName, string $separator,
409426
}
410427
}
411428

412-
$lines[] = "\n{$key}{$separator}{$value}";
429+
$item = "{$key}{$separator}{$value}";
430+
431+
if (!empty($previousKey) && $this->configKeysHaveSamePrefix($key, $previousKey)) {
432+
$lines[] = $item;
433+
} else {
434+
$lines[] = "\n{$item}";
435+
}
436+
437+
$previousKey = $key;
413438
}
414439

415440
$ymlSettings = implode("\n", $lines);
416441

417442
file_put_contents($fileName, $ymlSettings);
418443
}
419444

445+
protected function configKeysHaveSamePrefix(string $key, string $previousKey): bool
446+
{
447+
return Str::before($key, '_') === Str::before($previousKey, '_');
448+
}
449+
420450
protected function loadReadmePart(string $fileName): string
421451
{
422452
$file = base_path(DIRECTORY_SEPARATOR . self::TEMPLATES_PATH . DIRECTORY_SEPARATOR . $fileName);

src/Enums/AppTypeEnum.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace RonasIT\ProjectInitializator\Enums;
4+
5+
use RonasIT\Support\Traits\EnumTrait;
6+
7+
enum AppTypeEnum: string
8+
{
9+
use EnumTrait;
10+
11+
case Mobile = 'Mobile';
12+
case Web = 'Web';
13+
case Multiplatform = 'Multiplatform';
14+
}

0 commit comments

Comments
 (0)