Skip to content

Commit

Permalink
chore: flush backend stale chunks
Browse files Browse the repository at this point in the history
Signed-off-by: Sami Mazouz <[email protected]>
  • Loading branch information
SychO9 committed Jul 27, 2023
1 parent a9822f0 commit ce8fb01
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
30 changes: 26 additions & 4 deletions framework/core/src/Frontend/Compiler/JsDirectoryCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ class JsDirectoryCompiler implements CompilerInterface
{
use HasSources;

protected VersionerInterface $versioner;

public function __construct(
protected Cloud $assetsDir,
protected string $destinationPath
) {
$this->versioner = new FileVersioner($assetsDir);
}

public function getFilename(): ?string
Expand Down Expand Up @@ -69,6 +72,20 @@ protected function compileSource(DirectorySource $source, bool $force = false):
protected function flushSource(DirectorySource $source): void
{
$this->eachFile($source, fn (JsCompiler $compiler) => $compiler->flush());

$destinationDir = $this->destinationFor($source);

// Destination can still contain stale chunks.
$this->assetsDir->deleteDirectory($destinationDir);

// Delete stale revisions.
$remainingRevisions = $this->versioner->allRevisions();

foreach ($remainingRevisions as $filename => $revision) {
if (str_starts_with($filename, $destinationDir)) {
$this->versioner->putRevision($filename, null);
}
}
}

protected function eachFile(DirectorySource $source, callable $callback): void
Expand All @@ -86,17 +103,15 @@ protected function eachFile(DirectorySource $source, callable $callback): void
}
}

protected function compilerFor(DirectorySource $source, FilesystemAdapter $filesystem, mixed $relativeFilePath): JsCompiler
protected function compilerFor(DirectorySource $source, FilesystemAdapter $filesystem, string $relativeFilePath): JsCompiler
{
// Filesystem's root is the actual directory we want to copy.
// The destination path is relative to the assets' filesystem.

$extensionId = $source->getExtensionId() ?? 'core';

$jsCompiler = resolve(JsCompiler::class, [
'assetsDir' => $this->assetsDir,
// We put each file in `js/extensionId/frontend` (path provided) `/relativeFilePath` (such as `components/LogInModal.js`).
'filename' => str_replace('{ext}', $extensionId, $this->destinationPath).DIRECTORY_SEPARATOR.$relativeFilePath,
'filename' => $this->destinationFor($source, $relativeFilePath),
]);

$jsCompiler->addSources(
Expand All @@ -105,4 +120,11 @@ protected function compilerFor(DirectorySource $source, FilesystemAdapter $files

return $jsCompiler;
}

protected function destinationFor(DirectorySource $source, ?string $relativeFilePath = null): string
{
$extensionId = $source->getExtensionId() ?? 'core';

return str_replace('{ext}', $extensionId, $this->destinationPath).DIRECTORY_SEPARATOR.$relativeFilePath;
}
}
2 changes: 1 addition & 1 deletion framework/core/src/Frontend/Compiler/RevisionCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RevisionCompiler implements CompilerInterface

const EMPTY_REVISION = 'empty';

protected VersionerInterface|FileVersioner $versioner;
protected VersionerInterface $versioner;

public function __construct(
protected Cloud $assetsDir,
Expand Down

0 comments on commit ce8fb01

Please sign in to comment.