Skip to content

Commit e09ea41

Browse files
bug #754 Package JSON sync alters array formatting (HypeMC)
This PR was merged into the 1.12-dev branch. Discussion ---------- Package JSON sync alters array formatting Currently, each time `composer install` is run, it changes the formatting of arrays keys in `package.json`: ```diff { - "browserslist": [ - "defaults" - ] + "browserslist": ["defaults"] } ``` This is especially annoying since `npm install` actually reverts it to the previous formatting, so it's a constant "battle" between the two package managers. Commits ------- 6b998f8 Fix package json sync altering array formatting
2 parents ea1fe6e + 6b998f8 commit e09ea41

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

src/PackageJsonSynchronizer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ private function addPackageJsonLink(string $phpPackage)
7676

7777
$devDependencies = $content['devDependencies'];
7878
uksort($devDependencies, 'strnatcmp');
79-
$content['devDependencies'] = $devDependencies;
79+
$manipulator->addMainKey('devDependencies', $devDependencies);
8080

81-
file_put_contents($this->rootDir.'/package.json', $manipulator->format($content, -1));
81+
file_put_contents($this->rootDir.'/package.json', $manipulator->getContents());
8282
}
8383

8484
private function registerWebpackResources(array $phpPackages)

tests/Fixtures/packageJson/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@
44
"@symfony/stimulus-bridge": "^1.0.0",
55
"stimulus": "^1.1.1",
66
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets"
7-
}
7+
},
8+
"browserslist": [
9+
"defaults"
10+
]
811
}

tests/PackageJsonSynchronizerTest.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public function testSynchronizeNoPackage()
4545
'@symfony/stimulus-bridge' => '^1.0.0',
4646
'stimulus' => '^1.1.1',
4747
],
48+
'browserslist' => [
49+
'defaults',
50+
],
4851
],
4952
json_decode(file_get_contents($this->tempDir.'/package.json'), true)
5053
);
@@ -71,6 +74,9 @@ public function testSynchronizeExistingPackage()
7174
'@symfony/stimulus-bridge' => '^1.0.0',
7275
'stimulus' => '^1.1.1',
7376
],
77+
'browserslist' => [
78+
'defaults',
79+
],
7480
],
7581
json_decode(file_get_contents($this->tempDir.'/package.json'), true)
7682
);
@@ -109,9 +115,12 @@ public function testSynchronizeNewPackage()
109115
"@symfony/new-package": "file:vendor/symfony/new-package/assets",
110116
"@symfony/stimulus-bridge": "^1.0.0",
111117
"stimulus": "^1.1.1"
112-
}
118+
},
119+
"browserslist": [
120+
"defaults"
121+
]
113122
}',
114-
file_get_contents($this->tempDir.'/package.json')
123+
trim(file_get_contents($this->tempDir.'/package.json'))
115124
);
116125

117126
$this->assertSame(
@@ -142,4 +151,25 @@ public function testSynchronizeNewPackage()
142151
json_decode(file_get_contents($this->tempDir.'/assets/controllers.json'), true)
143152
);
144153
}
154+
155+
public function testArrayFormattingHasNotChanged()
156+
{
157+
$this->synchronizer->synchronize(['symfony/existing-package']);
158+
159+
// Should keep existing array formatting
160+
$this->assertSame(
161+
'{
162+
"name": "symfony/fixture",
163+
"devDependencies": {
164+
"@symfony/existing-package": "file:vendor/symfony/existing-package/Resources/assets",
165+
"@symfony/stimulus-bridge": "^1.0.0",
166+
"stimulus": "^1.1.1"
167+
},
168+
"browserslist": [
169+
"defaults"
170+
]
171+
}',
172+
trim(file_get_contents($this->tempDir.'/package.json'))
173+
);
174+
}
145175
}

0 commit comments

Comments
 (0)