diff --git a/src/Command/UpdateLicensesCommand.php b/src/Command/UpdateLicensesCommand.php index 31b9d75..e30413a 100644 --- a/src/Command/UpdateLicensesCommand.php +++ b/src/Command/UpdateLicensesCommand.php @@ -22,6 +22,7 @@ namespace PrestaShop\HeaderStamp\Command; +use Exception; use PhpParser\Node\Stmt; use PhpParser\ParserFactory; use PrestaShop\HeaderStamp\LicenseHeader; @@ -399,32 +400,50 @@ private function addLicenseToHtmlFile(SplFileInfo $file): void $this->addLicenseToFile($file, ''); } - private function addLicenseToJsonFile(SplFileInfo $file): bool + /** + * @throws Exception + */ + private function addLicenseToJsonFile(SplFileInfo $file): void { if (!in_array($file->getFilename(), ['composer.json', 'package.json'])) { - return false; + return; } $content = json_decode($file->getContents(), true); - $oldContent = $content; - $content['author'] = 'PrestaShop'; + + $authorDetails = [ + 'name' => 'PrestaShop SA', + 'email' => 'contact@prestashop.com', + ]; + + // update author information depending of file + if ('composer.json' === $file->getFilename()) { + $content['authors'] = [$authorDetails]; + } else { // package.json + $content['author'] = $authorDetails; + } + $content['license'] = (false !== strpos($this->license, 'afl')) ? 'AFL-3.0' : 'OSL-3.0'; + $encodedContent = json_encode($content, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); + + if (!$encodedContent) { + throw new Exception('File can not be encoded to JSON format'); + } + + // add blank line in end of file if not exist + if (substr($encodedContent, -1) !== "\n") { + $encodedContent .= "\n"; + } + if (!$this->runAsDry) { - $result = file_put_contents( + file_put_contents( $this->targetDirectory . '/' . $file->getRelativePathname(), - json_encode($content, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT) + $encodedContent ); - } else { - $result = true; } - $newFileContent = (string) json_encode($content); - $oldFileContent = (string) json_encode($oldContent); - - $this->reportOperationResult($newFileContent, $oldFileContent, $file->getFilename()); - - return false !== $result; + $this->reportOperationResult($encodedContent, $file->getContents(), $file->getFilename()); } /** diff --git a/tests/integration/expected/dashproducts/composer.json b/tests/integration/expected/dashproducts/composer.json index 8b76f6a..3bf140b 100644 --- a/tests/integration/expected/dashproducts/composer.json +++ b/tests/integration/expected/dashproducts/composer.json @@ -15,6 +15,5 @@ "config": { "preferred-install": "dist" }, - "type": "prestashop-module", - "author": "PrestaShop" -} \ No newline at end of file + "type": "prestashop-module" +} diff --git a/tests/integration/expected/gsitemap/composer.json b/tests/integration/expected/gsitemap/composer.json index c3dfa4b..9e7c304 100644 --- a/tests/integration/expected/gsitemap/composer.json +++ b/tests/integration/expected/gsitemap/composer.json @@ -3,12 +3,6 @@ "description": "PrestaShop module gsitemap", "homepage": "https://github.com/PrestaShop/gsitemap", "license": "AFL-3.0", - "authors": [ - { - "name": "PrestaShop SA", - "email": "contact@prestashop.com" - } - ], "require": { "php": ">=5.6.0" }, @@ -16,5 +10,10 @@ "preferred-install": "dist" }, "type": "prestashop-module", - "author": "PrestaShop" -} \ No newline at end of file + "authors": [ + { + "name": "PrestaShop SA", + "email": "contact@prestashop.com" + } + ] +} diff --git a/tests/integration/module-samples/dashproducts/composer.json b/tests/integration/module-samples/dashproducts/composer.json index 8b76f6a..3bf140b 100644 --- a/tests/integration/module-samples/dashproducts/composer.json +++ b/tests/integration/module-samples/dashproducts/composer.json @@ -15,6 +15,5 @@ "config": { "preferred-install": "dist" }, - "type": "prestashop-module", - "author": "PrestaShop" -} \ No newline at end of file + "type": "prestashop-module" +} diff --git a/tests/integration/module-samples/gsitemap/composer.json b/tests/integration/module-samples/gsitemap/composer.json index c3dfa4b..7ecd64c 100644 --- a/tests/integration/module-samples/gsitemap/composer.json +++ b/tests/integration/module-samples/gsitemap/composer.json @@ -3,18 +3,11 @@ "description": "PrestaShop module gsitemap", "homepage": "https://github.com/PrestaShop/gsitemap", "license": "AFL-3.0", - "authors": [ - { - "name": "PrestaShop SA", - "email": "contact@prestashop.com" - } - ], "require": { "php": ">=5.6.0" }, "config": { "preferred-install": "dist" }, - "type": "prestashop-module", - "author": "PrestaShop" -} \ No newline at end of file + "type": "prestashop-module" +} diff --git a/tests/integration/runner/FolderComparator.php b/tests/integration/runner/FolderComparator.php index 0c4af14..579badc 100644 --- a/tests/integration/runner/FolderComparator.php +++ b/tests/integration/runner/FolderComparator.php @@ -32,7 +32,6 @@ class FolderComparator 'autoload_static.php', 'autoload_real.php', 'ClassLoader.php', - 'composer.json', ]; /**