Skip to content

Commit 7e3a00f

Browse files
committed
Keep existing constraints in packages.json
1 parent 52baff1 commit 7e3a00f

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

src/PackageJsonSynchronizer.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Composer\Json\JsonFile;
1515
use Composer\Json\JsonManipulator;
16+
use Composer\Semver\VersionParser;
1617
use Seld\JsonLint\ParsingException;
1718

1819
/**
@@ -136,8 +137,10 @@ private function registerDependencies(array $flexDependencies): bool
136137
$content['devDependencies'][$dependency] = $constraint;
137138
$didChangePackageJson = true;
138139
} elseif ($constraint !== $content[$parentNode][$dependency]) {
139-
$content[$parentNode][$dependency] = $constraint;
140-
$didChangePackageJson = true;
140+
if ($this->shouldUpdateConstraint($content[$parentNode][$dependency], $constraint)) {
141+
$content[$parentNode][$dependency] = $constraint;
142+
$didChangePackageJson = true;
143+
}
141144
}
142145
}
143146

@@ -163,6 +166,19 @@ private function registerDependencies(array $flexDependencies): bool
163166
return $didChangePackageJson;
164167
}
165168

169+
private function shouldUpdateConstraint(string $existingConstraint, string $constraint)
170+
{
171+
try {
172+
$versionParser = new VersionParser();
173+
$existingConstraint = $versionParser->parseConstraints($existingConstraint);
174+
$constraint = $versionParser->parseConstraints($constraint);
175+
176+
return !$existingConstraint->matches($constraint);
177+
} catch (\UnexpectedValueException $e) {
178+
return true;
179+
}
180+
}
181+
166182
private function registerWebpackResources(array $phpPackages)
167183
{
168184
if (!file_exists($controllersJsonPath = $this->rootDir.'/assets/controllers.json')) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "symfony/fixture",
3+
"devDependencies": {
4+
"@hotcookies": "^2",
5+
"@hotdogs": "2.4.1",
6+
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets"
7+
},
8+
"browserslist": [
9+
"defaults"
10+
]
11+
}

tests/PackageJsonSynchronizerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,32 @@ public function testExistingElevatedPackage()
245245
json_decode(file_get_contents($this->tempDir.'/package.json'), true)
246246
);
247247
}
248+
249+
public function testStricterConstraintsAreKept()
250+
{
251+
(new Filesystem())->copy($this->tempDir.'/stricter_constraints_package.json', $this->tempDir.'/package.json', true);
252+
253+
$this->synchronizer->synchronize([
254+
[
255+
'name' => 'symfony/existing-package',
256+
'keywords' => ['symfony-ux'],
257+
],
258+
]);
259+
260+
// Should keep existing constraints when stricter than packages ones
261+
$this->assertSame(
262+
[
263+
'name' => 'symfony/fixture',
264+
'devDependencies' => [
265+
'@hotcookies' => '^2',
266+
'@hotdogs' => '2.4.1',
267+
'@symfony/existing-package' => 'file:vendor/symfony/existing-package/Resources/assets',
268+
],
269+
'browserslist' => [
270+
'defaults',
271+
],
272+
],
273+
json_decode(file_get_contents($this->tempDir.'/package.json'), true)
274+
);
275+
}
248276
}

0 commit comments

Comments
 (0)