Skip to content

Commit b411f39

Browse files
committed
feat: Allow multiple line with array for add-lines
1 parent 5919c36 commit b411f39

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("\n", $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
@@ -566,6 +566,70 @@ public function getUpdateTests()
566566
EOF
567567
],
568568
];
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+
629+
console.log(Turbo);
630+
EOF,
631+
],
632+
];
569633
}
570634

571635
private function runConfigure(array $config, ?Composer $composer = null)

0 commit comments

Comments
 (0)