Skip to content

Commit

Permalink
Setting of the languagehashes to the environment during server startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Mahnke committed Feb 13, 2024
1 parent 654224f commit c81e7db
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/config/config.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { red, blue, green, bold } from 'colors';
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from 'fs';
import { load } from 'js-yaml';
import { join } from 'path';

Expand All @@ -9,6 +9,7 @@ import { DefaultAppConfig } from './default-app-config';
import { ServerConfig } from './server-config.interface';
import { mergeConfig } from './config.util';
import { isNotEmpty } from '../app/shared/empty.util';
import { LanguageHashesConfig } from './languageHashes-config.interface';

const CONFIG_PATH = join(process.cwd(), 'config');

Expand Down Expand Up @@ -231,6 +232,29 @@ export const buildAppConfig = (destConfigPath?: string): AppConfig => {
// apply build defined production
appConfig.production = env === 'production';

const useDynamicLanguageHashes = isNotEmpty(ENV('DYNAMIC_LANGUAGE_HASHES', true)) ? ENV('DYNAMIC_LANGUAGE_HASHES', true) : false;
if (appConfig.production && useDynamicLanguageHashes) {
const i18nAssetsDir = 'dist/server/assets/i18n/';
const i18nFiles = readdirSync(i18nAssetsDir);
//sort all the i18nFiles by mtime descending
i18nFiles.sort(function(a, b) {
return statSync(i18nAssetsDir + b).mtime.getTime() - statSync(i18nAssetsDir + a).mtime.getTime();
});

i18nFiles.forEach(file => {
const match = file.match(/^(.+)\.(.+)\.json$/);
// only add language hash to config if no config exists for current language (ensuring the latest language file
// is used)
if (match && appConfig.languageHashes.filter(((languageHashConfig) => languageHashConfig.lang === match[1])).length == 0) {

Check failure on line 248 in src/config/config.server.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Expected '===' and instead saw '=='

Check failure on line 248 in src/config/config.server.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Expected '===' and instead saw '=='
const languageConfig: LanguageHashesConfig = {
lang: match[1],
md5: match[2]
}

Check failure on line 252 in src/config/config.server.ts

View workflow job for this annotation

GitHub Actions / tests (16.x)

Missing semicolon

Check failure on line 252 in src/config/config.server.ts

View workflow job for this annotation

GitHub Actions / tests (18.x)

Missing semicolon
appConfig.languageHashes.push(languageConfig);
}
});
}

// build base URLs
buildBaseUrl(appConfig.ui);
buildBaseUrl(appConfig.rest);
Expand Down

0 comments on commit c81e7db

Please sign in to comment.