Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .github/workflows/backend-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ jobs:
satis-network-key: ${{ secrets.SATIS_NETWORK_KEY }}
satis-network-token: ${{ secrets.SATIS_NETWORK_TOKEN }}

- uses: actions/setup-node@v4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

outdated action version

Suggested change
- uses: actions/setup-node@v4
- uses: actions/setup-node@v7

with:
node-version: '20'
Comment thread
albozek marked this conversation as resolved.
Outdated

- name: Install frontend dependencies
run: yarn install

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SonarQube:

Lifecycle scripts are enabled by default in Yarn v2+.
See more on https://sonarcloud.io/project/issues?id=ibexa_admin-ui&issues=AZ9_yweGtPeG6EAUOR9u&open=AZ9_yweGtPeG6EAUOR9u&pullRequest=1967

This looks relevant to the case, but if it's false positive, please mark it as false positive. I'm not able to assess this one on my own.

(you can use GitHub to log in to Sonar, so you can access full report, lmk if you have questions)


- name: Setup problem matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

Expand Down Expand Up @@ -131,6 +138,13 @@ jobs:
satis-network-key: ${{ secrets.SATIS_NETWORK_KEY }}
satis-network-token: ${{ secrets.SATIS_NETWORK_TOKEN }}

- uses: actions/setup-node@v4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- uses: actions/setup-node@v4
- uses: actions/setup-node@v7

with:
node-version: '20'

- name: Install frontend dependencies
run: yarn install

- name: Setup problem matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

Expand Down Expand Up @@ -182,6 +196,13 @@ jobs:
satis-network-key: ${{ secrets.SATIS_NETWORK_KEY }}
satis-network-token: ${{ secrets.SATIS_NETWORK_TOKEN }}

- uses: actions/setup-node@v4

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- uses: actions/setup-node@v4
- uses: actions/setup-node@v7

with:
node-version: '20'

- name: Install frontend dependencies
run: yarn install

- name: Setup problem matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"symfony/http-foundation": "^7.4",
"symfony/http-kernel": "^7.4",
"symfony/options-resolver": "^7.4",
"symfony/process": "^7.4",
"symfony/routing": "^7.4",
"symfony/security-core": "^7.4",
"symfony/security-http": "^7.4",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"@ibexa/eslint-config": "https://github.com/ibexa/eslint-config-ibexa.git#~v2.0.0",
"@ibexa/ts-config": "https://github.com/ibexa/ts-config-ibexa#~v1.1.0",
"@types/react": "^19.1.2",
"@types/react-dom": "^19.1.2"
"@types/react-dom": "^19.1.2",
"@typescript-eslint/typescript-estree": "^8.62.1"
},
"scripts": {
"test": "yarn prettier-test && yarn ts-test && yarn eslint-test",
Expand Down
6 changes: 6 additions & 0 deletions src/bundle/Resources/config/services/translation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ services:
tags:
- { name: jms_translation.file_visitor }

Ibexa\AdminUi\Translation\Extractor\TypeScriptFileVisitor:
tags:
- { name: jms_translation.file_visitor }

Ibexa\AdminUi\Translation\Extractor\TypeScriptExtractorClient: ~

Ibexa\AdminUi\Translation\Extractor\SortingTranslationExtractor:
tags:
- { name: jms_translation.extractor, alias: ez_location_sorting }
Expand Down
193 changes: 193 additions & 0 deletions src/lib/Translation/Extractor/TypeScriptExtractorClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\AdminUi\Translation\Extractor;

use JsonException;
use RuntimeException;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is run in CLI, then one of Symfony\Console\ exceptions should be used.

Comes from SonarQube analysis, but I find this valid.

use Symfony\Component\Process\InputStream;
use Symfony\Component\Process\Process;

final class TypeScriptExtractorClient

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final class TypeScriptExtractorClient
final readonly class TypeScriptExtractorClient

{
private const DEFAULT_RESPONSE_TIMEOUT_SECONDS = 30.0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private const DEFAULT_RESPONSE_TIMEOUT_SECONDS = 30.0;
private const float DEFAULT_RESPONSE_TIMEOUT_SECONDS = 30.0;


private string $parserScriptPath;

private bool $isRuntimeReady = false;

private ?Process $serverProcess = null;

private ?InputStream $serverInput = null;

private string $responseBuffer = '';

public function __construct(
?string $parserScriptPath = null,
private readonly string $nodeBinary = 'node',
private readonly float $responseTimeoutSeconds = self::DEFAULT_RESPONSE_TIMEOUT_SECONDS,
) {
$this->parserScriptPath = $parserScriptPath ?? __DIR__ . '/typescript_translation_extractor.mjs';
}

public function __destruct()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this have a return type?

{
$this->discardServerProcess();
}

/**
* @return array{messages?: mixed, warnings?: mixed, error?: string}
*/
public function extract(string $filePath): array
{
$this->assertRuntimeIsReady();
$this->ensureServerStarted();

$this->getServerInput()->write($filePath . "\n");

return $this->readExtractionResponse();
}

private function assertRuntimeIsReady(): void
{
if ($this->isRuntimeReady) {
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Architecture: is there a better way of checking readiness? For e.g., existence of some file?

  1. This creates stateful service, which tends to cause problems and is not reliable really, as it lives in the current process memory only
  2. it doesn't check if the runtime did its job.

I don't have a solution, because I don't know what this runtime does, but maybe it's worth looking into.


if (!is_file($this->parserScriptPath)) {
throw new RuntimeException(sprintf(
'TypeScript translation extractor script not found: %s.',
$this->parserScriptPath,
));
}

$runtimeCheckProcess = new Process([
$this->nodeBinary,
$this->parserScriptPath,
'--check-runtime',
]);
$runtimeCheckProcess->run();

if (!$runtimeCheckProcess->isSuccessful()) {
throw new RuntimeException(sprintf(
'TypeScript translation extractor runtime is not available. Please ensure `%s` is installed and `@typescript-eslint/typescript-estree` is available for the admin-ui package. %s',
$this->nodeBinary,
trim($runtimeCheckProcess->getErrorOutput()),
));
}

$this->isRuntimeReady = true;
}

private function ensureServerStarted(): void
{
if ($this->serverProcess !== null && $this->serverProcess->isRunning()) {
return;
}

$processInput = new InputStream();

$extractorProcess = new Process([
$this->nodeBinary,
$this->parserScriptPath,
'--serve',
]);
$extractorProcess->setInput($processInput);
$extractorProcess->setTimeout(null);
$extractorProcess->start();

$this->serverInput = $processInput;
$this->serverProcess = $extractorProcess;
$this->responseBuffer = '';
}

private function getServerInput(): InputStream
{
if ($this->serverInput === null) {
throw new RuntimeException('TypeScript translation extractor input stream has not been started.');
}

return $this->serverInput;
}

private function getServerProcess(): Process
{
if ($this->serverProcess === null) {
throw new RuntimeException('TypeScript translation extractor process has not been started.');
}

return $this->serverProcess;
}

/**
* @return array{messages?: mixed, warnings?: mixed, error?: string}
*/
private function readExtractionResponse(): array
{
$process = $this->getServerProcess();
$responseDeadline = microtime(true) + $this->responseTimeoutSeconds;

while (!str_contains($this->responseBuffer, "\n")) {
if (!$process->isRunning()) {
$this->discardServerProcess();

throw new RuntimeException(sprintf(
'TypeScript translation extractor process terminated unexpectedly: %s',
trim($process->getErrorOutput()),
));
}

if (microtime(true) > $responseDeadline) {
$this->discardServerProcess();

throw new RuntimeException(
'Timed out waiting for the TypeScript translation extractor process to respond.',
);
}

$this->responseBuffer .= $process->getIncrementalOutput();

if (!str_contains($this->responseBuffer, "\n")) {
usleep(2000);
}
}

[$responseLine, $rest] = explode("\n", $this->responseBuffer, 2);
$this->responseBuffer = $rest;

return $this->decodeResponse($responseLine);
}

/**
* @return array{messages?: mixed, warnings?: mixed, error?: string}
*/
private function decodeResponse(string $responseLine): array
{
try {
$responseData = json_decode($responseLine, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException) {
return ['error' => 'Unable to decode extractor output.'];
}

if (!is_array($responseData)) {
return ['error' => 'Extractor output must be a JSON object.'];
}

return $responseData;
}

private function discardServerProcess(): void
{
$this->serverInput?->close();
$this->serverProcess?->stop(3);

$this->serverProcess = null;
$this->serverInput = null;
$this->responseBuffer = '';
}
}
Loading
Loading