Skip to content

Commit 57aa098

Browse files
authored
Merge pull request #1073 from WyriHaximus/add-scripts-section-if-it-doesnt-exist
Add scripts section to `composer.json` if it doesn't exists
2 parents 64b15f7 + 0bbd409 commit 57aa098

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

src/Composer/Installer.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,22 @@ private static function addMakeOnInstallOrUpdate(IOInterface $io, string $rootPa
125125

126126
$composerJson = json_decode($composerJsonString, true);
127127
$composerJsonHash = hash('sha512', (string) json_encode($composerJson, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
128-
if (is_array($composerJson) && array_key_exists('scripts', $composerJson) && is_array($composerJson['scripts'])) {
129-
if (array_key_exists('post-install-cmd', $composerJson['scripts'])) {
130-
$composerJson['scripts']['post-install-cmd'] = self::addMakeOnInstallOrUpdateToScriptsSectionAndRemoveCommandsItReplaces($composerJson['scripts']['post-install-cmd']);
128+
if (is_array($composerJson)) {
129+
if (! (array_key_exists('scripts', $composerJson) && is_array($composerJson['scripts']))) {
130+
$composerJson['scripts'] = [];
131131
}
132132

133-
if (array_key_exists('post-update-cmd', $composerJson['scripts'])) {
134-
$composerJson['scripts']['post-update-cmd'] = self::addMakeOnInstallOrUpdateToScriptsSectionAndRemoveCommandsItReplaces($composerJson['scripts']['post-update-cmd']);
133+
if (! array_key_exists('post-install-cmd', $composerJson['scripts'])) {
134+
$composerJson['scripts']['post-install-cmd'] = [];
135135
}
136+
137+
$composerJson['scripts']['post-install-cmd'] = self::addMakeOnInstallOrUpdateToScriptsSectionAndRemoveCommandsItReplaces($composerJson['scripts']['post-install-cmd']);
138+
139+
if (! array_key_exists('post-update-cmd', $composerJson['scripts'])) {
140+
$composerJson['scripts']['post-update-cmd'] = [];
141+
}
142+
143+
$composerJson['scripts']['post-update-cmd'] = self::addMakeOnInstallOrUpdateToScriptsSectionAndRemoveCommandsItReplaces($composerJson['scripts']['post-update-cmd']);
136144
}
137145

138146
$replacementComposerJsonString = json_encode($composerJson, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

0 commit comments

Comments
 (0)