-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
209 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters