Skip to content
Advertisement

“php artisan myCommand” handling the post-install-cmd event returned with error code 1

I creaded a console command that will setup the database automatically based on user input and I added that command in post-install-cmd but when i run the composer install command it return error code 1.

If i run the command manually like php artisan setupDB it work, I dont know what is the issue.

May be I am adding my command in wrong script event!

This is my script section of composer.json file.

"scripts": {
        "post-autoload-dump": [
            "Illuminate\Foundation\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r "file_exists('.env') || copy('.env.example', '.env');""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ],
        "post-install-cmd": [
            "Illuminate\Foundation\ComposerScripts::postUpdate",
            "php artisan setupDb",
            "php artisan optimize"
        ]
    }

This is the error SS.

enter image description here

Advertisement

Answer

The problem is with the composer version, becase if you run any artisan command in post-install-cmd or pre-install-cmd it will run but some command ask input from user, just like mine as I describe in question that setupDb will take input from user.

How I found the issue ?

I used an example of post-autoload-dump that "Illuminate\Foundation\ComposerScripts::postAutoloadDump", was added in this event, I was thinking that for console command there should be something like this.

I try to add IlluminateConsole in post-install-cmd before artisan command and it return that it is deprecated with current composer version, So i updated my composer to v2, then it work like a charm.

Answer

So the answer will be that you need to update composer version to run post-install-cmd custom commands like(setupDb)

"scripts": {
        "post-autoload-dump": [
            "Illuminate\Foundation\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-root-package-install": [
            "@php -r "file_exists('.env') || copy('.env.example', '.env');""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ],
        "post-install-cmd": [
            "php artisan setupDb",
            "php artisan optimize"
        ]
    }

and it will not return any error also it will ask input from user if any command require.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement