Skip to content

Commit 9e81bab

Browse files
committed
feat: Allow multiple line with array for add-lines
1 parent 8ce1acd commit 9e81bab

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/Configurator/AddLinesConfigurator.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,14 @@ public function executeUnconfigure(Recipe $recipe, $config): void
164164
}
165165
}
166166

167-
private function getPatchedContents(string $file, string $value, string $position, ?string $target, bool $warnIfMissing): string
167+
private function getPatchedContents(string $file, string|array $value, string $position, ?string $target, bool $warnIfMissing): string
168168
{
169169
$fileContents = $this->readFile($file);
170170

171+
if (is_array($value)) {
172+
$value = implode(PHP_EOL, $value);
173+
}
174+
171175
if (false !== strpos($fileContents, $value)) {
172176
return $fileContents; // already includes value, skip
173177
}

tests/Configurator/AddLinesConfiguratorTest.php

+64
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,70 @@ public function getUpdateTests()
562562
import './stimulus_bootstrap';
563563
import * as Turbo from '@hotwired/turbo';
564564
565+
console.log(Turbo);
566+
EOF
567+
],
568+
];
569+
570+
yield 'recipe_changes_with_multiline_string' => [
571+
['assets/app.js' => $appJsOriginal],
572+
[
573+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';"],
574+
],
575+
[
576+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './stimulus_bootstrap';\nimport { useIntersection } from 'stimulus-use';"],
577+
],
578+
['assets/app.js' => <<<EOF
579+
import './stimulus_bootstrap';
580+
import { useIntersection } from 'stimulus-use';
581+
import * as Turbo from '@hotwired/turbo';
582+
583+
console.log(Turbo);
584+
EOF
585+
],
586+
];
587+
588+
yield 'recipe_changes_with_multiline_array' => [
589+
['assets/app.js' => $appJsOriginal],
590+
[
591+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';"],
592+
],
593+
[
594+
['file' => 'assets/app.js', 'position' => 'top', 'content' => [
595+
"import './stimulus_bootstrap';",
596+
"import { useIntersection } from 'stimulus-use';"
597+
]],
598+
],
599+
['assets/app.js' => <<<EOF
600+
import './stimulus_bootstrap';
601+
import { useIntersection } from 'stimulus-use';
602+
import * as Turbo from '@hotwired/turbo';
603+
604+
console.log(Turbo);
605+
EOF
606+
],
607+
];
608+
609+
yield 'recipe_changes_with_complex_multiline_array' => [
610+
['assets/app.js' => $appJsOriginal],
611+
[
612+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';"],
613+
],
614+
[
615+
['file' => 'assets/app.js', 'position' => 'top', 'content' => [
616+
"import './stimulus_bootstrap';",
617+
"import { useIntersection } from 'stimulus-use';",
618+
"",
619+
" console.log(years['2025'] !== years['0225']);",
620+
]],
621+
],
622+
['assets/app.js' => <<<EOF
623+
import './stimulus_bootstrap';
624+
import { useIntersection } from 'stimulus-use';
625+
626+
console.log(years['2025'] !== years['0225']);
627+
import * as Turbo from '@hotwired/turbo';
628+
565629
console.log(Turbo);
566630
EOF
567631
],

0 commit comments

Comments
 (0)