From cea2266fd9f114fbca354a194f583b53ce9f50a8 Mon Sep 17 00:00:00 2001 From: "Lullabot-Drainpipe[bot]" <157769597+Lullabot-Drainpipe[bot]@users.noreply.github.com> Date: Fri, 26 Jul 2024 18:28:18 +0000 Subject: [PATCH] feat(twig): support storybook twig extension in linting (#613) The automatic twig linting throws a fatal error when the Storybook module is present, as the static tests don't bootstrap Drupal to discover twig extensions. Since bootstrapping Drupal is expensive, instead check and load the twig extension manually. Re https://github.com/Lullabot/appstate-drupal/pull/33#issuecomment-2231807673 Co-authored-by: Sally Young Co-authored-by: Andrew Berry --- bin/drainpipe-twig-linter | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/bin/drainpipe-twig-linter b/bin/drainpipe-twig-linter index 5288483..607934f 100755 --- a/bin/drainpipe-twig-linter +++ b/bin/drainpipe-twig-linter @@ -36,6 +36,16 @@ $themeManager = Mockery::mock(ThemeManagerInterface::class); $dateFormatter = Mockery::mock(DateFormatterInterface::class); $fileUrlGenerator = Mockery::mock(FileUrlGenerator::class); +if (class_exists('\TwigStorybook\Twig\TwigExtension')) { + $composer_json = json_decode(file_get_contents(__DIR__ . '/../../../../composer.json'), true); + if (json_last_error()) { + throw new \RuntimeException('Could not parse composer.json'); + } + + $web_root = $composer_json['extra']['drupal-scaffold']['locations']['web-root']; + $twig->addExtension(new \TwigStorybook\Twig\TwigExtension(new \TwigStorybook\Service\StoryCollector(), '/../../../../' . $web_root)); +} + $twig->addExtension(new TwigExtension($renderer, $urlGenerator, $themeManager, $dateFormatter, $fileUrlGenerator)); $lintCommand = new LintCommand($twig);