Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
How do I get Composer command arguments for custom scripts?

My goal is to change the current user directory after a project has been created.

I am running composer create-project vendor/repo some-directory, and I have the following section, in the composer.json file.

{
    "scripts": {
        "post-create-project-cmd": [
            "cd /some/destination"
        ]
    }
}

So I need dynamically replace /some/destination with some-directory, specified in the command line arguments.

Is it possible?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

As mentioned in the docs :

What is a script?

A script, in Composer's terms, can either be a PHP callback (defined as a static method) or any command-line executable command.

so , you may either use a command like mv -type man mv in your terminal for more info- :

{
    "scripts": {
        "post-create-project-cmd": [
            "mv /some/destination /some-directory",
            "cd /some-directory"
        ]
    }
}

or by creating a callable method to handle this for you.

{
    "scripts": {
        "post-create-project-cmd": [
            "SomeVendor\\SomeObject::renameMyDirMethod"
        ]
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

What Composer does when you run composer create-project vendor/repo some-directory is (simplifying):

  • Create the some-directory directory (If the directory is not empty, Composer raises the exception Project directory some-directory/ is not empty.)
  • Change the current directory to some-directory
  • Execute the scripts reported in the scripts section of the project composer.json file (In your case the one used from vendor/repo.)

So, if you are showing the scripts section used from the vendor/repo, changing the current directory to the directory where the project is being created is not necessary, since Composer already does that.
If you need to get the current directory from a Bash script listed in the scripts section of the project being created, you just check the content of the variable $PWD, which is the equivalent of getcwd() in PHP.

As example of what done from a Composer project, see the content of ScriptHandler.php, used from the Composer project drupal-composer/drupal-project. It creates some files in the directory whose filename is returned from getcwd().

$fs = new Filesystem();
$drupalFinder = new DrupalFinder();
$drupalFinder->locateRoot(getcwd());
$drupalRoot = $drupalFinder->getDrupalRoot();
$dirs = [
  'modules',
  'profiles',
  'themes',
];
// Required for unit testing
foreach ($dirs as $dir) {
  if (!$fs->exists($drupalRoot . '/'. $dir)) {
    $fs->mkdir($drupalRoot . '/'. $dir);
    $fs->touch($drupalRoot . '/'. $dir . '/.gitkeep');
  }
}

Actually, it is creating the directories inside a directory found inside the current directory, but this doesn't change that the directory where the project is created is set as current directory from Composer.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda