szeidler / composer-patches-cli

Adds a CLI wrapper for cweagans/composer-patches
MIT License
28 stars 8 forks source link

Improve search of composer.json file #1

Closed esolitos closed 6 years ago

esolitos commented 6 years ago

(Sorry for initial empty issue) Since version 1.5 composer can handle running in subdirectories, prompting the user with a ,essage like:

No composer.json in current directory, do you want to use the one at /path/to/composer? [Y,n]?

I believe this plugin should use the same approach. As of now if run in a subdirectory an error is returned:

Composer could not find a composer.json file in /path/to/composer/subdir
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section
szeidler commented 6 years ago

I see, that's indeed a problem. I just had a look into Composer. Unfortunately the composer.json fallback is not encapsulated, so that we cannot easily use a public method.

But it's basically following code, that is handling it. It can be integrated as an initializing method.

    $dir = dirname(getcwd());
    $home = realpath(getenv('HOME') ?: getenv('USERPROFILE') ?: '/');
    // abort when we reach the home dir or top of the filesystem
    while (dirname($dir) !== $dir && $dir !== $home) {
      if (file_exists($dir.'/'.Factory::getComposerFile())) {
        if ($io->askConfirmation('<info>No composer.json in current directory, do you want to use the one at '.$dir.'?</info> [<comment>Y,n</comment>]? ', true)) {
          $oldWorkingDir = getcwd();
          chdir($dir);
        }
        break;
      }
      $dir = dirname($dir);
    }