diff --git a/README.md b/README.md index 2d73d2d..5b2fc92 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/Console/Concerns/HasInitializeLaravelApp.php b/src/Console/Concerns/HasInitializeLaravelApp.php new file mode 100644 index 0000000..a0846de --- /dev/null +++ b/src/Console/Concerns/HasInitializeLaravelApp.php @@ -0,0 +1,96 @@ +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'); + } + } +} diff --git a/src/Console/Concerns/HasNodePackageManager.php b/src/Console/Concerns/HasNodePackageManager.php new file mode 100644 index 0000000..5c64b5e --- /dev/null +++ b/src/Console/Concerns/HasNodePackageManager.php @@ -0,0 +1,29 @@ +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; + } +} diff --git a/src/Console/Concerns/HasProcess.php b/src/Console/Concerns/HasProcess.php new file mode 100644 index 0000000..7ba1f9f --- /dev/null +++ b/src/Console/Concerns/HasProcess.php @@ -0,0 +1,23 @@ +run(implode(' && ', $command), function (string $type, string $output) { + $this->output->write($output); + }); + + $this->newLine(); + } +} diff --git a/src/Console/InstallCommand.php b/src/Console/InstallCommand.php index 94fa1e5..4b9028a 100644 --- a/src/Console/InstallCommand.php +++ b/src/Console/InstallCommand.php @@ -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; @@ -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. * @@ -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. */ @@ -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. */ @@ -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(); - } } diff --git a/src/Console/LaravelInitialCommand.php b/src/Console/LaravelInitialCommand.php new file mode 100644 index 0000000..e982597 --- /dev/null +++ b/src/Console/LaravelInitialCommand.php @@ -0,0 +1,49 @@ +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(); + } +} diff --git a/src/EngageServiceProvider.php b/src/EngageServiceProvider.php index e427230..ba59a6d 100644 --- a/src/EngageServiceProvider.php +++ b/src/EngageServiceProvider.php @@ -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; @@ -45,6 +46,7 @@ protected function registerCommands(): void $this->commands([ IdeHelperCommand::class, InstallCommand::class, + LaravelInitialCommand::class, UiCommand::class, ]); }