Skip to content

Commit

Permalink
add automatic tailwind build after post update
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDev committed Oct 24, 2023
1 parent c3ba96a commit 5871784
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 8 deletions.
21 changes: 19 additions & 2 deletions packages/core/src/EventListener/PageListener.php
Expand Up @@ -2,17 +2,24 @@

namespace Pushword\Core\EventListener;

use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Events;
use Pushword\Core\Entity\PageInterface;
use Pushword\Core\Service\PageOpenGraphImageGenerator;
use Pushword\Core\Service\TailwindGenerator;
use Symfony\Bundle\SecurityBundle\Security;

// Symfony\Component\Security\Core\Security;

#[AsEntityListener(event: Events::preRemove, entity: '%pw.entity_page%')]
#[AsEntityListener(event: Events::preUpdate, entity: '%pw.entity_page%')]
#[AsEntityListener(event: Events::prePersist, entity: '%pw.entity_page%')]
#[AsEntityListener(event: Events::postPersist, entity: '%pw.entity_page%')]
#[AsEntityListener(event: Events::postUpdate, entity: '%pw.entity_page%')]
final class PageListener
{
public function __construct(
private readonly Security $security,
private readonly PageOpenGraphImageGenerator $pageOpenGraphImageGenerator,
private readonly TailwindGenerator $tailwindGenerator,
) {
}

Expand All @@ -33,6 +40,16 @@ public function prePersist(PageInterface $page): void
$this->pageOpenGraphImageGenerator->setPage($page)->generatePreviewImage();
}

public function postPersist(PageInterface $page): void
{
$this->tailwindGenerator->run($page);
}

public function postUpdate(PageInterface $page): void
{
$this->tailwindGenerator->run($page);
}

public function preUpdate(PageInterface $page): void
{
$this->updatePageEditor($page);
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/Resources/config/services.yaml
Expand Up @@ -44,12 +44,7 @@ services:
# Page
# --------

# Todo: move to EventSuscriber
Pushword\Core\EventListener\PageListener:
tags:
- { name: doctrine.orm.entity_listener, entity: "%pw.entity_page%", event: preRemove }
- { name: doctrine.orm.entity_listener, entity: "%pw.entity_page%", event: preUpdate }
- { name: doctrine.orm.entity_listener, entity: "%pw.entity_page%", event: prePersist }


Pushword\Core\Repository\PageRepository:
arguments:
Expand Down
29 changes: 29 additions & 0 deletions packages/core/src/Service/TailwindGenerator.php
@@ -0,0 +1,29 @@
<?php

namespace Pushword\Core\Service;

use Pushword\Core\Entity\PageInterface;
use Symfony\Component\Filesystem\Filesystem;

class TailwindGenerator
{
public function __construct(
private readonly string $projectDir,
) {
}

public function run(PageInterface $page): void
{
if (! file_exists($this->projectDir.'/assets/webpack.config.js')) {
return;
}

$fs = new FileSystem();
$fs->dumpFile(
$this->projectDir.'/var/TailwindGeneratorCache/'.$page->getId(),
serialize($page)
);

@exec('cd "'.str_replace('"', '\"', $this->projectDir).'/assets" && yarn encore production >/dev/null 2>&1 &');
}
}
1 change: 1 addition & 0 deletions packages/js-helper/src/encore.js
Expand Up @@ -12,6 +12,7 @@ function getFilesToWatch(basePath = './..') {
basePath + '/templates/*.html.twig',
basePath + '/templates/**/*.html.twig',
basePath + '/templates/**/**/*.html.twig',
basePath + '/var/TailwindGeneratorCache/*',
];
}
function getTailwindConfig(watchFiles = null) {
Expand Down
Binary file modified packages/skeleton/var/app.db
Binary file not shown.

0 comments on commit 5871784

Please sign in to comment.