diff --git a/composer.json b/composer.json index 8ccc6c0..cf6e93b 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,10 @@ "symfony/process": "^2.7 || >=3.0", "symfony/filesystem": ">=2.7" }, + "suggest": { + "imponeer/composer-yarn-installer": "Lets use composer constraints for updating yarn package version", + "imponeer/composer-nodejs-installer": "Lets use composer constraints for updating nodejs package version" + }, "extra": { "class": "MariusBuescher\\NodeComposer\\NodeComposerPlugin" } diff --git a/src/NodeComposerPlugin.php b/src/NodeComposerPlugin.php index 9a14e9e..fa299f9 100644 --- a/src/NodeComposerPlugin.php +++ b/src/NodeComposerPlugin.php @@ -10,8 +10,8 @@ use Composer\Script\Event; use Composer\Script\ScriptEvents; use Composer\Util\RemoteFilesystem; -use MariusBuescher\NodeComposer\Exception\VersionVerificationException; use MariusBuescher\NodeComposer\Exception\NodeComposerConfigException; +use MariusBuescher\NodeComposer\Exception\VersionVerificationException; use MariusBuescher\NodeComposer\Installer\NodeInstaller; use MariusBuescher\NodeComposer\Installer\YarnInstaller; @@ -38,12 +38,26 @@ public function activate(Composer $composer, IOInterface $io) $this->io = $io; $extraConfig = $this->composer->getPackage()->getExtra(); + $packageConfig = $extraConfig['mariusbuescher']['node-composer'] ?? []; + + $this->updateConfigFromPackageProvidesConfig( + $packageConfig, + 'node-version', + $packageConfig['package-for-node-version'] ?? 'imponeer/composer-nodejs-installer', + 'nodejs/node' + ); + $this->updateConfigFromPackageProvidesConfig( + $packageConfig, + 'yarn-version', + $packageConfig['package-for-yarn-version'] ?? 'imponeer/composer-yarn-installer', + 'yarnpkg/yarn' + ); - if (!isset($extraConfig['mariusbuescher']['node-composer'])) { + if (empty($packageConfig)) { throw new NodeComposerConfigException('You must configure the node composer plugin'); } - $this->config = Config::fromArray($extraConfig['mariusbuescher']['node-composer']); + $this->config = Config::fromArray($packageConfig); } public static function getSubscribedEvents() @@ -143,4 +157,21 @@ public function deactivate(Composer $composer, IOInterface $io) public function uninstall(Composer $composer, IOInterface $io) { } + + /** + * Updates local config with data from some specific packages + * + * @param array $config Local config for the update + * @param string $configKey Local config key that will be updated if specific package will be found + * @param string $packageName Package name from where to fetch provides section data + * @param string $providesName Provides key name + */ + protected function updateConfigFromPackageProvidesConfig(array &$config, $configKey, $packageName, $providesName) + { + $foundPackages = $this->composer->getRepositoryManager()->getLocalRepository()->findPackages($packageName); + if (isset($foundPackages[0])) { + $provides = $foundPackages[0]->getProvides(); + $config[$configKey] = $provides[$providesName]->getPrettyConstraint(); + } + } }