Skip to content

Commit 394f3e4

Browse files
committed
bug #736 Fix NPM package sorting algorithm and encoding (tgalopin)
This PR was merged into the 1.9-dev branch. Discussion ---------- Fix NPM package sorting algorithm and encoding Commits ------- 22bfd14 Fix NPM package sorting algorithm and encoding
2 parents 0c1d93d + 22bfd14 commit 394f3e4

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

src/PackageJsonSynchronizer.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,6 @@ private function removePackageJsonLinks(array $packageJson)
6363
}
6464
}
6565

66-
private static function sortPackages(JsonManipulator $manipulator, $subNode)
67-
{
68-
$content = json_decode($manipulator->getContents(), true);
69-
$subNodeContent = $content[$subNode];
70-
ksort($subNodeContent);
71-
72-
return json_encode([$subNode => $subNodeContent]);
73-
}
74-
7566
private function addPackageJsonLink(string $phpPackage)
7667
{
7768
if (!$assetsDir = $this->resolveAssetsDir($phpPackage)) {
@@ -80,7 +71,14 @@ private function addPackageJsonLink(string $phpPackage)
8071

8172
$manipulator = new JsonManipulator(file_get_contents($this->rootDir.'/package.json'));
8273
$manipulator->addSubNode('devDependencies', '@'.$phpPackage, 'file:vendor/'.$phpPackage.$assetsDir);
83-
file_put_contents($this->rootDir.'/package.json', self::sortPackages($manipulator, 'devDependencies'));
74+
75+
$content = json_decode($manipulator->getContents(), true);
76+
77+
$devDependencies = $content['devDependencies'];
78+
uksort($devDependencies, 'strnatcmp');
79+
$content['devDependencies'] = $devDependencies;
80+
81+
file_put_contents($this->rootDir.'/package.json', $manipulator->format($content, -1));
8482
}
8583

8684
private function registerWebpackResources(array $phpPackages)
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
2-
"devDependencies": {
3-
"@symfony/stimulus-bridge": "^1.0.0",
4-
"stimulus": "^1.1.1",
5-
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets"
6-
}
2+
"name": "symfony/fixture",
3+
"devDependencies": {
4+
"@symfony/stimulus-bridge": "^1.0.0",
5+
"stimulus": "^1.1.1",
6+
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets"
7+
}
78
}

tests/PackageJsonSynchronizerTest.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function testSynchronizeNoPackage()
4040
// Should remove existing package references as it has been removed from the lock
4141
$this->assertSame(
4242
[
43+
'name' => 'symfony/fixture',
4344
'devDependencies' => [
4445
'@symfony/stimulus-bridge' => '^1.0.0',
4546
'stimulus' => '^1.1.1',
@@ -64,6 +65,7 @@ public function testSynchronizeExistingPackage()
6465
// Should keep existing package references and config
6566
$this->assertSame(
6667
[
68+
'name' => 'symfony/fixture',
6769
'devDependencies' => [
6870
'@symfony/existing-package' => 'file:vendor/symfony/existing-package/Resources/assets',
6971
'@symfony/stimulus-bridge' => '^1.0.0',
@@ -98,17 +100,18 @@ public function testSynchronizeNewPackage()
98100
{
99101
$this->synchronizer->synchronize(['symfony/existing-package', 'symfony/new-package']);
100102

101-
// Should keep existing package references and config and add the new package
103+
// Should keep existing package references and config and add the new package, while keeping the formatting
102104
$this->assertSame(
103-
[
104-
'devDependencies' => [
105-
'@symfony/existing-package' => 'file:vendor/symfony/existing-package/Resources/assets',
106-
'@symfony/new-package' => 'file:vendor/symfony/new-package/assets',
107-
'@symfony/stimulus-bridge' => '^1.0.0',
108-
'stimulus' => '^1.1.1',
109-
],
110-
],
111-
json_decode(file_get_contents($this->tempDir.'/package.json'), true)
105+
'{
106+
"name": "symfony/fixture",
107+
"devDependencies": {
108+
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets",
109+
"@symfony/new-package": "file:vendor/symfony/new-package/assets",
110+
"@symfony/stimulus-bridge": "^1.0.0",
111+
"stimulus": "^1.1.1"
112+
}
113+
}',
114+
file_get_contents($this->tempDir.'/package.json')
112115
);
113116

114117
$this->assertSame(

0 commit comments

Comments
 (0)