From db84b66a2a5ce7aa0cec5b30f4f0e834d6bc9cc6 Mon Sep 17 00:00:00 2001 From: Chris DeLuca <637174+bronzehedwick@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:28:02 -0400 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 --- drainpipe-dev/bin/drainpipe-twig-linter | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drainpipe-dev/bin/drainpipe-twig-linter b/drainpipe-dev/bin/drainpipe-twig-linter index 528848305..607934fa8 100755 --- a/drainpipe-dev/bin/drainpipe-twig-linter +++ b/drainpipe-dev/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);