-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add pretty-json command * fixup! Add pretty-json command
- Loading branch information
1 parent
b2b162e
commit 64d9bf8
Showing
5 changed files
with
107 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\SwissKnife\Command; | ||
|
||
use Nette\Utils\FileSystem; | ||
use Nette\Utils\Json; | ||
use Rector\SwissKnife\Finder\FilesFinder; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
final class PrettyJsonCommand extends Command | ||
{ | ||
public function __construct( | ||
private readonly SymfonyStyle $symfonyStyle, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('pretty-json'); | ||
|
||
$this->addArgument( | ||
'sources', | ||
InputArgument::REQUIRED | InputArgument::IS_ARRAY, | ||
'File or directory to prettify' | ||
); | ||
$this->setDescription('Turns JSON files from 1-line format to pretty format'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$sources = (array) $input->getArgument('sources'); | ||
$jsonFileInfos = FilesFinder::findJsonFiles($sources); | ||
|
||
if (count($jsonFileInfos) === 0) { | ||
$this->symfonyStyle->error('No *.json files found'); | ||
return self::FAILURE; | ||
} | ||
|
||
$message = sprintf('Analysing %d *.json files', count($jsonFileInfos)); | ||
$this->symfonyStyle->note($message); | ||
|
||
// convert file infos from uggly json to pretty json | ||
foreach ($jsonFileInfos as $jsonFileInfo) { | ||
$jsonContent = FileSystem::read($jsonFileInfo->getRealPath()); | ||
$prettyJsonContent = Json::encode(Json::decode($jsonContent), JSON_PRETTY_PRINT); | ||
|
||
// nothing to convert | ||
if ($prettyJsonContent === $jsonContent) { | ||
$this->symfonyStyle->writeln(sprintf('The "%s" file is already pretty', $jsonFileInfo->getRealPath())); | ||
continue; | ||
} | ||
|
||
// notify the file was changed | ||
$this->symfonyStyle->writeln( | ||
sprintf('The "%s" file was changed to pretty json', $jsonFileInfo->getRealPath()) | ||
); | ||
FileSystem::write($jsonFileInfo->getRealPath(), $prettyJsonContent); | ||
} | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters