Skip to content
Advertisement

What does the “key: value” syntax do in composer scripts

From compser documentation you can write scripts like so:

{
    "scripts": {
        "some-event": [
            "command1",
            "command2",
            "command3"
        ],
    }
}

But in Symfony I found a slightly different syntax that instead of a list is using a key:value pair like this:

{
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
    },
}

How does this syntax work exactly? Does it redirect commands to another one, adds a dependency between 2 commands or what?

Advertisement

Answer

This syntax is used by symfony/flex plugin and is only valid under auto-scripts key. The code in flex looks like this:

        $json = new JsonFile(Factory::getComposerFile());
        $jsonContents = $json->read();

        $executor = new ScriptExecutor($this->composer, $this->io, $this->options);
        foreach ($jsonContents['scripts']['auto-scripts'] as $cmd => $type) {
            $executor->execute($type, $cmd);
        }

With execute being handled using this code:

        switch ($type) {
            case 'symfony-cmd':
                return $this->expandSymfonyCmd($cmd);
            case 'php-script':
                return $this->expandPhpScript($cmd);
            case 'script':
                return $cmd;
            default:
                throw new InvalidArgumentException();
        }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement