Skip to content

Commit

Permalink
Add laravel:initial command
Browse files Browse the repository at this point in the history
  • Loading branch information
ycs77 committed Aug 15, 2024
1 parent af64d7f commit 6029f14
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 126 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ php artisan inertia:install
php artisan inertia:install --ts
```

Or just initialize the Laravel project:

```bash
php artisan laravel:initial
```

## Error Handler

> If you running the `inertia:install` command, below will automatically added.
Expand Down
96 changes: 96 additions & 0 deletions src/Console/Concerns/HasInitializeLaravelApp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

namespace Inertia\Console\Concerns;

trait HasInitializeLaravelApp
{
/**
* Update the editor config.
*/
protected function updateEditorConfig(): void
{
$editorConfig = file_get_contents(base_path('.editorconfig'));

if (str_contains($editorConfig, '[*.{yml,yaml}]')) {
$editorConfig = str_replace(
'[*.{yml,yaml}]',
'[*.{css,js,cjs,mjs,json,ts,vue,yml,yaml}]',
$editorConfig
);
}

if (! str_contains($editorConfig, '[composer.json]')) {
$editorConfig = str_replace(
"yml,yaml}]\nindent_size = 2\n",
"yml,yaml}]\nindent_size = 2\n\n[composer.json]\nindent_size = 4\n",
$editorConfig
);
}

file_put_contents(base_path('.editorconfig'), $editorConfig);

$this->components->info('Updated .editorconfig');
}

/**
* Update the timezone configuration.
*/
protected function updateTimezoneConfig(): void
{
file_put_contents(base_path('.env.example'), str_replace(
'APP_TIMEZONE=UTC',
'APP_TIMEZONE=Asia/Taipei',
file_get_contents(base_path('.env.example'))
));

file_put_contents(base_path('.env'), str_replace(
'APP_TIMEZONE=UTC',
'APP_TIMEZONE=Asia/Taipei',
file_get_contents(base_path('.env'))
));

$this->components->info('Updated timezone config');
}

/**
* Update the locale configuration.
*/
protected function updateLocaleConfig(): void
{
$env = file_get_contents(base_path('.env.example'));
$env = str_replace('APP_LOCALE=en', 'APP_LOCALE=zh_TW', $env);
$env = str_replace('APP_FAKER_LOCALE=en_US', 'APP_FAKER_LOCALE=zh_TW', $env);
file_put_contents(base_path('.env.example'), $env);

$env = file_get_contents(base_path('.env'));
$env = str_replace('APP_LOCALE=en', 'APP_LOCALE=zh_TW', $env);
$env = str_replace('APP_FAKER_LOCALE=en_US', 'APP_FAKER_LOCALE=zh_TW', $env);
file_put_contents(base_path('.env'), $env);

$this->components->info('Updated locale config');
}

/**
* Clear default js files.
*/
protected function clearDefaultJsFiles(): void
{
if (file_exists(resource_path('js/app.js'))) {
$js = 'js';
} elseif (file_exists(resource_path('js/app.ts'))) {
$js = 'ts';
} else {
return;
}

$appJs = trim(file_get_contents(resource_path("js/app.$js")));

if (str_contains($appJs, "import './bootstrap';")) {
file_put_contents(resource_path("js/app.$js"), '');

@unlink(resource_path('js/bootstrap.js'));

$this->components->info('Cleared default js files');
}
}
}
29 changes: 29 additions & 0 deletions src/Console/Concerns/HasNodePackageManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Inertia\Console\Concerns;

use Illuminate\Support\Facades\Process;
use Inertia\Support\NodePackageManager;

trait HasNodePackageManager
{
/**
* Create a new Node package manager instance.
*/
protected function createNpm(string $workingPath): NodePackageManager
{
$npm = new NodePackageManager($workingPath);

$npm->runningProcessWith(function ($command) use ($workingPath) {
Process::path($workingPath)
->timeout(10 * 60) // 10 minutes
->run($command, function (string $type, string $output) {
$this->output->write($output);
});
});

$npm->format();

return $npm;
}
}
23 changes: 23 additions & 0 deletions src/Console/Concerns/HasProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Inertia\Console\Concerns;

use Illuminate\Support\Facades\Process;

trait HasProcess
{
/**
* Run the given command.
*/
protected function runProcessCommand($command, string $workingPath): void
{
$command = is_array($command) ? $command : [$command];

Process::path($workingPath)
->run(implode(' && ', $command), function (string $type, string $output) {
$this->output->write($output);
});

$this->newLine();
}
}
130 changes: 4 additions & 126 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Composer;
use Illuminate\Support\Facades\Process;
use Inertia\Support\NodePackageManager;
use Symfony\Component\Console\Attribute\AsCommand;

Expand All @@ -15,6 +14,10 @@
#[AsCommand(name: 'inertia:install')]
class InstallCommand extends Command
{
use Concerns\HasInitializeLaravelApp;
use Concerns\HasNodePackageManager;
use Concerns\HasProcess;

/**
* The name and signature of the console command.
*
Expand Down Expand Up @@ -138,96 +141,6 @@ protected function findComposer(): string
return implode(' ', $this->composer->findComposer());
}

/**
* Update the editor config.
*/
protected function updateEditorConfig(): void
{
$editorConfig = file_get_contents(base_path('.editorconfig'));

if (str_contains($editorConfig, '[*.{yml,yaml}]')) {
$editorConfig = str_replace(
'[*.{yml,yaml}]',
'[*.{css,js,cjs,mjs,json,ts,vue,yml,yaml}]',
$editorConfig
);
}

if (! str_contains($editorConfig, '[composer.json]')) {
$editorConfig = str_replace(
"yml,yaml}]\nindent_size = 2\n",
"yml,yaml}]\nindent_size = 2\n\n[composer.json]\nindent_size = 4\n",
$editorConfig
);
}

file_put_contents(base_path('.editorconfig'), $editorConfig);

$this->components->info('Updated .editorconfig');
}

/**
* Update the timezone configuration.
*/
protected function updateTimezoneConfig(): void
{
file_put_contents(base_path('.env.example'), str_replace(
'APP_TIMEZONE=UTC',
'APP_TIMEZONE=Asia/Taipei',
file_get_contents(base_path('.env.example'))
));

file_put_contents(base_path('.env'), str_replace(
'APP_TIMEZONE=UTC',
'APP_TIMEZONE=Asia/Taipei',
file_get_contents(base_path('.env'))
));

$this->components->info('Updated timezone config');
}

/**
* Update the locale configuration.
*/
protected function updateLocaleConfig(): void
{
$env = file_get_contents(base_path('.env.example'));
$env = str_replace('APP_LOCALE=en', 'APP_LOCALE=zh_TW', $env);
$env = str_replace('APP_FAKER_LOCALE=en_US', 'APP_FAKER_LOCALE=zh_TW', $env);
file_put_contents(base_path('.env.example'), $env);

$env = file_get_contents(base_path('.env'));
$env = str_replace('APP_LOCALE=en', 'APP_LOCALE=zh_TW', $env);
$env = str_replace('APP_FAKER_LOCALE=en_US', 'APP_FAKER_LOCALE=zh_TW', $env);
file_put_contents(base_path('.env'), $env);

$this->components->info('Updated locale config');
}

/**
* Clear default js files.
*/
protected function clearDefaultJsFiles(): void
{
if (file_exists(resource_path('js/app.js'))) {
$js = 'js';
} elseif (file_exists(resource_path('js/app.ts'))) {
$js = 'ts';
} else {
return;
}

$appJs = trim(file_get_contents(resource_path("js/app.$js")));

if (str_contains($appJs, "import './bootstrap';")) {
file_put_contents(resource_path("js/app.$js"), '');

@unlink(resource_path('js/bootstrap.js'));

$this->components->info('Cleared default js files');
}
}

/**
* Install the "inertiajs/inertia-laravel" package.
*/
Expand Down Expand Up @@ -604,26 +517,6 @@ protected function installTailwindCss(): void
$this->components->info('Installed tailwindcss successfully.');
}

/**
* Create a new Node package manager instance.
*/
protected function createNpm(string $workingPath): NodePackageManager
{
$npm = new NodePackageManager($workingPath);

$npm->runningProcessWith(function ($command) use ($workingPath) {
Process::path($workingPath)
->timeout(10 * 60) // 10 minutes
->run($command, function (string $type, string $output) {
$this->output->write($output);
});
});

$npm->format();

return $npm;
}

/**
* Install the node dependencies.
*/
Expand All @@ -634,19 +527,4 @@ protected function installNodeDependencies(): void
$this->npm->commit();
$this->npm->install();
}

/**
* Run the given command.
*/
protected function runProcessCommand($command, string $workingPath): void
{
$command = is_array($command) ? $command : [$command];

Process::path($workingPath)
->run(implode(' && ', $command), function (string $type, string $output) {
$this->output->write($output);
});

$this->newLine();
}
}
49 changes: 49 additions & 0 deletions src/Console/LaravelInitialCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Inertia\Console;

use Illuminate\Console\Command;
use Inertia\Support\NodePackageManager;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'laravel:initial')]
class LaravelInitialCommand extends Command
{
use Concerns\HasInitializeLaravelApp;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'laravel:initial';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Initialize Laravel application';

/**
* Execute the console command.
*/
public function handle(): void
{
$cwd = getcwd();

$this->info(' Initialize laravel application');
$this->updateEditorConfig();
$this->updateTimezoneConfig();
$this->updateLocaleConfig();
$this->clearDefaultJsFiles();
$this->formatPackageJson($cwd);

$this->components->info('Laravel application initialize successfully.');
}

protected function formatPackageJson(string $cwd): void
{
(new NodePackageManager($cwd))->format();
}
}
2 changes: 2 additions & 0 deletions src/EngageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\ServiceProvider;
use Inertia\Console\IdeHelperCommand;
use Inertia\Console\InstallCommand;
use Inertia\Console\LaravelInitialCommand;
use Inertia\Console\UiCommand;
use Inertia\Exceptions\Handler as ExceptionHandler;
use Inertia\Pagination\Paginator;
Expand Down Expand Up @@ -45,6 +46,7 @@ protected function registerCommands(): void
$this->commands([
IdeHelperCommand::class,
InstallCommand::class,
LaravelInitialCommand::class,
UiCommand::class,
]);
}
Expand Down

0 comments on commit 6029f14

Please sign in to comment.