目前為止加入工具箱的專案都是 javascript 框架,不曉得 NX 有沒有辦法搭配非 javascript 體系的框架。
來實驗看看吧,嘗試在 NX 中加入 php 框架的 Laravel 專案。
要安裝 Laravel 前要先安裝 php 跟 composer。
接著一樣到 apps 底下開一個新的 php 專案。
composer create-project laravel/laravel ironman-laravel
然後添加 project.json
。
{
"name": "ironman-laravel",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/ironman-laravel",
"projectType": "application",
"targets": {
"serve": {
"executor": "nx:run-commands",
"options": {
"commands": ["php artisan serve"],
"cwd": "{projectRoot}",
"forwardAllArgs": false
}
}
},
"tags": ["scope:ironman-laravel"],
"implicitDependencies": ["tag:scope:ironman-laravel"]
}
測試一下指令,確認是能運作的,所以可以藉由 nx:run-commands
來操作其他框架的專案的,不過 專案間的關聯就要手動設置,並且 executor 跟 generator 等工具也只能自制了。
pnpm exec nx run ironman-laravel:serve
順便試試 laravel 預設的 docker 工具 sail 的運作,首先到 ironman-laravel 目錄下初始 sail 的設定,生成 docker-compose.yml
。
php artisan sail:install
有 docker-compose.yml
後先做一遍 docker image 的建置,用 NX target 跑不過。
./vendor/bin/sail build
再來設定 target ,加上啟動跟暫停 docker 的指令。
{
"name": "ironman-laravel",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "apps/ironman-laravel",
"projectType": "application",
"targets": {
"serve": {
"executor": "nx:run-commands",
"options": {
"commands": ["php artisan serve"],
"cwd": "{projectRoot}",
"forwardAllArgs": false
}
},
"sail": {
"executor": "nx:run-commands",
"options": {
"commands": ["./vendor/bin/sail up -d"],
"cwd": "{projectRoot}",
"forwardAllArgs": false
}
},
"sail-stop": {
"executor": "nx:run-commands",
"options": {
"commands": ["./vendor/bin/sail stop"],
"cwd": "{projectRoot}",
"forwardAllArgs": false
}
}
},
"tags": ["scope:ironman-laravel"],
"implicitDependencies": ["tag:scope:ironman-laravel"]
}
就能用 NX 啟動 sail 了。
pnpm exec nx run ironman-laravel:sail