Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!