Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If specific packages are found in project installation, gets versions from yarn and nodejs from these packages config #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
37 changes: 34 additions & 3 deletions src/NodeComposerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand Down Expand Up @@ -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();
}
}
}