Skip to content

Commit

Permalink
FIX Don't keep empty files (#28)
Browse files Browse the repository at this point in the history
* FIX Don't keep empty files

Files can be empty if previous runs provided incorrect values (e.g.
english strings inside non-english files), and subsequent runs remove
the errant values.

* FIX Allow committing deletion of files
  • Loading branch information
GuySartorelli authored May 7, 2024
1 parent 25f272f commit 72ae09b
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function run()
$this->cleanYaml();
$this->mergeJson();
$this->removeEnglishStringsFromJsonTranslations();
$this->removeEmptyYamlFiles();
$this->removeEmptyJsFiles();
}
if ($this->doCollectStrings) {
$this->collectStrings();
Expand Down Expand Up @@ -531,7 +533,7 @@ private function gitCommitPushAndPullRequest(): void
$langPath = $this->getYmlLangDirectory($modulePath);
foreach (array_merge((array) $jsPath, (array) $langPath) as $path) {
if (is_dir($path)) {
$this->exec("git add $path/*", $modulePath);
$this->exec("git add $path", $modulePath);
}
}
$this->exec("git add .tx/config", $modulePath);
Expand Down Expand Up @@ -729,9 +731,65 @@ private function generateJavascriptInDirectory(string $modulePath, string $jsPat
file_put_contents($targetFile, $targetContents);
$count++;
}
// Delete any javascript files which don't have a src file
foreach (glob("{$jsPath}/*.js") as $filePath) {
$dir = dirname($filePath);
$fileName = basename($filePath);
$srcFilePath = "$dir/src/$fileName";
// no src .js and no src .json
if (!file_exists($srcFilePath) && !file_exists($srcFilePath . 'on')) {
$this->log("Deleting empty js file: $filePath", true);
$success = unlink($filePath);
if (!$success) {
throw new RuntimeException("Couldn't delete empty yaml file: $filePath");
}
}
}
return $count;
}

private function removeEmptyYamlFiles(): void
{
foreach ($this->modulePaths as $modulePath) {
foreach (glob($this->getYmlLangDirectory($modulePath) . '/*.yml') as $filePath) {
$rawYaml = file_get_contents($filePath);
$parsed = Yaml::parse($rawYaml);
$isEmpty = true;
foreach (array_keys($parsed) as $countryCode) {
if (!empty($parsed[$countryCode])) {
$isEmpty = false;
}
}
if ($isEmpty) {
$this->log("Deleting empty yaml file: $filePath", true);
$success = unlink($filePath);
if (!$success) {
throw new RuntimeException("Couldn't delete empty yaml file: $filePath");
}
}
}
}
}

private function removeEmptyJsFiles(): void
{
foreach ($this->modulePaths as $modulePath) {
$jsPaths = $this->getJSLangDirectories($modulePath);
foreach ((array)$jsPaths as $jsPath) {
foreach (glob("{$jsPath}/src/*.js*") as $filePath) {
$sourceContentsDecoded = $this->jsonDecode(file_get_contents($filePath));
if (empty($sourceContentsDecoded)) {
$this->log("Deleting empty js src file: $filePath", true);
$success = unlink($filePath);
if (!$success) {
throw new RuntimeException("Couldn't delete empty json file: $filePath");
}
}
}
}
}
}

private function jsonEncode(array $data): string
{
$content = json_encode($data, JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES + JSON_UNESCAPED_UNICODE);
Expand Down

0 comments on commit 72ae09b

Please sign in to comment.