Skip to content

Commit 7830da7

Browse files
committed
refactor(composer-updater): Improve diff formatting
- Refactored the formatDiff method in the composer-updater script - Improved the diff output by adding color coding for added and removed lines
1 parent 3806175 commit 7830da7

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

composer-updater

+31-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,10 @@ $status = (new SingleCommandApplication)
125125
).PHP_EOL;
126126

127127
if ($this->dryRun) {
128-
echo $this->differ->diff($this->composerJsonContents, $outdatedComposerJsonContents);
128+
$this->symfonyStyle->writeln($this->formatDiff($this->differ->diff(
129+
$this->composerJsonContents,
130+
$outdatedComposerJsonContents
131+
)));
129132

130133
return $this;
131134
}
@@ -291,6 +294,33 @@ $status = (new SingleCommandApplication)
291294

292295
return false;
293296
}
297+
298+
private function formatDiff(string $diff): string
299+
{
300+
$lines = explode(
301+
"\n",
302+
$diff,
303+
);
304+
305+
$formatted = array_map(static function (string $line): string {
306+
return preg_replace(
307+
[
308+
'/^(\+.*)$/',
309+
'/^(-.*)$/',
310+
],
311+
[
312+
'<fg=green>$1</>',
313+
'<fg=red>$1</>',
314+
],
315+
$line,
316+
);
317+
}, $lines);
318+
319+
return implode(
320+
"\n",
321+
$formatted,
322+
);
323+
}
294324
})();
295325
})
296326
->run();

0 commit comments

Comments
 (0)