Skip to content
64 changes: 47 additions & 17 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Support\Str;
use RonasIT\ProjectInitializator\Enums\AuthTypeEnum;
use RonasIT\ProjectInitializator\Enums\RoleEnum;
use RonasIT\ProjectInitializator\Enums\AppTypeEnum;
use Winter\LaravelConfigWriter\ArrayFile;

class InitCommand extends Command implements Isolatable
Expand Down Expand Up @@ -69,6 +70,8 @@ class InitCommand extends Command implements Isolatable

protected string $appName;

protected AppTypeEnum $appType;

public function handle(): void
{
$this->prepareAppName();
Expand All @@ -80,7 +83,7 @@ public function handle(): void
field: 'email of code owner / team lead',
rules: 'required|email',
);

$this->appUrl = $this->ask('Please enter an application URL', "https://api.dev.{$kebabName}.com");

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

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

$this->appType = AppTypeEnum::from(
$this->choice(
question: 'What type of application will your API serve?',
choices: AppTypeEnum::values(),
default: AppTypeEnum::Multiplatform->value,
),
);

$this->authType = AuthTypeEnum::from($this->choice(
question: 'Please choose the authentication type',
choices: AuthTypeEnum::values(),
Expand All @@ -105,13 +116,23 @@ public function handle(): void
if ($this->authType === AuthTypeEnum::Clerk) {
$this->enableClerk();

$this->createOrUpdateConfigFile('.env.development', '=', [
$data = [
'AUTH_GUARD' => 'clerk',
]);
'CLERK_ALLOWED_ISSUER' => '',
'CLERK_SECRET_KEY' => '',
'CLERK_SIGNER_KEY_PATH' => '',
];

$this->createOrUpdateConfigFile($envFile, '=', [
'AUTH_GUARD' => 'clerk',
]);
if ($this->appType !== AppTypeEnum::Mobile) {
$data['CLERK_ALLOWED_ORIGINS'] = '';
}

$this->createOrUpdateConfigFile('.env.development', '=', $data);
$this->createOrUpdateConfigFile($envFile, '=', $data);

if ($envFile !== '.env.example') {
$this->createOrUpdateConfigFile('.env.example', '=', $data);
}
}

if ($this->confirm('Do you want to generate an admin user?', true)) {
Expand Down Expand Up @@ -198,7 +219,7 @@ public function handle(): void
protected function setAutoDocContactEmail(string $email): void
{
$config = ArrayFile::open(base_path('config/auto-doc.php'));

$config->set('info.contact.email', $email);

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

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

$type = $this->choice(
question: 'What type of application will your API serve?',
choices: ['Mobile', 'Web', 'Multiplatform'],
default: 'Multiplatform'
);

$this->setReadmeValue($file, 'type', $type);
$this->setReadmeValue($file, 'type', $this->appType->value);

$this->readmeContent = $file;
}
Expand Down Expand Up @@ -300,9 +315,9 @@ protected function fillContacts(): void

$this->removeTag($filePart, $key);
}

$this->setReadmeValue($filePart, 'team_lead_link', $this->codeOwnerEmail);

$this->updateReadmeFile($filePart);
}

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

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

$previousKey = null;

foreach ($data as $key => $value) {
$value = $this->addQuotes($value);

Expand All @@ -409,14 +426,27 @@ protected function createOrUpdateConfigFile(string $fileName, string $separator,
}
}

$lines[] = "\n{$key}{$separator}{$value}";
$item = "{$key}{$separator}{$value}";

if (!empty($previousKey) && $this->configKeysHaveSamePrefix($key, $previousKey)) {
$lines[] = $item;
} else {
$lines[] = "\n{$item}";
}

$previousKey = $key;
}

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

file_put_contents($fileName, $ymlSettings);
}

protected function configKeysHaveSamePrefix(string $key, string $previousKey): bool
{
return Str::before($key, '_') === Str::before($previousKey, '_');
}

protected function loadReadmePart(string $fileName): string
{
$file = base_path(DIRECTORY_SEPARATOR . self::TEMPLATES_PATH . DIRECTORY_SEPARATOR . $fileName);
Expand Down
14 changes: 14 additions & 0 deletions src/Enums/AppTypeEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace RonasIT\ProjectInitializator\Enums;

use RonasIT\Support\Traits\EnumTrait;

enum AppTypeEnum: string
{
use EnumTrait;

case Mobile = 'Mobile';
case Web = 'Web';
case Multiplatform = 'Multiplatform';
}
Loading