12

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?

MariaL
  • 1,112
  • 2
  • 16
  • 41
  • Might want to take a look at the [Events section](https://getcomposer.org/doc/articles/scripts.md) (pre-update-cmd or post-update-cmd) – brombeer Feb 08 '18 at 10:03

3 Answers3

32

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
iainn
  • 16,826
  • 9
  • 33
  • 40
4

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
Tushar Saha
  • 647
  • 6
  • 13
1

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