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?
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"
]
}
}
What Composer does when you run composer create-project vendor/repo some-directory is (simplifying):
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.