Run command line command in composer.json

16,617

Solution 1

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

Solution 2

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

Solution 3

If you want to run a command that should run for forever this will help you

in composer.json

"scripts": {
   
    "start-lumen" : [
        "php -S localhost:8000 -t public"
    ]
}

add --timeout=0 to run for forever

composer run-script  start-lumen --timeout=0
Share:
16,617
MariaL
Author by

MariaL

Updated on June 24, 2022

Comments

  • MariaL
    MariaL almost 2 years

    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?

  • J.Hpour
    J.Hpour almost 3 years
    Also it would be possible with: composer test.