Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command line command in composer.json

I'm trying to write a composer.json file that will run several command line commands in a row so just as an example something like this:

"scripts": {
   "test": [
        "@createDir"
   ],
   "createDir": "mkdir testing"

}

When I run the composer file in terminal using composer.phar update the directory isn't created though. Can anybody point me in the right direction how to do this or what I'm doing wrong?

like image 409
MariaL Avatar asked Feb 08 '18 09:02

MariaL


2 Answers

Composer doesn't run all scripts by default at the end of an install or update. For that to happen, your script needs to be under one of the Command Event keys, e.g. post-update-cmd.

You can still reference other scripts within these blocks, e.g.

"scripts": {
    "post-install-cmd": [
        "@test"
    ],
    "test": [
        "touch foo"
    ]
}

To run an individual script, you use the run-script command:

composer run-script test
like image 89
iainn Avatar answered Sep 22 '22 15:09

iainn


Maybe the structure is wrong, let me show you an example:

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
    "php": ">=5.6.4",
    "laravel/framework": "5.3.*",
    "acacha/admin-lte-template-laravel": "dev-master",
    "yajra/laravel-datatables-oracle": "^6.21",
    "barryvdh/laravel-dompdf": "^0.7.0",
    "spatie/laravel-backup": "^3.0.0"
},
"require-dev": {
    "fzaninotto/faker": "~1.4",
    "mockery/mockery": "0.9.*",
    "phpunit/phpunit": "~5.0",
    "symfony/css-selector": "3.1.*",
    "symfony/dom-crawler": "3.1.*"
},
"autoload": {
    "classmap": [
        "database",
        "App/Helpers/MyCustomHelper"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},
"autoload-dev": {
    "classmap": [
        "tests/TestCase.php"
    ]
},
"scripts": {
    "post-root-package-install": [
        "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
    ],
    "post-create-project-cmd": [
        "php artisan key:generate"
    ],
    "post-install-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postInstall",
        "php artisan optimize"
    ],
    "post-update-cmd": [
        "Illuminate\\Foundation\\ComposerScripts::postUpdate",
        "php artisan optimize"
    ]
},
"config": {
    "preferred-install": "dist"
}
}

EDIT: Maybe you can try with composer dump-autoload

like image 32
Ángel Carlos del Pozo Muela Avatar answered Sep 21 '22 15:09

Ángel Carlos del Pozo Muela